Repository navigation
Description
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 byc83a1dd8ec44456e310d88f1eae787baa0ab6a3e("fix(filesystem): address review findings for GHSA-p9cv-8g75-4xcg") and unchanged bydb4cdfddfd447fee6a86ddd5e93f23203160cc2b - 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.tsmoveFile()→pinned-directory.tsrenameIntoDirectory()
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:
- 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; - 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()— theinDirectory(sourceDirectory, …)
block that renames the source toheld(added byc83a1dd8), and the subsequent
inDirectory(destinationDirectory, …)block.src/filesystem/pinned-directory.ts,cannotEnterError()— maps theENOENTfromchdir()to
Parent directory does not exist: <directory>and keeps codeENOENT.- Reached through
src/filesystem/lib.tsmoveFile()→ themove_filetool 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 withENOENTand
the destination directory issourcePathor 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
shivamm-gupta commentedon Nov 18, 2026
Repro'd this on
v2/main— as noted in the issue, the staged rename inrenameIntoDirectoryrenames the source to.mcp-xxx.movingin 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, soinDirectory()in step 2 gets anENOENTwhichcannotEnterErrortranslates to"Parent directory does not exist".I put together a fix with tests in a branch here:
shivamm-gupta@a6ab5b73
Behavior before & after
move_file("mydir", "mydir/sub/nested")throwsENOENT: Parent directory does not exist: .../mydir/sub.EINVAL: invalid argument, rename ...up front before any disk mutations or staging renames occur.pinned-directory.test.tsand protocol integration test inmissing-parent-error.test.tsverifyingEINVALis returned and "Parent directory does not exist" is not emitted.Prompt used
Per repo contribution guidelines, here is the prompt that produced the fix:
cliffhall commentedon Nov 18, 2026
Triage: Priority Low (total 5)
move_fileinto 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)buglabelBoard: Servers V2 (#43), Status
Incoming, awaiting maintainer review (no milestone yet).