Skip to content

fix(filesystem): handle Windows EPERM when renaming over locked files#3296

Open
MattBenesch wants to merge 1 commit intomodelcontextprotocol:mainfrom
MattBenesch:fix/issue-3199
Open

fix(filesystem): handle Windows EPERM when renaming over locked files#3296
MattBenesch wants to merge 1 commit intomodelcontextprotocol:mainfrom
MattBenesch:fix/issue-3199

Conversation

@MattBenesch
Copy link

On Windows, fs.rename() fails with EPERM when the target file is locked by another process (e.g., open in an editor). This adds a fallback that uses fs.cp() + fs.unlink() when EPERM is encountered, allowing edits to succeed even when the file is open.

Fixes #3199

Description

Adds atomicReplaceFile() helper that falls back to fs.cp() when fs.rename() fails with EPERM.

Publishing Your Server

Note: We are no longer accepting PRs to add servers to the README. Instead, please publish your server to the MCP Server Registry to make it discoverable to the MCP ecosystem.

To publish your server, follow the quickstart guide. You can browse published servers at https://registry.modelcontextprotocol.io/.

Server Details

  • Server: filesystem
  • Changes to: tools (edit_file, write_file)

Motivation and Context

On Windows, files open in editors (VS Code, Notepad, etc.) are locked. The current fs.rename() approach fails with EPERM when trying to atomically replace these locked files. This prevents users from editing files they have open.

How Has This Been Tested?

Unit tests added for EPERM fallback behavior. All 46 lib.test.ts tests pass.

Breaking Changes

None - existing behavior unchanged on Linux/macOS. Windows users will now have edits succeed where they previously failed.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Protocol Documentation
  • My changes follows MCP security best practices
  • I have updated the server's README accordingly
  • I have tested this with an LLM client
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have documented all environment variables and configuration options

Additional context

On Windows, fs.rename() fails with EPERM when the target file is locked
by another process (e.g., open in an editor). This adds a fallback that
uses fs.cp() + fs.unlink() when EPERM is encountered, allowing edits to
succeed even when the file is open.

Fixes modelcontextprotocol#3199
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant