Skip to content

Commit 5b047e2

Browse files
fix(filesystem): preserve CLI directories when MCP roots are available
CLI-provided allowed directories are now respected as the authoritative source when present. Previously, the oninitialized handler and roots/list_changed notification handler would unconditionally replace CLI directories with the client's MCP roots, making it impossible to scope the server to directories outside the client's project root. The fix tracks whether directories were explicitly provided via CLI arguments and skips root resolution in both handlers when they were. MCP roots are still used as a fallback when no CLI directories are given. Fixes modelcontextprotocol#3929
1 parent d5bfe34 commit 5b047e2

1 file changed

Lines changed: 15 additions & 1 deletion

File tree

‎src/filesystem/index.ts‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ if (accessibleDirectories.length === 0 && allowedDirectories.length > 0) {
8989

9090
allowedDirectories = accessibleDirectories;
9191

92+
// Track whether directories were explicitly provided via CLI arguments.
93+
// When CLI directories are provided, they take precedence over MCP roots
94+
// to prevent clients from overriding the user's explicit configuration.
95+
const cliDirectoriesProvided = args.length > 0 && allowedDirectories.length > 0;
96+
9297
// Initialize the global allowedDirectories in lib.ts
9398
setAllowedDirectories(allowedDirectories);
9499

@@ -715,7 +720,12 @@ async function updateAllowedDirectoriesFromRoots(requestedRoots: Root[]) {
715720
}
716721

717722
// Handles dynamic roots updates during runtime, when client sends "roots/list_changed" notification, server fetches the updated roots and replaces all allowed directories with the new roots.
723+
// When CLI directories were provided, roots updates are ignored to preserve the explicit configuration.
718724
server.server.setNotificationHandler(RootsListChangedNotificationSchema, async () => {
725+
if (cliDirectoriesProvided) {
726+
console.error("Ignoring roots update: CLI directories take precedence");
727+
return;
728+
}
719729
try {
720730
// Request the updated roots list from the client
721731
const response = await server.server.listRoots();
@@ -728,10 +738,14 @@ server.server.setNotificationHandler(RootsListChangedNotificationSchema, async (
728738
});
729739

730740
// Handles post-initialization setup, specifically checking for and fetching MCP roots.
741+
// CLI-provided directories take precedence over MCP roots to ensure explicit
742+
// user configuration is not overridden by the client's project roots.
731743
server.server.oninitialized = async () => {
732744
const clientCapabilities = server.server.getClientCapabilities();
733745

734-
if (clientCapabilities?.roots) {
746+
if (cliDirectoriesProvided) {
747+
console.error("CLI directories provided, ignoring MCP roots. Allowed directories:", allowedDirectories);
748+
} else if (clientCapabilities?.roots) {
735749
try {
736750
const response = await server.server.listRoots();
737751
if (response && 'roots' in response) {

0 commit comments

Comments
 (0)