Use roots cache in get-roots-list tool
* In get-roots-list.ts, query the server's cache of the latest roots from the client and only request the list from the client if it doesn't exist. * In roots.ts, export the roots map
This commit is contained in:
parent
77deef08ce
commit
6854902775
2 changed files with 20 additions and 15 deletions
|
|
@ -5,7 +5,7 @@ import {
|
||||||
} from "@modelcontextprotocol/sdk/types.js";
|
} from "@modelcontextprotocol/sdk/types.js";
|
||||||
|
|
||||||
// Track roots by session id
|
// Track roots by session id
|
||||||
const roots: Map<string | undefined, Root[]> = new Map<
|
export const roots: Map<string | undefined, Root[]> = new Map<
|
||||||
string | undefined,
|
string | undefined,
|
||||||
Root[]
|
Root[]
|
||||||
>();
|
>();
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
||||||
import { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
|
import { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
|
||||||
|
import { roots } from "../server/roots.js"
|
||||||
|
|
||||||
// Tool configuration
|
// Tool configuration
|
||||||
const name = "get-roots-list";
|
const name = "get-roots-list";
|
||||||
|
|
@ -13,18 +14,20 @@ const config = {
|
||||||
/**
|
/**
|
||||||
* Registers the 'get-roots-list' tool with the given MCP server.
|
* Registers the 'get-roots-list' tool with the given MCP server.
|
||||||
*
|
*
|
||||||
* If the client does not support the roots protocol, the tool is not registered.
|
* If the client does not support the roots capability, the tool is not registered.
|
||||||
*
|
*
|
||||||
* The registered tool interacts with the MCP roots protocol, which enables the server to access information about
|
* The registered tool interacts with the MCP roots capability, which enables the server to access
|
||||||
* the client's workspace directories or file system roots. When supported by the client, the server retrieves
|
* information about the client's workspace directories or file system roots.
|
||||||
* and formats the current list of roots for display.
|
|
||||||
*
|
*
|
||||||
* Key behaviors:
|
* When supported by the client, the server automatically retrieves and formats the current list
|
||||||
* - Determines whether the connected MCP client supports the roots protocol by checking client capabilities.
|
* of roots from the client upon connection and whenever the client sends a `roots/list_changed`
|
||||||
* - Fetches and formats the list of roots, including their names and URIs, if supported by the client.
|
* notification.
|
||||||
* - Handles cases where roots are not supported, or no roots are currently provided, with explanatory messages.
|
|
||||||
*
|
*
|
||||||
* @param {McpServer} server - The server instance interacting with the MCP client and managing the roots protocol.
|
* Therefore, this tool displays the roots that the server currently knows about for the connected
|
||||||
|
* client. If for some reason the server never got the initial roots list, the tool will request the
|
||||||
|
* list from the client again.
|
||||||
|
*
|
||||||
|
* @param {McpServer} server - The server instance interacting with the MCP client
|
||||||
*/
|
*/
|
||||||
export const registerGetRootsListTool = (server: McpServer) => {
|
export const registerGetRootsListTool = (server: McpServer) => {
|
||||||
const clientSupportsRoots =
|
const clientSupportsRoots =
|
||||||
|
|
@ -34,8 +37,10 @@ export const registerGetRootsListTool = (server: McpServer) => {
|
||||||
name,
|
name,
|
||||||
config,
|
config,
|
||||||
async (args, extra): Promise<CallToolResult> => {
|
async (args, extra): Promise<CallToolResult> => {
|
||||||
const currentRoots = (await server.server.listRoots()).roots;
|
const currentRoots = roots?.has(extra.sessionId)
|
||||||
if (currentRoots.length === 0) {
|
? roots.get(extra.sessionId)
|
||||||
|
: (await server.server.listRoots()).roots
|
||||||
|
if (currentRoots && currentRoots.length === 0) {
|
||||||
return {
|
return {
|
||||||
content: [
|
content: [
|
||||||
{
|
{
|
||||||
|
|
@ -51,20 +56,20 @@ export const registerGetRootsListTool = (server: McpServer) => {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
const rootsList = currentRoots
|
const rootsList = currentRoots ? currentRoots
|
||||||
.map((root, index) => {
|
.map((root, index) => {
|
||||||
return `${index + 1}. ${root.name || "Unnamed Root"}\n URI: ${
|
return `${index + 1}. ${root.name || "Unnamed Root"}\n URI: ${
|
||||||
root.uri
|
root.uri
|
||||||
}`;
|
}`;
|
||||||
})
|
})
|
||||||
.join("\n\n");
|
.join("\n\n") : 'No roots found'
|
||||||
|
|
||||||
return {
|
return {
|
||||||
content: [
|
content: [
|
||||||
{
|
{
|
||||||
type: "text",
|
type: "text",
|
||||||
text:
|
text:
|
||||||
`Current MCP Roots (${currentRoots.length} total):\n\n${rootsList}\n\n` +
|
`Current MCP Roots (${currentRoots!.length} total):\n\n${rootsList}\n\n` +
|
||||||
"Note: This server demonstrates the roots protocol capability but doesn't actually access files. " +
|
"Note: This server demonstrates the roots protocol capability but doesn't actually access files. " +
|
||||||
"The roots are provided by the MCP client and can be used by servers that need file system access.",
|
"The roots are provided by the MCP client and can be used by servers that need file system access.",
|
||||||
},
|
},
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue