MCPcopy Create free account
hub / github.com/Noumena-Network/code / callMCPTool

Function callMCPTool

src/services/mcp/client.ts:3032–3248  ·  view source on GitHub ↗
({
  client: { client, name, config },
  tool,
  args,
  meta,
  signal,
  onProgress,
}: {
  client: ConnectedMCPServer
  tool: string
  args: Record<string, unknown>
  meta?: Record<string, unknown>
  signal: AbortSignal
  onProgress?: (data: MCPProgress) => void
})

Source from the content-addressed store, hash-verified

3030}
3031
3032async function callMCPTool({
3033 client: { client, name, config },
3034 tool,
3035 args,
3036 meta,
3037 signal,
3038 onProgress,
3039}: {
3040 client: ConnectedMCPServer
3041 tool: string
3042 args: Record<string, unknown>
3043 meta?: Record<string, unknown>
3044 signal: AbortSignal
3045 onProgress?: (data: MCPProgress) => void
3046}): Promise<{
3047 content: MCPToolResult
3048 _meta?: Record<string, unknown>
3049 structuredContent?: Record<string, unknown>
3050}> {
3051 const toolStartTime = Date.now()
3052 let progressInterval: NodeJS.Timeout | undefined
3053
3054 try {
3055 logMCPDebug(name, `Calling MCP tool: ${tool}`)
3056
3057 // Set up progress logging for long-running tools (every 30 seconds)
3058 progressInterval = setInterval(
3059 (startTime, name, tool) => {
3060 const elapsed = Date.now() - startTime
3061 const elapsedSeconds = Math.floor(elapsed / 1000)
3062 const duration = `${elapsedSeconds}s`
3063 logMCPDebug(name, `Tool '${tool}' still running (${duration} elapsed)`)
3064 },
3065 30000, // Log every 30 seconds
3066 toolStartTime,
3067 name,
3068 tool,
3069 )
3070
3071 // Use Promise.race with our own timeout to handle cases where SDK's
3072 // internal timeout doesn't work (e.g., SSE stream breaks mid-request)
3073 const timeoutMs = getMcpToolTimeoutMs()
3074 let timeoutId: NodeJS.Timeout | undefined
3075
3076 const timeoutPromise = new Promise<never>((_, reject) => {
3077 timeoutId = setTimeout(
3078 (reject, name, tool, timeoutMs) => {
3079 reject(
3080 new TelemetrySafeError_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS(
3081 `MCP server "${name}" tool "${tool}" timed out after ${Math.floor(timeoutMs / 1000)}s`,
3082 'MCP tool timeout',
3083 ),
3084 )
3085 },
3086 timeoutMs,
3087 reject,
3088 name,
3089 tool,

Callers 1

callIdeRpcFunction · 0.85

Calls 10

logMCPDebugFunction · 0.85
getMcpToolTimeoutMsFunction · 0.85
processMCPResultFunction · 0.85
isMcpSessionExpiredErrorFunction · 0.85
clearServerCacheFunction · 0.85
onProgressFunction · 0.50
logMCPErrorFunction · 0.50
logEventFunction · 0.50
callToolMethod · 0.45

Tested by

no test coverage detected