|
| 1 | +import type { McpServerConfig } from "@anthropic-ai/claude-agent-sdk"; |
| 2 | +import { Client } from "@modelcontextprotocol/sdk/client/index.js"; |
| 3 | +import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js"; |
| 4 | +import type { Tool } from "@modelcontextprotocol/sdk/types.js"; |
| 5 | +import { Logger } from "../../../utils/logger.js"; |
| 6 | + |
| 7 | +export interface McpToolMetadata { |
| 8 | + readOnly: boolean; |
| 9 | +} |
| 10 | + |
| 11 | +const mcpToolMetadataCache: Map<string, McpToolMetadata> = new Map(); |
| 12 | + |
| 13 | +function buildToolKey(serverName: string, toolName: string): string { |
| 14 | + return `mcp__${serverName}__${toolName}`; |
| 15 | +} |
| 16 | + |
| 17 | +function isHttpMcpServer( |
| 18 | + config: McpServerConfig, |
| 19 | +): config is McpServerConfig & { type: "http"; url: string } { |
| 20 | + return config.type === "http" && typeof (config as any).url === "string"; |
| 21 | +} |
| 22 | + |
| 23 | +async function fetchToolsFromHttpServer( |
| 24 | + _serverName: string, |
| 25 | + config: McpServerConfig & { type: "http"; url: string }, |
| 26 | +): Promise<Tool[]> { |
| 27 | + const transport = new StreamableHTTPClientTransport(new URL(config.url), { |
| 28 | + requestInit: { |
| 29 | + headers: (config as any).headers || {}, |
| 30 | + }, |
| 31 | + }); |
| 32 | + |
| 33 | + const client = new Client({ |
| 34 | + name: "twig-metadata-fetcher", |
| 35 | + version: "1.0.0", |
| 36 | + }); |
| 37 | + |
| 38 | + try { |
| 39 | + await client.connect(transport); |
| 40 | + const result = await client.listTools(); |
| 41 | + return result.tools; |
| 42 | + } finally { |
| 43 | + await client.close().catch(() => {}); |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +function extractToolMetadata(tool: Tool): McpToolMetadata { |
| 48 | + return { |
| 49 | + readOnly: tool.annotations?.readOnlyHint === true, |
| 50 | + }; |
| 51 | +} |
| 52 | + |
| 53 | +export async function fetchMcpToolMetadata( |
| 54 | + mcpServers: Record<string, McpServerConfig>, |
| 55 | + logger: Logger = new Logger({ debug: false, prefix: "[McpToolMetadata]" }), |
| 56 | +): Promise<void> { |
| 57 | + const fetchPromises: Promise<void>[] = []; |
| 58 | + |
| 59 | + for (const [serverName, config] of Object.entries(mcpServers)) { |
| 60 | + if (!isHttpMcpServer(config)) { |
| 61 | + continue; |
| 62 | + } |
| 63 | + |
| 64 | + const fetchPromise = fetchToolsFromHttpServer(serverName, config) |
| 65 | + .then((tools) => { |
| 66 | + const toolCount = tools.length; |
| 67 | + const readOnlyCount = tools.filter( |
| 68 | + (t) => t.annotations?.readOnlyHint === true, |
| 69 | + ).length; |
| 70 | + |
| 71 | + for (const tool of tools) { |
| 72 | + const toolKey = buildToolKey(serverName, tool.name); |
| 73 | + mcpToolMetadataCache.set(toolKey, extractToolMetadata(tool)); |
| 74 | + } |
| 75 | + |
| 76 | + logger.info("Fetched MCP tool metadata", { |
| 77 | + serverName, |
| 78 | + toolCount, |
| 79 | + readOnlyCount, |
| 80 | + }); |
| 81 | + }) |
| 82 | + .catch((error) => { |
| 83 | + logger.error("Failed to fetch MCP tool metadata", { |
| 84 | + serverName, |
| 85 | + error: error instanceof Error ? error.message : String(error), |
| 86 | + }); |
| 87 | + }); |
| 88 | + |
| 89 | + fetchPromises.push(fetchPromise); |
| 90 | + } |
| 91 | + |
| 92 | + await Promise.all(fetchPromises); |
| 93 | +} |
| 94 | + |
| 95 | +export function isMcpToolReadOnly(toolName: string): boolean { |
| 96 | + const metadata = mcpToolMetadataCache.get(toolName); |
| 97 | + return metadata?.readOnly === true; |
| 98 | +} |
| 99 | + |
| 100 | +export function clearMcpToolMetadataCache(): void { |
| 101 | + mcpToolMetadataCache.clear(); |
| 102 | +} |
0 commit comments