()
| 122 | private _toolHandlersInitialized = false; |
| 123 | |
| 124 | private setToolRequestHandlers() { |
| 125 | if (this._toolHandlersInitialized) { |
| 126 | return; |
| 127 | } |
| 128 | |
| 129 | this.server.assertCanSetRequestHandler(getMethodValue(ListToolsRequestSchema)); |
| 130 | this.server.assertCanSetRequestHandler(getMethodValue(CallToolRequestSchema)); |
| 131 | |
| 132 | this.server.registerCapabilities({ |
| 133 | tools: { |
| 134 | listChanged: true |
| 135 | } |
| 136 | }); |
| 137 | |
| 138 | this.server.setRequestHandler( |
| 139 | ListToolsRequestSchema, |
| 140 | (): ListToolsResult => ({ |
| 141 | tools: Object.entries(this._registeredTools) |
| 142 | .filter(([, tool]) => tool.enabled) |
| 143 | .map(([name, tool]): Tool => { |
| 144 | const toolDefinition: Tool = { |
| 145 | name, |
| 146 | title: tool.title, |
| 147 | description: tool.description, |
| 148 | inputSchema: (() => { |
| 149 | const obj = normalizeObjectSchema(tool.inputSchema); |
| 150 | return obj |
| 151 | ? (toJsonSchemaCompat(obj, { |
| 152 | strictUnions: true, |
| 153 | pipeStrategy: 'input' |
| 154 | }) as Tool['inputSchema']) |
| 155 | : EMPTY_OBJECT_JSON_SCHEMA; |
| 156 | })(), |
| 157 | annotations: tool.annotations, |
| 158 | execution: tool.execution, |
| 159 | _meta: tool._meta |
| 160 | }; |
| 161 | |
| 162 | if (tool.outputSchema) { |
| 163 | const obj = normalizeObjectSchema(tool.outputSchema); |
| 164 | if (obj) { |
| 165 | toolDefinition.outputSchema = toJsonSchemaCompat(obj, { |
| 166 | strictUnions: true, |
| 167 | pipeStrategy: 'output' |
| 168 | }) as Tool['outputSchema']; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | return toolDefinition; |
| 173 | }) |
| 174 | }) |
| 175 | ); |
| 176 | |
| 177 | this.server.setRequestHandler(CallToolRequestSchema, async (request, extra): Promise<CallToolResult | CreateTaskResult> => { |
| 178 | try { |
| 179 | const tool = this._registeredTools[request.params.name]; |
| 180 | if (!tool) { |
| 181 | throw new McpError(ErrorCode.InvalidParams, `Tool ${request.params.name} not found`); |
no test coverage detected