( options: CallToolOptions, deps: McpClientDependencies, )
| 54 | * - Error wrapping for telemetry |
| 55 | */ |
| 56 | export async function callMcpTool( |
| 57 | options: CallToolOptions, |
| 58 | deps: McpClientDependencies, |
| 59 | ): Promise<CallToolResult> { |
| 60 | const { client, tool, args, meta, signal, onProgress, timeoutMs } = options |
| 61 | const { name: serverName, client: mcpClient } = client |
| 62 | const effectiveTimeout = timeoutMs ?? getMcpToolTimeoutMs() |
| 63 | |
| 64 | let progressInterval: ReturnType<typeof setInterval> | undefined |
| 65 | |
| 66 | try { |
| 67 | deps.logger.debug(`[${serverName}] Calling MCP tool: ${tool}`) |
| 68 | |
| 69 | // Progress logging for long-running tools (every 30 seconds) |
| 70 | progressInterval = setInterval(() => { |
| 71 | deps.logger.debug(`[${serverName}] Tool '${tool}' still running`) |
| 72 | }, 30_000) |
| 73 | |
| 74 | const result = await Promise.race([ |
| 75 | mcpClient.callTool( |
| 76 | { |
| 77 | name: tool, |
| 78 | arguments: args, |
| 79 | _meta: meta, |
| 80 | }, |
| 81 | CallToolResultSchema, |
| 82 | { |
| 83 | signal, |
| 84 | timeout: effectiveTimeout, |
| 85 | onprogress: onProgress, |
| 86 | }, |
| 87 | ), |
| 88 | createTimeoutPromise(serverName, tool, effectiveTimeout), |
| 89 | ]) |
| 90 | |
| 91 | // Handle isError in result |
| 92 | if ('isError' in result && result.isError) { |
| 93 | let errorDetails = 'Unknown error' |
| 94 | if ( |
| 95 | 'content' in result && |
| 96 | Array.isArray(result.content) && |
| 97 | result.content.length > 0 |
| 98 | ) { |
| 99 | const firstContent = result.content[0] |
| 100 | if ( |
| 101 | firstContent && |
| 102 | typeof firstContent === 'object' && |
| 103 | 'text' in firstContent |
| 104 | ) { |
| 105 | errorDetails = (firstContent as { text: string }).text |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | throw new McpToolCallError(serverName, tool, errorDetails) |
| 110 | } |
| 111 | |
| 112 | return { |
| 113 | content: result, |
no test coverage detected