Skip to content

fix(filesystem): preserve CLI directories when merging with MCP roots#3612

Open
Bingtagui404 wants to merge 5 commits intomodelcontextprotocol:mainfrom
Bingtagui404:fix/filesystem-preserve-cli-dirs
Open

fix(filesystem): preserve CLI directories when merging with MCP roots#3612
Bingtagui404 wants to merge 5 commits intomodelcontextprotocol:mainfrom
Bingtagui404:fix/filesystem-preserve-cli-dirs

Conversation

@Bingtagui404
Copy link

Summary

Fixes #3602

When the MCP client supports the roots protocol, the oninitialized and roots/list_changed handlers unconditionally replaced all CLI-provided allowedDirectories with client roots. This caused any extra directories passed via command-line arguments to be silently discarded.

Before: npx @modelcontextprotocol/server-filesystem /home/user /mnt/Storage /mnt/Games + client root /home/user → only /home/user accessible.

After: CLI directories are preserved as an immutable baseline and merged with client roots. All four directories remain accessible.

Changes

  • src/filesystem/index.ts: Save CLI directories as cliAllowedDirectories baseline. Change updateAllowedDirectoriesFromRoots to merge CLI baseline + current client roots instead of replacing. When roots become empty/invalid, fall back to CLI baseline only (no stale roots).
  • src/filesystem/roots-utils.ts: Add mergeAllowedDirectories() helper — uses Set to deduplicate CLI dirs + root dirs.
  • src/filesystem/__tests__/roots-utils.test.ts: Add 3 regression tests:
    • Preserves CLI directories while adding current client roots
    • Deduplicates directories shared by CLI args and client roots
    • Falls back to CLI baseline when client provides no valid roots

Test plan

  • npx tsc --noEmit — type check passes
  • npx vitest run --config src/filesystem/vitest.config.ts src/filesystem/__tests__/roots-utils.test.ts — all 6 tests pass (3 existing + 3 new)
  • Full filesystem test suite — no new failures introduced

The oninitialized and roots/list_changed handlers replaced all
CLI-provided allowed directories with client roots, silently
discarding any extra directories passed via command-line arguments.

Store CLI directories as an immutable baseline and merge them with
client roots using a Set for deduplication. When roots become empty
or invalid, fall back to the CLI baseline instead of keeping stale
roots.

Fixes modelcontextprotocol#3602
@Bingtagui404 Bingtagui404 force-pushed the fix/filesystem-preserve-cli-dirs branch from 4127473 to a4a0032 Compare March 17, 2026 11:23
@Bingtagui404
Copy link
Author

Filesystem checks are passing for this PR. The only failing job is Build fetch, which appears unrelated to the files changed here. I've pushed a no-op commit to retrigger CI.

@Bingtagui404
Copy link
Author

Filesystem-related checks are passing for this PR. The failing Build fetch job is unrelated to this change: it fails during uv sync --locked --all-extras --dev because src/fetch/uv.lock needs to be updated. The same Build fetch failure is also present on the current main branch (run #23161383338).

@Bingtagui404
Copy link
Author

To clarify: the Build fetch failure is a known repo-wide issue tracked in #3548. It is unrelated to the filesystem changes in this PR.

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.

server-filesystem: MCP roots protocol overwrites CLI-provided allowed directories

1 participant