Skip to content

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

Open

Description

@soarkit

Environment

  • Repository: modelcontextprotocol/servers
  • Component: @modelcontextprotocol/server-filesystem (src/filesystem/)
  • Commit under test: 3d22dc17b5d1b434267c9d42f22e7d5b336875fe ("fix(filesystem): address review round 3 for GHSA-p9cv-8g75-4xcg"), with the behaviour introduced by c83a1dd8ec44456e310d88f1eae787baa0ab6a3e ("fix(filesystem): address review findings for GHSA-p9cv-8g75-4xcg") and unchanged by db4cdfddfd447fee6a86ddd5e93f23203160cc2b
  • Baseline: parent commit 036850aeffcd6ce4d21190bde990897a06e61a9e
  • Node.js 24.22.3 on Linux (container, overlayfs); TypeScript 5 compiled with --module esnext --target es2022 --moduleResolution bundler
  • Tool path exercised: move_file → lib.ts moveFile() → pinned-directory.ts renameIntoDirectory()

Summary

move_file on a directory whose destination lies inside that same directory is rejected with
Parent directory does not exist: <destination's parent> and error code ENOENT. The parent
directory does exist; the request is invalid for a different reason (a directory cannot be moved
into its own subtree), and before c83a1dd8 the same call was rejected with EINVAL naming the
rename that the kernel refused. The reordering introduced by c83a1dd8 renames the source
directory out of the way before the destination directory is entered, so by the time the
destination's parent is looked up it has been moved away, and the failure is reported as a missing
directory rather than as the invalid request it is.

Root cause

renameIntoDirectory() (src/filesystem/pinned-directory.ts) now runs in two stages:

  1. anchored at path.dirname(sourcePath), it renames the source to a random staging name
    (stagingName()), and reads that entry's identity — this happens before the destination is
    looked at
    ;
  2. only then does it call inDirectory(path.dirname(destinationPath), …) to move the staged entry
    into the destination directory.

When destinationPath is inside sourcePath (for example move_file("sub", "sub/inner/sub")),
step 1 has already renamed sub to .mcp-<hex>.moving, so sub/inner no longer resolves. The
chdir() in step 2 fails, cannotEnterError() maps that ENOENT to
Parent directory does not exist: <dir> and puts code ENOENT on the error, and the caller never
sees the real reason. In the previous code the destination directory was entered first, so the same
call reached the kernel's rename() and failed with EINVAL ("invalid argument").

The recovery path does restore the source correctly, so no data is lost; what is wrong is the
reported condition and the reported error code.

Affected code

  • src/filesystem/pinned-directory.ts, renameIntoDirectory() — the inDirectory(sourceDirectory, …)
    block that renames the source to held (added by c83a1dd8), and the subsequent
    inDirectory(destinationDirectory, …) block.
  • src/filesystem/pinned-directory.ts, cannotEnterError() — maps the ENOENT from chdir() to
    Parent directory does not exist: <directory> and keeps code ENOENT.
  • Reached through src/filesystem/lib.ts moveFile() → the move_file tool in
    src/filesystem/server.ts.

Reproduction

Reproduction steps (Node.js 24; no MCP client needed — the changed code is executed directly, and
moveFile() from lib.ts is reproduced verbatim around it because it is unchanged by this commit):

// move-into-own-subdir.mjs
import fs from "fs";
// `pinned-directory.js` is compiled from the commit under test (see the build commands below).
const mod = await import("./out/pinned-directory.js");

const root = "/tmp/run/case";
fs.rmSync(root, { recursive: true, force: true });
fs.mkdirSync(`${root}/d1/sub/inner`, { recursive: true });
fs.writeFileSync(`${root}/d1/sub/x.txt`, "x");

const src = `${root}/d1/sub`;
const dst = `${root}/d1/sub/inner/sub`;

// lib.ts moveFile(), reproduced verbatim in behaviour
async function moveFile(sourcePath, destinationPath) {
  try {
    await fs.promises.lstat(destinationPath);
  } catch (error) {
    if (error.code !== "ENOENT") throw error;
    try {
      mod.renameIntoDirectory(sourcePath, destinationPath);
    } catch (renameError) {
      if (renameError.code !== "EXDEV") throw renameError;
      await mod.moveFileAcrossDevices(sourcePath, destinationPath);
    }
    return;
  }
  throw new Error(`Destination already exists: ${destinationPath}`);
}

try {
  await moveFile(src, dst);
} catch (e) {
  console.log("code:", e.code);
  console.log("message:", e.message);
}
console.log(fs.readdirSync(`${root}/d1`), fs.readdirSync(`${root}/d1/sub`));

