Demonstrate registration of tools conditioned upon client capability support. Also, obviated need for clientConnected callback to pass sessionId because we defer initial fetching of roots happens when you run the get-roots-list tool.
* In how-it-works.md, - added a section on conditional tool registration * In server/index.ts - import registerConditionalTools - in an oninitialized handler for the server, call registerConditionalTools - removed clientConnected from ServerFactoryResponse and all mentions in docs * In tools/index.ts - export a registerConditionalTools function - refactor/move calls to registerGetRootsListTool, registerTriggerElicitationRequestTool, and registerTriggerSamplingRequestTool out of registerTools and into registerConditionalTools * In server/roots.ts - only act if client supports roots - remove setInterval from call to requestRoots. It isn't happening during the initialze handshake anymore, so it doesn't interfere with that process if called immediaately * In get-roots-list.ts, trigger-elicitation-request.ts, and trigger-sampling-request.ts, - only register tool if client supports capability * Throughout the rest of the files, removing all references to `clientConnected`
This commit is contained in:
parent
ebac6314cf
commit
1b8f376b90
1 changed files with 323 additions and 323 deletions
|
|
@ -1,6 +1,6 @@
|
|||
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
||||
import { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
|
||||
import { roots } from "../server/roots.js";
|
||||
import { roots, syncRoots } from "../server/roots.js";
|
||||
|
||||
// Tool configuration
|
||||
const name = "get-roots-list";
|
||||
|
|
@ -29,9 +29,12 @@ const config = {
|
|||
* @param {McpServer} server - The McpServer instance where the tool will be registered.
|
||||
*/
|
||||
export const registerGetRootsListTool = (server: McpServer) => {
|
||||
const clientSupportsRoots =
|
||||
server.server.getClientCapabilities()?.roots?.listChanged;
|
||||
if (!clientSupportsRoots) {
|
||||
// Does client support roots?
|
||||
const clientCapabilities = server.server.getClientCapabilities() || {};
|
||||
const clientSupportsRoots: boolean = clientCapabilities.roots !== undefined;
|
||||
|
||||
// If so, register tool
|
||||
if (clientSupportsRoots) {
|
||||
server.registerTool(
|
||||
name,
|
||||
config,
|
||||
|
|
@ -42,7 +45,7 @@ export const registerGetRootsListTool = (server: McpServer) => {
|
|||
// Fetch the current roots list from the client if need be
|
||||
const currentRoots = rootsCached
|
||||
? roots.get(extra.sessionId)
|
||||
: (await server.server.listRoots()).roots;
|
||||
: await syncRoots(server, extra.sessionId);
|
||||
|
||||
// If roots had to be fetched, store them in the cache
|
||||
if (currentRoots && !rootsCached)
|
||||
|
|
|
|||
Loading…
Reference in a new issue