Skip to content

fix(filesystem): refuse to move a directory into itself with the real reason - #5618

Open
cliffhall wants to merge 1 commit into
v2/mainfrom
v2/fix/5555-filesystem-move-into-own-subtree
Open

cliffhall wants to merge 1 commit into
v2/mainfrom
v2/fix/5555-filesystem-move-into-own-subtree

Conversation

@cliffhall

Copy link
Copy Markdown
Member

Closes #5555

Description

move_file of a directory to a destination inside that same directory (sub → sub/inner/sub) failed with Parent directory does not exist: …/sub/inner, although that directory exists. The same-device move (renameIntoDirectory() in pinned-directory.ts) renames its source to a staging name before it enters the destination's directory, so a destination directory inside the source is carried away with it, and the failed chdir is then worded as a missing parent.

renameIntoDirectory() now refuses that move before anything is renamed, with code EINVAL and the message Cannot move a directory into itself: <destination> is inside <source>. The staging logic is unchanged.

The check (a private isInsideSource() beside renameIntoDirectory()) compares directories by identity rather than by path string: it lstats the source and, when that is a directory, stats each directory from the destination's directory up to the root and compares it with the source using the module's existing isSameEntry(). So:

  • another spelling of the source is still caught (a different case on a case-insensitive volume, another Unicode normalization), with no folding rules of its own;
  • a name that merely extends the source's (sub → sub2) is not caught;
  • a symlink to a directory is not a directory move, and moving one into its target still works;
  • a file source costs one lstat and nothing else;
  • the cross-device path is unaffected: the refusal happens before the first rename, so before EXDEV can arise, and a directory cannot be inside itself across devices.

It is a refusal, not an anchor: it reads by path, and if it is wrong because a directory was swapped in between, the anchored steps of the move still hold as before.

Server Details

  • Server: filesystem
  • Changes to: the move_file tool's error for one invalid request; the README's move_file entry

Motivation and Context

#5555. The error named a condition that was false (an existing directory reported as missing), which sends a client, an LLM in particular, to "fix" it by creating the directory and retrying. Before the staged rename was introduced, the same call failed with the kernel's EINVAL.

The idea of refusing up front with EINVAL is the one the issue suggests, and @shivamm-gupta prototyped it in a comment on the issue (thank you). This change is written independently, and differs in comparing directories by identity instead of by path prefix.

How Has This Been Tested?

Automated. New src/filesystem/__tests__/move-into-own-subtree.test.ts:

  • over the protocol (a Client on the in-memory transport): move_file into a directory below the source, directly inside the source, and with a trailing separator on the source all return the new message, never Parent directory does not exist, and leave the tree untouched with no staging name behind; a destination directory that really is missing still reports Parent directory does not exist; sub → sub2, a directory moved up out of its own subtree, and a file moved into a sibling directory still succeed;
  • on renameIntoDirectory() directly: EINVAL with fs.renameSync never called; a destination directory that does not exist below the source; another case of the source on a case-insensitive volume (skipped where the volume is case-sensitive); a symlink to a directory moved into that directory (succeeds); a missing source still reports the rename's ENOENT.

npm run local:gate exits 0, which includes the per-file coverage gate.

Inspector CLI 2.12.0, both eras, before and after. In a fresh directory $D holding sub/inner/ and sub/x.txt:

npx -y @modelcontextprotocol/inspector@2.12.0 --cli node <dist>/index.js $D \
  --protocol-era <legacy|modern> --method tools/call --tool-name move_file \
  --tool-arg source=$D/sub --tool-arg destination=$D/sub/inner/sub --format json
Build Era Result text (isError: true, exit 5)
v2/main (7f383a8e) legacy (2025-11-25) Parent directory does not exist: $D/sub/inner
v2/main (7f383a8e) modern (2026-07-28) Parent directory does not exist: $D/sub/inner
this branch legacy (2025-11-25) Cannot move a directory into itself: $D/sub/inner/sub is inside $D/sub
this branch modern (2026-07-28) Cannot move a directory into itself: $D/sub/inner/sub is inside $D/sub

