modelcontextprotocol / modelcontextprotocol/servers

filesystem edit_file: EPERM error when renaming over locked files on Windows

Open
#3,199 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
TypeScript
Stars
90.5k
Forks
11.7k
Avg merge
2d 2h
Merged PRs (30d)
5

Description

Describe the bug
The filesystem_edit_file MCP tool failed on Windows operating systems with the following error:

To Reproduce
Steps to reproduce the behavior:

  1. Create a text file test.txt with some content
  2. Open the file in a text editor (e.g., VS Code, Notepad) - this adds a file read lock
  3. Attempt to run the following Node.js script to overwrite the file:
import fs from "fs/promises";
import { randomBytes } from 'crypto';

const filePath = './test.txt';
const tempPath = `${filePath}.${randomBytes(16).toString('hex')}.tmp`;

async function editFile() {
  await fs.writeFile(tempPath, 'updated content', 'utf-8');
  await fs.rename(tempPath, filePath); // Throws EPERM on Windows
}

editFile();
  1. Observe the EPERM error

Expected behavior
The tool should successfully overwrite files on Windows, just as it does on Linux and macOS platforms.

Logs
Error: EPERM: operation not permitted, rename 'C:\test.txt.abc123.tmp' -> 'C:\test.txt'.

Additional context
On Windows, fs.rename() cannot overwrite existing files when the file is locked (e.g., opened in an editor), resulting in EPERM error. This is a Windows-specific limitation of the Node.js fs.rename() function.

Solution Applied
Replaced fs.rename() with fs.cp() which supports the force: true option to overwrite existing files:

const tempPath = `${filePath}.${randomBytes(16).toString('hex')}.tmp`;
try {
  await fs.writeFile(tempPath, modifiedContent, 'utf-8');
  // Use cp with force option - works on all platforms including Windows
  await fs.cp(tempPath, filePath, { force: true });
} finally {
  try {
    await fs.unlink(tempPath);
  } catch {}
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Search the repository for the filesystem_edit_file MCP tool and its fs.rename usage; the issue does not name a source file or test. Reproduce the Windows locked-file case, then verify that the temporary-file workflow overwrites the target and cleans up the temporary file without the EPERM error.

Written by the indexing model from the issue text.

Assessment

Tech stack
node.js, typescript
Domain
tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.