Sync roots at startup. This was deferred to list_roots tool is used, but I'm putting it back.

The syncRoots call should be idempotent, requesting roots if they haven't been yet for the session, but always retuning the cached roots otherwise. That could be deferred but setting the handler for roots_list changed note should not.

* In server/roots.ts
  - only set the notification handler and call for initial roots list if the roots aren't already cached for this client.

* In server/index.ts
  - in the oninitialized handler
    - get the sessionId from the transport
    - set a 350ms timeout to call syncRoots with the server and sessionId
      - this delay cause it to run after the `notifications/initialized` handler finishes, otherwise, the request gets lost.

* All other changes attributable to prettier
This commit is contained in:
cliffhall
2025-12-17 17:44:17 -05:00
parent 17a2be2320
commit f8933ec3e6
5 changed files with 44 additions and 24 deletions

View File

@@ -40,7 +40,7 @@ export const registerGetRootsListTool = (server: McpServer) => {
config,
async (args, extra): Promise<CallToolResult> => {
// Get the current rootsFetch the current roots list from the client if need be
const currentRoots = await syncRoots(server, extra.sessionId);
const currentRoots = await syncRoots(server, extra.sessionId);
// Respond if client supports roots but doesn't have any configured
if (

View File

@@ -39,7 +39,6 @@ export const registerTools = (server: McpServer) => {
* These must be registered conditionally, after initialization.
*/
export const registerConditionalTools = (server: McpServer) => {
console.log("Registering conditional tools...");
registerGetRootsListTool(server);
registerTriggerElicitationRequestTool(server);
registerTriggerSamplingRequestTool(server);

View File

@@ -131,9 +131,9 @@ export const registerTriggerElicitationRequestTool = (server: McpServer) => {
title: "Titled Single Select Enum",
description: "Choose your favorite hero",
oneOf: [
{const: "hero-1", title: "Superman"},
{const: "hero-2", title: "Green Lantern"},
{const: "hero-3", title: "Wonder Woman"},
{ const: "hero-1", title: "Superman" },
{ const: "hero-2", title: "Green Lantern" },
{ const: "hero-3", title: "Wonder Woman" },
],
default: "hero-1",
},
@@ -145,9 +145,9 @@ export const registerTriggerElicitationRequestTool = (server: McpServer) => {
maxItems: 3,
items: {
anyOf: [
{const: "fish-1", title: "Tuna"},
{const: "fish-2", title: "Salmon"},
{const: "fish-3", title: "Trout"},
{ const: "fish-1", title: "Tuna" },
{ const: "fish-2", title: "Salmon" },
{ const: "fish-3", title: "Trout" },
],
},
default: ["fish-1"],
@@ -166,7 +166,7 @@ export const registerTriggerElicitationRequestTool = (server: McpServer) => {
},
},
ElicitResultSchema,
{timeout: 10 * 60 * 1000 /* 10 minutes */}
{ timeout: 10 * 60 * 1000 /* 10 minutes */ }
);
// Handle different response actions
@@ -220,7 +220,7 @@ export const registerTriggerElicitationRequestTool = (server: McpServer) => {
text: `\nRaw result: ${JSON.stringify(elicitationResult, null, 2)}`,
});
return {content};
return { content };
}
);
}