Skip to content

Commit a13e809

Browse files
cliffhallclaude
andcommitted
fix(filesystem): apply only the newest roots refresh's answer
Each roots refresh (the initial load and every roots/list_changed) takes a generation before it asks for roots; its answer is applied only if no newer refresh has started, checked after both roots/list and validation. A stale answer is logged and discarded, so it cannot undo a newer update such as a revocation. Closes modelcontextprotocol#5097 Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]> (cherry picked from commit 423fe6b) Signed-off-by: cliffhall <[email protected]>
1 parent 2b74b00 commit a13e809

3 files changed

Lines changed: 99 additions & 4 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@modelcontextprotocol/server-filesystem": patch
3+
---
4+
5+
Overlapping roots refreshes now take effect in the order they started: when a client sends `roots/list_changed` again before the previous `roots/list` answer arrived (or while the initial roots are still loading), an answer that arrives after a newer refresh has started is discarded instead of overwriting it, so a stale answer can no longer undo a newer update such as a revocation (#5097).

‎src/filesystem/__tests__/server-roots.test.ts‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,78 @@ describe("roots/list_changed", () => {
345345
expect(await allowedDirectoriesOf(client)).toEqual([cliDir]);
346346
});
347347

348+
// #5097: the answers to overlapping refreshes can arrive out of order; only
349+
// the newest refresh's answer is applied, so a stale one cannot undo it.
350+
it("applies the newest of two overlapping updates when their answers arrive out of order (#5097)", async () => {
351+
let calls = 0;
352+
let releaseStale!: () => void;
353+
const staleHeld = new Promise<void>((resolve) => (releaseStale = resolve));
354+
const { client } = await connectTracked([], {
355+
capabilities: ROOTS,
356+
listRoots: async () => {
357+
calls += 1;
358+
if (calls === 1) return [rootOf(rootDir)]; // the initial load
359+
if (calls === 2) {
360+
await staleHeld; // the older update, answered last
361+
return [rootOf(rootDir)];
362+
}
363+
return []; // the newer update withdraws the root
364+
},
365+
});
366+
await vi.waitFor(async () =>
367+
expect(await allowedDirectoriesOf(client)).toEqual([rootDir]),
368+
);
369+
370+
await client.sendRootsListChanged();
371+
await vi.waitFor(() => expect(calls).toBe(2));
372+
await client.sendRootsListChanged();
373+
await vi.waitFor(async () =>
374+
expect(await allowedDirectoriesOf(client)).toEqual([]),
375+
);
376+
377+
releaseStale();
378+
await vi.waitFor(() =>
379+
expect(stderr).toHaveBeenCalledWith(
380+
"Discarded a stale roots/list answer: a newer roots update has started",
381+
),
382+
);
383+
expect(await allowedDirectoriesOf(client)).toEqual([]);
384+
});
385+
386+
it("discards the initial answer when an update overtakes it (#5097)", async () => {
387+
let calls = 0;
388+
let releaseInitial!: () => void;
389+
const initialHeld = new Promise<void>(
390+
(resolve) => (releaseInitial = resolve),
391+
);
392+
const { client } = await connectTracked([], {
393+
capabilities: ROOTS,
394+
listRoots: async () => {
395+
calls += 1;
396+
if (calls === 1) {
397+
await initialHeld;
398+
return [rootOf(rootDir)];
399+
}
400+
return [rootOf(cliDir)];
401+
},
402+
});
403+
await vi.waitFor(() => expect(calls).toBe(1));
404+
await client.sendRootsListChanged();
405+
await vi.waitFor(() =>
406+
expect(stderr).toHaveBeenCalledWith(
407+
"Updated allowed directories from MCP roots: 1 valid directories",
408+
),
409+
);
410+
411+
releaseInitial();
412+
await vi.waitFor(() =>
413+
expect(stderr).toHaveBeenCalledWith(
414+
"Discarded a stale roots/list answer: a newer roots update has started",
415+
),
416+
);
417+
expect(await allowedDirectoriesOf(client)).toEqual([cliDir]);
418+
});
419+
348420
it("logs and keeps the current directories when the re-fetch fails", async () => {
349421
let fail = false;
350422
const { client } = await connectTracked([], {

‎src/filesystem/server.ts‎

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,12 @@ export function createServer(
177177
// withdraws its roots withdraws the server's access too (#5094). Until then an
178178
// update with no valid roots keeps the command-line directories.
179179
let rootsInForce = false;
180+
// Each roots refresh (the initial load and every roots/list_changed) takes
181+
// the next generation before it asks for roots. The SDK runs notification
182+
// handlers without waiting for earlier ones, so answers can come back out of
183+
// order; one is applied only if no newer refresh has started since, so a
184+
// stale answer cannot undo a newer update, such as a revocation (#5097).
185+
let rootsGeneration = 0;
180186

181187
const server = new McpServer({
182188
name: "secure-filesystem-server",
@@ -763,9 +769,19 @@ export function createServer(
763769
},
764770
);
765771

766-
// Updates allowed directories based on MCP client roots
767-
async function updateAllowedDirectoriesFromRoots(requestedRoots: Root[]) {
772+
// Updates allowed directories based on MCP client roots, unless a newer
773+
// refresh started after this one's `generation` was taken (#5097).
774+
async function updateAllowedDirectoriesFromRoots(
775+
requestedRoots: Root[],
776+
generation: number,
777+
) {
768778
const validatedRootDirs = await getValidRootDirectories(requestedRoots);
779+
if (generation !== rootsGeneration) {
780+
console.error(
781+
"Discarded a stale roots/list answer: a newer roots update has started",
782+
);
783+
return;
784+
}
769785
if (validatedRootDirs.length > 0) {
770786
allowedDirectories = [...validatedRootDirs];
771787
rootsInForce = true;
@@ -786,12 +802,13 @@ export function createServer(
786802
server.server.setNotificationHandler(
787803
RootsListChangedNotificationSchema,
788804
async () => {
805+
const generation = ++rootsGeneration;
789806
try {
790807
// Request the updated roots list from the client
791808
const response = await server.server.listRoots();
792809
/* v8 ignore else -- the SDK validates the roots/list result against ListRootsResultSchema, which requires roots, so the implicit else cannot run */
793810
if (response && "roots" in response) {
794-
await updateAllowedDirectoriesFromRoots(response.roots);
811+
await updateAllowedDirectoriesFromRoots(response.roots, generation);
795812
}
796813
} catch (error) {
797814
console.error(
@@ -812,11 +829,12 @@ export function createServer(
812829
const clientCapabilities = server.server.getClientCapabilities();
813830

814831
if (clientCapabilities?.roots) {
832+
const generation = ++rootsGeneration;
815833
try {
816834
const response = await server.server.listRoots();
817835
/* v8 ignore else -- the SDK validates the roots/list result against ListRootsResultSchema, which requires roots, so the else cannot run */
818836
if (response && "roots" in response) {
819-
await updateAllowedDirectoriesFromRoots(response.roots);
837+
await updateAllowedDirectoriesFromRoots(response.roots, generation);
820838
} else {
821839
console.error(
822840
"Client returned no roots set, keeping current settings",

0 commit comments

Comments
 (0)