Skip to content

Commit 92f9558

Browse files
cliffhallclaude
andcommitted
fix(filesystem): hold tool calls while any roots refresh is pending
A runtime roots/list_changed refresh now replaces the initialization barrier, as the initial load and an overtaking refresh already did, so tool calls that arrive while its roots/list is pending wait for the result instead of running against the previous allow-list, which could still include a root the client had just withdrawn. The initialRootsPending flag is no longer needed. Closes #5101 Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
1 parent c9fdf34 commit 92f9558

4 files changed

Lines changed: 57 additions & 19 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+
Tool calls that arrive while a `roots/list_changed` refresh is fetching the client's roots now wait for it, as they already did for the initial roots, so a call can no longer reach a root the client has just withdrawn while the answer is pending (#5101).

‎src/filesystem/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ The server's directory access control follows this flow:
5151
- Tool calls that arrive before the initial roots are loaded wait for them, so they are checked against the client's roots
5252
- **On runtime updates**: Client can send `notifications/roots/list_changed`
5353
- Server requests updated roots and replaces allowed directories again
54+
- Tool calls that arrive while an update is being fetched wait for it, so none runs against a root the client has just withdrawn
5455
- Once the client's roots are in force, an update with no valid roots revokes access: every path is refused ("Access denied - no allowed directories") until the client exposes a root again. Before any root has been in force, such an update keeps the command-line directories
5556

5657
4. **Fallback Behavior** (if client doesn't support roots)

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,45 @@ describe("roots/list_changed", () => {
502502
);
503503
});
504504

505+
// #5101: a call made while a runtime refresh is pending waits for it, so it
506+
// cannot reach a root the client has just withdrawn.
507+
it("holds tool calls while a runtime refresh is pending (#5101)", async () => {
508+
let calls = 0;
509+
let releaseUpdate!: () => void;
510+
const updateHeld = new Promise<void>(
511+
(resolve) => (releaseUpdate = resolve),
512+
);
513+
const { client } = await connectTracked([], {
514+
capabilities: ROOTS,
515+
listRoots: async () => {
516+
calls += 1;
517+
if (calls === 1) return [rootOf(rootDir)];
518+
await updateHeld; // the withdrawal is answered slowly
519+
return [];
520+
},
521+
});
522+
await vi.waitFor(async () =>
523+
expect(await allowedDirectoriesOf(client)).toEqual([rootDir]),
524+
);
525+
526+
await client.sendRootsListChanged();
527+
await vi.waitFor(() => expect(calls).toBe(2));
528+
let settled = false;
529+
const pending = call(client, "list_directory", { path: rootDir }).then(
530+
(result) => {
531+
settled = true;
532+
return result;
533+
},
534+
);
535+
await new Promise((resolve) => setTimeout(resolve, 50));
536+
expect(settled).toBe(false);
537+
538+
releaseUpdate();
539+
expect(textOf(await pending)).toMatch(
540+
/^Access denied - no allowed directories/,
541+
);
542+
});
543+
505544
it("logs and keeps the current directories when the re-fetch fails", async () => {
506545
let fail = false;
507546
const { client } = await connectTracked([], {

‎src/filesystem/server.ts‎

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -186,19 +186,16 @@ export function createServer(
186186
version: SERVER_VERSION,
187187
});
188188

189-
// Settles once the post-initialize setup (fetching the client's initial
190-
// roots) has finished, successfully or not; it never rejects. Every tool call
191-
// waits for it, so a call that arrives while the initial roots/list is still
192-
// outstanding is checked against the client's roots rather than against the
193-
// command-line directories (#3204). It is replaced in oninitialized, which
194-
// the SDK runs before any request sent after notifications/initialized.
189+
// Settles once the newest roots refresh has finished, successfully or not; it
190+
// never rejects. Every tool call waits for it, so a call that arrives while
191+
// roots are being fetched is checked against the roots that come back rather
192+
// than the ones being replaced: the command-line directories while the
193+
// initial roots/list is outstanding (#3204), or a root the client has just
194+
// withdrawn while a roots/list_changed refresh is (#5101). The initial load
195+
// (oninitialized, which the SDK runs before any request sent after
196+
// notifications/initialized) and every refresh replace it, and a waiting
197+
// call follows each replacement until it stops changing (#5097).
195198
let initialization: Promise<void> = Promise.resolve();
196-
// True from the initial roots/list until a refresh's answer has been
197-
// applied or the newest refresh has failed. A roots/list_changed that
198-
// overtakes the initial load while this holds replaces `initialization`, so
199-
// waiting tool calls keep waiting for the refresh that will actually apply
200-
// rather than running against the command-line directories (#5097).
201-
let initialRootsPending = false;
202199
const registerTool = server.registerTool.bind(server);
203200
server.registerTool = ((
204201
name: string,
@@ -823,12 +820,11 @@ export function createServer(
823820
"Failed to request roots from client:",
824821
error instanceof Error ? error.message : String(error),
825822
);
826-
} finally {
827-
if (generation === rootsGeneration) initialRootsPending = false;
828823
}
829824
})();
830-
// Overtaking the initial load: tool calls wait for this refresh instead.
831-
if (initialRootsPending) initialization = refresh;
825+
// Tool calls wait for this refresh, so none runs against a root the
826+
// client has just withdrawn while its answer is pending (#5101).
827+
initialization = refresh;
832828
await refresh;
833829
},
834830
);
@@ -844,7 +840,6 @@ export function createServer(
844840

845841
if (clientCapabilities?.roots) {
846842
const generation = ++rootsGeneration;
847-
initialRootsPending = true;
848843
try {
849844
const response = await server.server.listRoots();
850845
/* v8 ignore else -- the SDK validates the roots/list result against ListRootsResultSchema, which requires roots, so the else cannot run */
@@ -860,8 +855,6 @@ export function createServer(
860855
"Failed to request initial roots from client:",
861856
error instanceof Error ? error.message : String(error),
862857
);
863-
} finally {
864-
if (generation === rootsGeneration) initialRootsPending = false;
865858
}
866859
} else {
867860
if (allowedDirectories.length > 0) {

0 commit comments

Comments
 (0)