Skip to content

fix(filesystem): support recursive create_directory and prevent move_file overwrites - #4654

Open
AbhiPra24 wants to merge 1 commit into
modelcontextprotocol:mainfrom
AbhiPra24:fix/filesystem-create-dir-and-move-file
Open

fix(filesystem): support recursive create_directory and prevent move_file overwrites#4654
AbhiPra24 wants to merge 1 commit into
modelcontextprotocol:mainfrom
AbhiPra24:fix/filesystem-create-dir-and-move-file

Conversation

@AbhiPra24

@AbhiPra24 AbhiPra24 commented Aug 17, 2026

Copy link
Copy Markdown

Summary

Fixes #4629 and #4628.

1. create_directory parent creation (Fixes #4629)

  • Problem: When create_directory is called on a path with multiple non-existent parent directories (e.g. <allowed_dir>/a/b/c), validatePath() failed on path.dirname() throwing "Parent directory does not exist: <allowed_dir>/a/b", preventing recursive directory creation even though create_directory passes { recursive: true } to fs.mkdir.
  • Fix: In validatePath(), when a path does not exist (ENOENT), walk up ancestor directories until an existing ancestor is found, resolving it via fs.realpath and verifying it stays within allowedDirectories.

2. move_file non-overwriting behavior (Fixes #4628)

  • Problem: move_file called fs.rename directly, silently overwriting existing destination files and causing data loss, violating the documented contract ("If the destination exists, the operation will fail").
  • Fix: Added pre-rename check with await fs.lstat(validDestPath) to throw Destination already exists: <path> when target is occupied.

Verification

  • Added regression tests in src/filesystem/__tests__/lib.test.ts and src/filesystem/__tests__/structured-content.test.ts.
  • npm run build and npm test in src/filesystem passed (157/157 tests green).

cc @olaservo for review when available. Thank you!

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

Labels

None yet

Projects

None yet

1 participant