( toolName: string, toolModule: ImportedToolModule, args: unknown, )
| 248 | } |
| 249 | |
| 250 | async function invokeRegisteredTool( |
| 251 | toolName: string, |
| 252 | toolModule: ImportedToolModule, |
| 253 | args: unknown, |
| 254 | ): Promise<ToolResponse> { |
| 255 | const startedAt = Date.now(); |
| 256 | |
| 257 | try { |
| 258 | const session = createRenderSession('text', { outputStyle: 'minimal' }); |
| 259 | const ctx: ToolHandlerContext = { |
| 260 | emit: (fragment) => { |
| 261 | session.emit(fragment); |
| 262 | }, |
| 263 | attach: session.attach, |
| 264 | }; |
| 265 | await toolModule.handler(args as Record<string, unknown>, ctx); |
| 266 | |
| 267 | if (ctx.structuredOutput) { |
| 268 | session.setStructuredOutput?.(ctx.structuredOutput); |
| 269 | } |
| 270 | |
| 271 | const catalog = registryState.catalog; |
| 272 | const catalogTool = catalog?.getByMcpName(toolName); |
| 273 | if (catalog && catalogTool) { |
| 274 | postProcessSession({ |
| 275 | tool: catalogTool, |
| 276 | session, |
| 277 | ctx, |
| 278 | catalog, |
| 279 | runtime: 'mcp', |
| 280 | }); |
| 281 | } |
| 282 | |
| 283 | const response = sessionToToolResponse(session); |
| 284 | recordMcpInvocation(toolName, startedAt, 'completed'); |
| 285 | return response; |
| 286 | } catch (error) { |
| 287 | recordInternalErrorMetric({ |
| 288 | component: 'mcp-tool-registry', |
| 289 | runtime: 'mcp', |
| 290 | errorKind: error instanceof Error ? error.name || 'Error' : typeof error, |
| 291 | }); |
| 292 | recordMcpInvocation(toolName, startedAt, 'infra_error'); |
| 293 | throw error; |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | function registerToolFromManifest( |
| 298 | toolManifest: ToolManifestEntry, |
no test coverage detected