Skip to content

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

@kuviki

Description

@kuviki

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 {}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions