Skip to content

Commit 65c4a49

Browse files
committed
fix(filesystem): use CLI dirs snapshot to prevent accumulation on roots/list_changed
1 parent 6421ba3 commit 65c4a49

2 files changed

Lines changed: 81 additions & 3 deletions

File tree

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
2+
import { mkdtempSync, rmSync, realpathSync } from 'fs';
3+
import { tmpdir } from 'os';
4+
import { join } from 'path';
5+
6+
describe('updateAllowedDirectoriesFromRoots merge behaviour', () => {
7+
let cliDir: string;
8+
let mcpDir1: string;
9+
let mcpDir2: string;
10+
11+
beforeEach(() => {
12+
cliDir = realpathSync(mkdtempSync(join(tmpdir(), 'mcp-cli-')));
13+
mcpDir1 = realpathSync(mkdtempSync(join(tmpdir(), 'mcp-roots1-')));
14+
mcpDir2 = realpathSync(mkdtempSync(join(tmpdir(), 'mcp-roots2-')));
15+
});
16+
17+
afterEach(() => {
18+
rmSync(cliDir, { recursive: true, force: true });
19+
rmSync(mcpDir1, { recursive: true, force: true });
20+
rmSync(mcpDir2, { recursive: true, force: true });
21+
});
22+
23+
it('should preserve CLI dirs after init with roots', () => {
24+
// Simulates the snapshot: cliAllowedDirectories = [cliDir]
25+
const cliAllowedDirectories = [cliDir];
26+
const validatedRootDirs = [mcpDir1];
27+
28+
const merged = new Set([...cliAllowedDirectories, ...validatedRootDirs]);
29+
const allowedDirectories = [...merged];
30+
31+
expect(allowedDirectories).toContain(cliDir);
32+
expect(allowedDirectories).toContain(mcpDir1);
33+
});
34+
35+
it('should replace MCP roots on roots/list_changed without accumulating', () => {
36+
// Simulates: CLI dir + first MCP root
37+
const cliAllowedDirectories = [cliDir];
38+
const firstMcpRoots = [mcpDir1];
39+
40+
let merged = new Set([...cliAllowedDirectories, ...firstMcpRoots]);
41+
let allowedDirectories = [...merged];
42+
43+
expect(allowedDirectories).toContain(cliDir);
44+
expect(allowedDirectories).toContain(mcpDir1);
45+
expect(allowedDirectories).toHaveLength(2);
46+
47+
// Simulates: roots/list_changed with new root (mcpDir2 replaces mcpDir1)
48+
const secondMcpRoots = [mcpDir2];
49+
merged = new Set([...cliAllowedDirectories, ...secondMcpRoots]);
50+
allowedDirectories = [...merged];
51+
52+
expect(allowedDirectories).toContain(cliDir);
53+
expect(allowedDirectories).toContain(mcpDir2);
54+
expect(allowedDirectories).not.toContain(mcpDir1);
55+
expect(allowedDirectories).toHaveLength(2);
56+
});
57+
58+
it('should not accumulate stale roots across multiple updates', () => {
59+
const cliAllowedDirectories = [cliDir];
60+
let allowedDirectories: string[] = [];
61+
62+
for (let i = 0; i < 5; i++) {
63+
const tmpDir = realpathSync(mkdtempSync(join(tmpdir(), `mcp-root-${i}-`)));
64+
const merged = new Set([...cliAllowedDirectories, tmpDir]);
65+
allowedDirectories = [...merged];
66+
rmSync(tmpDir, { recursive: true, force: true });
67+
}
68+
69+
// After each iteration, only CLI dir + latest root should be present
70+
expect(allowedDirectories).toHaveLength(2);
71+
expect(allowedDirectories).toContain(cliDir);
72+
});
73+
});

‎src/filesystem/index.ts‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ if (accessibleDirectories.length === 0 && allowedDirectories.length > 0) {
8989

9090
allowedDirectories = accessibleDirectories;
9191

92+
// Save CLI-provided directories separately so dynamic MCP roots updates
93+
// can replace the previous MCP roots without losing CLI dirs or accumulating stale ones.
94+
const cliAllowedDirectories = [...allowedDirectories];
95+
9296
// Initialize the global allowedDirectories in lib.ts
9397
setAllowedDirectories(allowedDirectories);
9498

@@ -706,8 +710,9 @@ server.registerTool(
706710
async function updateAllowedDirectoriesFromRoots(requestedRoots: Root[]) {
707711
const validatedRootDirs = await getValidRootDirectories(requestedRoots);
708712
if (validatedRootDirs.length > 0) {
709-
// Merge with existing CLI-provided directories instead of replacing
710-
const merged = new Set([...allowedDirectories, ...validatedRootDirs]);
713+
// Merge CLI-provided directories with current MCP roots,
714+
// replacing any previous MCP roots on each update.
715+
const merged = new Set([...cliAllowedDirectories, ...validatedRootDirs]);
711716
allowedDirectories = [...merged];
712717
setAllowedDirectories(allowedDirectories); // Update the global state in lib.ts
713718
console.error(`Updated allowed directories from MCP roots: ${validatedRootDirs.length} valid directories`);
@@ -716,7 +721,7 @@ async function updateAllowedDirectoriesFromRoots(requestedRoots: Root[]) {
716721
}
717722
}
718723

719-
// 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.
724+
// Handles dynamic roots updates during runtime, when client sends "roots/list_changed" notification, server fetches the updated roots and merges them with CLI-provided directories (replacing any previous MCP roots).
720725
server.server.setNotificationHandler(RootsListChangedNotificationSchema, async () => {
721726
try {
722727
// Request the updated roots list from the client

0 commit comments

Comments
 (0)