Skip to content

Commit 423fe6b

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 #5097 Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
1 parent de49bdd commit 423fe6b

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
@@ -174,6 +174,12 @@ export function createServer(
174174
// withdraws its roots withdraws the server's access too (#5094). Until then an
175175
// update with no valid roots keeps the command-line directories.
176176
let rootsInForce = false;
177+
// Each roots refresh (the initial load and every roots/list_changed) takes
178+
// the next generation before it asks for roots. The SDK runs notification
179+
// handlers without waiting for earlier ones, so answers can come back out of
180+
// order; one is applied only if no newer refresh has started since, so a
181+
// stale answer cannot undo a newer update, such as a revocation (#5097).
182+
let rootsGeneration = 0;
177183

178184
const server = new McpServer({
179185
name: "secure-filesystem-server",
@@ -760,9 +766,19 @@ export function createServer(
760766
},
761767
);
762768

763-
// Updates allowed directories based on MCP client roots
764-
async function updateAllowedDirectoriesFromRoots(requestedRoots: Root[]) {
769+
// Updates allowed directories based on MCP client roots, unless a newer
770+
// refresh started after this one's `generation` was taken (#5097).
771+
async function updateAllowedDirectoriesFromRoots(
772+
requestedRoots: Root[],
773+
generation: number,
774+
) {
765775
const validatedRootDirs = await getValidRootDirectories(requestedRoots);
776+
if (generation !== rootsGeneration) {
777+
console.error(
778+
"Discarded a stale roots/list answer: a newer roots update has started",
779+
);
780+
return;
781+
}
766782
if (validatedRootDirs.length > 0) {
767783
allowedDirectories = [...validatedRootDirs];
768784
rootsInForce = true;
@@ -783,12 +799,13 @@ export function createServer(
783799
server.server.setNotificationHandler(
784800
"notifications/roots/list_changed",
785801
async () => {
802+
const generation = ++rootsGeneration;
786803
try {
787804
// Request the updated roots list from the client
788805
const response = await server.server.listRoots();
789806
/* v8 ignore else -- the SDK validates the roots/list result against ListRootsResultSchema, which requires roots, so the implicit else cannot run */
790807
if (response && "roots" in response) {
791-
await updateAllowedDirectoriesFromRoots(response.roots);
808+
await updateAllowedDirectoriesFromRoots(response.roots, generation);
792809
}
793810
} catch (error) {
794811
console.error(
@@ -809,11 +826,12 @@ export function createServer(
809826
const clientCapabilities = server.server.getClientCapabilities();
810827

811828
if (clientCapabilities?.roots) {
829+
const generation = ++rootsGeneration;
812830
try {
813831
const response = await server.server.listRoots();
814832
/* v8 ignore else -- the SDK validates the roots/list result against ListRootsResultSchema, which requires roots, so the else cannot run */
815833
if (response && "roots" in response) {
816-
await updateAllowedDirectoriesFromRoots(response.roots);
834+
await updateAllowedDirectoriesFromRoots(response.roots, generation);
817835
} else {
818836
console.error(
819837
"Client returned no roots set, keeping current settings",

0 commit comments

Comments
 (0)