| 344 | |
| 345 | function registerProxiedTool<T extends ExtensionTool>(mcp: McpServer, tool: T): void { |
| 346 | type Name = T['name'] |
| 347 | type Result = ToolResultMap[Name] |
| 348 | |
| 349 | const registerToolFn = mcp.registerTool.bind(mcp) as ( |
| 350 | name: string, |
| 351 | options: { description: string; inputSchema: ZodType; outputSchema?: ZodType }, |
| 352 | handler: (args: unknown) => Promise<CallToolResult> |
| 353 | ) => unknown |
| 354 | |
| 355 | const schema = tool.parameters |
| 356 | const handler = async (args: unknown) => { |
| 357 | let requestId: string | undefined |
| 358 | try { |
| 359 | const parsedArgs = schema.parse(args) |
| 360 | const activeExt = extensionRegistry.getActive() |
| 361 | if (!activeExt) { |
| 362 | throw createCodedError( |
| 363 | TEMPAD_MCP_ERROR_CODES.NO_ACTIVE_EXTENSION, |
| 364 | 'No active TemPad Dev extension available.' |
| 365 | ) |
| 366 | } |
| 367 | |
| 368 | const registration = register<Result>(activeExt.id, toolTimeoutMs) |
| 369 | requestId = registration.requestId |
| 370 | |
| 371 | const message: ToolCallMessage = { |
| 372 | type: 'toolCall', |
| 373 | id: registration.requestId, |
| 374 | payload: { |
| 375 | name: tool.name, |
| 376 | args: parsedArgs |
| 377 | } |
| 378 | } |
| 379 | activeExt.ws.send(JSON.stringify(message)) |
| 380 | log.info( |
| 381 | { tool: tool.name, req: registration.requestId, extId: activeExt.id }, |
| 382 | 'Forwarded tool call.' |
| 383 | ) |
| 384 | |
| 385 | const payload = await registration.promise |
| 386 | return createToolResponse(tool.name, payload) |
| 387 | } catch (error) { |
| 388 | const normalized = coerceToolError(error) |
| 389 | log.error( |
| 390 | { |
| 391 | tool: tool.name, |
| 392 | req: requestId, |
| 393 | code: getRecordProperty(normalized, 'code'), |