Build the module for each revision:

git show 036850aeffcd6ce4d21190bde990897a06e61a9e:src/filesystem/pinned-directory.ts > v0/pinned-directory.ts
git show 036850aeffcd6ce4d21190bde990897a06e61a9e:src/filesystem/path-validation.ts > v0/path-validation.ts
git show c83a1dd8ec44456e310d88f1eae787baa0ab6a3e:src/filesystem/pinned-directory.ts > v1/pinned-directory.ts   # same for path-validation.ts
npx tsc --module esnext --target es2022 --moduleResolution bundler --skipLibCheck \
  --outDir out v1/pinned-directory.ts v1/path-validation.ts
node move-into-own-subdir.mjs

Expected behavior

The call fails, and the error states the real condition: the destination is inside the directory
being moved, so the request is invalid (EINVAL, as in the parent commit), or a message saying the
destination cannot be inside the moved directory. Either way the error must not claim that an
existing directory is missing.

Actual behavior

OUTPUT (parent commit 036850ae, before the change):

code: EINVAL
message: EINVAL: invalid argument, rename '/tmp/run/case/d1/sub' -> '.sub.<hex>.moving'

OUTPUT (commit 3d22dc17; identical at c83a1dd8 and db4cdfdd):

code: ENOENT
message: Parent directory does not exist: /tmp/run/case/d1/sub/inner

/tmp/run/case/d1/sub/inner existed when the call was made and still exists afterwards:

after: d1/sub, d1/sub/inner, d1/sub/x.txt (content "x")   // source restored, nothing left staged

Suggested fix

Determine the destination before mutating the source:

  • enter (or at least resolve) path.dirname(destinationPath) before renaming the source to its
    staging name, and refuse with the real condition if that fails; or
  • keep the current order but, when inDirectory(destinationDirectory, …) fails with ENOENT and
    the destination directory is sourcePath or below it, fail with the invalid-argument error
    instead of the missing-parent error.

A check that destinationPath is not sourcePath and not inside it, made before the source is
renamed, would cover the case directly and keep the staging logic unchanged.

Activity

  1. shivamm-gupta commented on Nov 18, 2026

    @shivamm-gupta

    Repro'd this on v2/main — as noted in the issue, the staged rename in renameIntoDirectory renames the source to .mcp-xxx.moving in step 1 before destination directory validation runs. When destination is inside source (e.g. move_file("sub", "sub/inner/sub")), step 1 moves the destination parent away, so inDirectory() in step 2 gets an ENOENT which cannotEnterError translates to "Parent directory does not exist".

    I put together a fix with tests in a branch here:
    shivamm-gupta@a6ab5b73

    Behavior before & after

    • Before: move_file("mydir", "mydir/sub/nested") throws ENOENT: Parent directory does not exist: .../mydir/sub.
    • After: Throws EINVAL: invalid argument, rename ... up front before any disk mutations or staging renames occur.
    • Tests added: Unit test in pinned-directory.test.ts and protocol integration test in missing-parent-error.test.ts verifying EINVAL is returned and "Parent directory does not exist" is not emitted.

    Prompt used

    Per repo contribution guidelines, here is the prompt that produced the fix:

    In @modelcontextprotocol/server-filesystem (src/filesystem/pinned-directory.ts), fix renameIntoDirectory() so that moving a directory into itself or into one of its subdirectories is rejected up front with EINVAL before any staging rename occurs (#5555). Add an isInsideDirectory() check that validates destinationPath is not inside sourcePath when sourcePath is a directory (via lstatSync), throwing an Error with code: "EINVAL". Add unit tests in __tests__/pinned-directory.test.ts and tool tests in __tests__/missing-parent-error.test.ts. Update src/filesystem/README.md and add a patch changeset.

  2. added
    bugSomething isn't working
    server-filesystemReference implementation for the Filesystem MCP server - src/filesystem
    on Nov 18, 2026
  3. cliffhall commented on Nov 18, 2026

    @cliffhall
    Member

    Triage: Priority Low (total 5)

    • Severity 2: move_file into the source's own subtree still fails and the source is restored, but the error names the wrong cause (ENOENT "Parent directory does not exist" instead of an invalid-request error)
    • Urgency 2: wanted eventually, nothing blocked on it
    • Bonuses: +1 bug label

    Board: Servers V2 (#43), Status Incoming, awaiting maintainer review (no milestone yet).

  4. added this to the v2.6.0 milestone on Nov 18, 2026
  5. self-assigned this
    on Nov 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingserver-filesystemReference implementation for the Filesystem MCP server - src/filesystemv2

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions