@@ -66,6 +66,7 @@ async function writeListToolsMcpServer(params: {
6666 filePath : string ;
6767 logPath : string ;
6868 delayMs ?: number ;
69+ listToolsReleasePath ?: string ;
6970 initializeDelayMs ?: number ;
7071 hang ?: boolean ;
7172 inputSchema ?: unknown ;
@@ -105,6 +106,7 @@ import fs from "node:fs/promises";
105106
106107const logPath = ${ JSON . stringify ( params . logPath ) } ;
107108const delayMs = ${ params . delayMs ?? 0 } ;
109+ const listToolsReleasePath = ${ JSON . stringify ( params . listToolsReleasePath ) } ;
108110const initializeDelayMs = ${ params . initializeDelayMs ?? 0 } ;
109111const hang = ${ params . hang === true } ;
110112const capabilities = ${ JSON . stringify ( params . capabilities ?? { tools : { } } ) } ;
@@ -233,7 +235,7 @@ function handle(message) {
233235 const currentListCount = listCount;
234236 const toolPageCursor = toolPageCursors?.[currentListCount - 1];
235237 log("delay tools/list " + delayMs);
236- pendingTimer = setTimeout( () => {
238+ const sendListResponse = () => {
237239 send({
238240 jsonrpc: "2.0",
239241 id: message.id,
@@ -250,7 +252,20 @@ function handle(message) {
250252 log("notify tools/list_changed");
251253 send({ jsonrpc: "2.0", method: "notifications/tools/list_changed" });
252254 }
253- }, delayMs);
255+ };
256+ void (async () => {
257+ while (listToolsReleasePath) {
258+ const released = await fs
259+ .access(listToolsReleasePath)
260+ .then(() => true)
261+ .catch(() => false);
262+ if (released) {
263+ break;
264+ }
265+ await new Promise((resolve) => setTimeout(resolve, 10));
266+ }
267+ pendingTimer = setTimeout(sendListResponse, delayMs);
268+ })();
254269 }
255270 if (message.method === "tools/call") {
256271 if (hangToolCallsUntilRestart) {
@@ -5308,20 +5323,24 @@ process.stdin.on("end", () => {
53085323 ) ;
53095324
53105325 it (
5311- "parallelizes MCP server catalog loading across multiple slow servers " ,
5326+ "starts MCP server catalog loading concurrently " ,
53125327 { timeout : LIST_TOOLS_TEST_DEADLINE_MS } ,
53135328 async ( ) => {
53145329 const tempDir = makeTempDir ( tempDirs , "bundle-mcp-parallel-" ) ;
5315- const delays = [ 200 , 400 , 600 ] ;
5316- const serverPaths = delays . map ( ( delay , i ) => {
5330+ const releasePath = path . join ( tempDir , "release-list-tools" ) ;
5331+ const serverPaths = Array . from ( { length : 3 } , ( _ , i ) => {
53175332 const serverPath = path . join ( tempDir , `slow-server-${ i } .mjs` ) ;
53185333 const logPath = path . join ( tempDir , `server-${ i } .log` ) ;
5319- return { serverPath, logPath, delay , serverName : `slowServer${ i } ` } ;
5334+ return { serverPath, logPath, serverName : `slowServer${ i } ` } ;
53205335 } ) ;
53215336
53225337 await Promise . all (
5323- serverPaths . map ( ( { serverPath, logPath, delay } ) =>
5324- writeListToolsMcpServer ( { filePath : serverPath , logPath, delayMs : delay } ) ,
5338+ serverPaths . map ( ( { serverPath, logPath } ) =>
5339+ writeListToolsMcpServer ( {
5340+ filePath : serverPath ,
5341+ logPath,
5342+ listToolsReleasePath : releasePath ,
5343+ } ) ,
53255344 ) ,
53265345 ) ;
53275346
@@ -5347,30 +5366,25 @@ process.stdin.on("end", () => {
53475366 } ,
53485367 } ) ;
53495368
5369+ const catalogPromise = runtime . getCatalog ( ) ;
53505370 try {
5351- const sumDelays = delays . reduce ( ( a , b ) => a + b , 0 ) ;
5352- const maxDelay = Math . max ( ... delays ) ;
5353- const parallelBudgetMs = maxDelay + 500 ;
5354-
5355- const t0 = performance . now ( ) ;
5356- const catalog = await runtime . getCatalog ( ) ;
5357- const wallTime = performance . now ( ) - t0 ;
5371+ await Promise . all (
5372+ serverPaths . map ( ( { logPath } ) =>
5373+ waitForFileText ( logPath , "tools/list cursor" , LIST_TOOLS_SERVER_LOG_TIMEOUT_MS ) ,
5374+ ) ,
5375+ ) ;
5376+ await fs . writeFile ( releasePath , "released" , "utf8" ) ;
5377+ const catalog = await catalogPromise ;
53585378
5359- // Must have successfully connected to all servers
5360- expect ( Object . keys ( catalog . servers ) ) . toHaveLength ( delays . length ) ;
5379+ expect ( Object . keys ( catalog . servers ) ) . toHaveLength ( serverPaths . length ) ;
53615380 expect ( catalog . tools . map ( ( t ) => t . toolName ) ) . toEqual ( [
53625381 "slow_tool" ,
53635382 "slow_tool" ,
53645383 "slow_tool" ,
53655384 ] ) ;
5366-
5367- // Sequential listing would have to wait roughly sumDelays before overhead;
5368- // parallel listing should stay near the slowest server plus launch overhead.
5369- expect ( wallTime ) . toBeLessThan ( parallelBudgetMs ) ;
5370- expect ( parallelBudgetMs ) . toBeLessThan ( sumDelays ) ;
5371-
5372- expect ( wallTime ) . toBeGreaterThanOrEqual ( maxDelay * 0.7 ) ;
53735385 } finally {
5386+ await fs . writeFile ( releasePath , "released" , "utf8" ) . catch ( ( ) => { } ) ;
5387+ await catalogPromise . catch ( ( ) => { } ) ;
53745388 await runtime . dispose ( ) ;
53755389 }
53765390 } ,
0 commit comments