fix(mcp-tools): improve error message formatting and response handlin… (#5565)

fix(mcp-tools): improve error message formatting and response handling in upsertMCPToolResponse function
This commit is contained in:
MyPrototypeWhat 2025-05-01 18:06:53 +08:00 committed by GitHub
parent e0e1edebbe
commit d78d8373d7

View File

@ -228,7 +228,7 @@ export async function callMCPTool(tool: MCPTool): Promise<MCPCallToolResponse> {
content: [ content: [
{ {
type: 'text', type: 'text',
text: `Error calling tool ${tool.name}: ${e instanceof Error ? (e.stack || e.message || "No error details available") : JSON.stringify(e)}` text: `Error calling tool ${tool.name}: ${e instanceof Error ? e.stack || e.message || 'No error details available' : JSON.stringify(e)}`
} }
] ]
}) })
@ -309,18 +309,21 @@ export function upsertMCPToolResponse(
onChunk: (chunk: MCPToolInProgressChunk | MCPToolCompleteChunk) => void onChunk: (chunk: MCPToolInProgressChunk | MCPToolCompleteChunk) => void
) { ) {
const index = results.findIndex((ret) => ret.id === resp.id) const index = results.findIndex((ret) => ret.id === resp.id)
let result = resp
if (index !== -1) { if (index !== -1) {
results[index] = { const cur = {
...results[index], ...results[index],
response: resp.response, response: resp.response,
status: resp.status status: resp.status
} }
results[index] = cur
result = cur
} else { } else {
results.push(resp) results.push(resp)
} }
onChunk({ onChunk({
type: resp.status === 'invoking' ? ChunkType.MCP_TOOL_IN_PROGRESS : ChunkType.MCP_TOOL_COMPLETE, type: resp.status === 'invoking' ? ChunkType.MCP_TOOL_IN_PROGRESS : ChunkType.MCP_TOOL_COMPLETE,
responses: results responses: [result]
}) })
} }