import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js' import { type RegisteredTool } from '../tools/registry.ts' import { toZod } from './api/to-zod.ts' import { remapErrorAsResponse } from './remap_errors.ts' // The tool primitive lives in `../tools/registry.ts` (SDK-free, so the cloud // loop can use it too). Re-exported here so the MCP-side tool files can keep // importing `defineTool`/`RegisteredTool` from the server module. export { defineTool, type RegisteredTool, type ToolDefinition, } from '../tools/registry.ts' /** * Registers a list of RegisteredTool entries on a high-level McpServer. * Converts JSON Schema definitions to Zod and provides consistent * stringification and error catching for tool execution. */ export function registerToolList(server: McpServer, tools: RegisteredTool[]) { for(const tool of tools) { server.registerTool( tool.definition.name, { description: tool.definition.description, inputSchema: toZod(tool.definition.inputSchema), }, async(args) => { try { return { content: [ { type: 'text' as const, text: JSON.stringify(await tool.handler(args), null, 2), }, ], } } catch(err) { return remapErrorAsResponse(err) } }, ) } }