@@ -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" ,
@@ -187,14 +193,24 @@ export function createServer(
187193 // command-line directories (#3204). It is replaced in oninitialized, which
188194 // the SDK runs before any request sent after notifications/initialized.
189195 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 ;
190202 const registerTool = server . registerTool . bind ( server ) ;
191203 server . registerTool = ( (
192204 name : string ,
193205 config : Parameters < typeof registerTool > [ 1 ] ,
194206 handler : ( ...args : unknown [ ] ) => unknown ,
195207 ) =>
196208 registerTool ( name , config , ( async ( ...args : unknown [ ] ) => {
197- await initialization ;
209+ let awaited : Promise < void > ;
210+ do {
211+ awaited = initialization ;
212+ await awaited ;
213+ } while ( awaited !== initialization ) ;
198214 return handler ( ...args ) ;
199215 } ) as never ) ) as typeof server . registerTool ;
200216
@@ -760,9 +776,19 @@ export function createServer(
760776 } ,
761777 ) ;
762778
763- // Updates allowed directories based on MCP client roots
764- async function updateAllowedDirectoriesFromRoots ( requestedRoots : Root [ ] ) {
779+ // Updates allowed directories based on MCP client roots, unless a newer
780+ // refresh started after this one's `generation` was taken (#5097).
781+ async function updateAllowedDirectoriesFromRoots (
782+ requestedRoots : Root [ ] ,
783+ generation : number ,
784+ ) {
765785 const validatedRootDirs = await getValidRootDirectories ( requestedRoots ) ;
786+ if ( generation !== rootsGeneration ) {
787+ console . error (
788+ "Discarded a stale roots/list answer: a newer roots update has started" ,
789+ ) ;
790+ return ;
791+ }
766792 if ( validatedRootDirs . length > 0 ) {
767793 allowedDirectories = [ ...validatedRootDirs ] ;
768794 rootsInForce = true ;
@@ -783,19 +809,27 @@ export function createServer(
783809 server . server . setNotificationHandler (
784810 "notifications/roots/list_changed" ,
785811 async ( ) => {
786- try {
787- // Request the updated roots list from the client
788- const response = await server . server . listRoots ( ) ;
789- /* v8 ignore else -- the SDK validates the roots/list result against ListRootsResultSchema, which requires roots, so the implicit else cannot run */
790- if ( response && "roots" in response ) {
791- await updateAllowedDirectoriesFromRoots ( response . roots ) ;
812+ const generation = ++ rootsGeneration ;
813+ const refresh = ( async ( ) => {
814+ try {
815+ // Request the updated roots list from the client
816+ const response = await server . server . listRoots ( ) ;
817+ /* v8 ignore else -- the SDK validates the roots/list result against ListRootsResultSchema, which requires roots, so the implicit else cannot run */
818+ if ( response && "roots" in response ) {
819+ await updateAllowedDirectoriesFromRoots ( response . roots , generation ) ;
820+ }
821+ } catch ( error ) {
822+ console . error (
823+ "Failed to request roots from client:" ,
824+ error instanceof Error ? error . message : String ( error ) ,
825+ ) ;
826+ } finally {
827+ if ( generation === rootsGeneration ) initialRootsPending = false ;
792828 }
793- } catch ( error ) {
794- console . error (
795- "Failed to request roots from client:" ,
796- error instanceof Error ? error . message : String ( error ) ,
797- ) ;
798- }
829+ } ) ( ) ;
830+ // Overtaking the initial load: tool calls wait for this refresh instead.
831+ if ( initialRootsPending ) initialization = refresh ;
832+ await refresh ;
799833 } ,
800834 ) ;
801835
@@ -809,11 +843,13 @@ export function createServer(
809843 const clientCapabilities = server . server . getClientCapabilities ( ) ;
810844
811845 if ( clientCapabilities ?. roots ) {
846+ const generation = ++ rootsGeneration ;
847+ initialRootsPending = true ;
812848 try {
813849 const response = await server . server . listRoots ( ) ;
814850 /* v8 ignore else -- the SDK validates the roots/list result against ListRootsResultSchema, which requires roots, so the else cannot run */
815851 if ( response && "roots" in response ) {
816- await updateAllowedDirectoriesFromRoots ( response . roots ) ;
852+ await updateAllowedDirectoriesFromRoots ( response . roots , generation ) ;
817853 } else {
818854 console . error (
819855 "Client returned no roots set, keeping current settings" ,
@@ -824,6 +860,8 @@ export function createServer(
824860 "Failed to request initial roots from client:" ,
825861 error instanceof Error ? error . message : String ( error ) ,
826862 ) ;
863+ } finally {
864+ if ( generation === rootsGeneration ) initialRootsPending = false ;
827865 }
828866 } else {
829867 if ( allowedDirectories . length > 0 ) {
0 commit comments