The modern results carry io.modelcontextprotocol/serverInfo in _meta. After every run $D still held exactly sub, sub/inner and sub/x.txt.

LLM client: Claude Code 2.1.327, headless (claude -p --mcp-config … --strict-mcp-config --allowedTools mcp__filesystem__move_file). It has no era switch and does not print the protocol version it negotiated, so the era could not be selected. Prompt: "Use the filesystem server's move_file tool to move the directory $D/sub to $D/sub/inner/sub. Then reply with exactly the text the tool returned, followed on a new line by one sentence saying what you would do next."

  • v2/main: the tool returned Parent directory does not exist: $D/sub/inner.
  • this branch: the tool returned Cannot move a directory into itself: $D/sub/inner/sub is inside $D/sub, and the model said it would leave the directory where it is.

Breaking Changes

None. A request that failed before still fails; only its error text changes (and the error's code, ENOENT → EINVAL, inside the server).

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 follow MCP security best practices
  • I have updated the server's README accordingly
  • I have added a changeset (npm run changeset) if this changes what a TypeScript server publishes
  • 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 (not applicable: none added or changed)

Additional context

The tool description and input schema are unchanged, so the interface diff is empty.

🤖 Generated with Claude Code

… reason

move_file of a directory to a destination inside that directory reported
"Parent directory does not exist" (ENOENT) for a destination directory that
did exist: the same-device move renames its source to a staging name first,
which carries the destination's directory away with it.

renameIntoDirectory now refuses that move before anything is renamed, with
EINVAL and "Cannot move a directory into itself: <destination> is inside
<source>". The check compares directories by identity from the destination's
directory up to the root, so another spelling of the source (a different
case on a case-insensitive volume) is still caught, and a symlink to a
directory, which is not a directory move, is not.

Closes #5555

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: cliffhall <cliff@futurescale.com>
@cliffhall cliffhall added the v2 label Nov 18, 2026
@changeset-bot

changeset-bot Bot commented Nov 18, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 37a1f2f

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@modelcontextprotocol/server-filesystem Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions

Copy link
Copy Markdown
Contributor

filesystem: MCP interface diff

Base: 7f383a8e0f34 (Merge pull request #5597 from modelcontextprotocol/changeset-release/v2/main)
Head: the build in this checkout, at 4d225ac26e96 (Merge 37a1f2f into 7f383a8)

Diffed because it was named (--server).

✅ No interface changes detected.

Tools Prompts Resources Resource templates
Base 16 0 0 1
Head 16 0 0 1

Updated by this run for 37a1f2f8dd72a6912969ac990f556422caa801fa.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are reported for the directory self-move error-handling change.

No open issues 🎉

What changed in this PR

Fixes #5555 by making the filesystem server report the real reason when move_file attempts to move a directory into its own subtree, rather than incorrectly reporting a missing parent directory.

Changes:

  • Checks directory identity before staging the move and rejects self-containing moves with EINVAL, leaving staging logic unchanged.
  • Adds regression tests for rejection, an untouched source tree, and valid moves.
  • Documents the error and records a patch changeset.
File Description
src/​filesystem/​README.md Documents the self-move refusal and error message.
src/​filesystem/​pinned-directory.ts Adds an identity-based subtree check before renaming.
src/​filesystem/​__tests__/​move-into-own-subtree.test.ts Tests rejection behavior, tree preservation, and valid moves.
.changeset/​filesystem-move-into-own-subtree.md Records the filesystem package’s patch release note.

Review effort: Balanced

@cliffhall

Copy link
Copy Markdown
Member Author

Copilot round 1: clean (no findings, no suppressed comments). The review loop stops here on the clean-round exit; nothing was pushed in response.

This branch has not been deployed

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

move_file reports "Parent directory does not exist" for a destination whose parent does exist

2 participants