Skip to content

Commit ddf0a8e

Browse files
committed
fix: better handling of MCP tool errors
1 parent 1ff183c commit ddf0a8e

2 files changed

Lines changed: 22 additions & 10 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
- improved MCP client interruption handling
77
- properly adding user input messages to the input history when using MCP tools
88
- wrapping long tool message content
9+
- better handling of MCP tool errors
910

1011
## [0.3.2]
1112

src/main/mcp-client.ts

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -321,19 +321,30 @@ export class McpClient {
321321

322322
project.sendToolMessage(toolCall.name, toolCall.args);
323323

324-
const toolResponse = await selectedTool.invoke(toolCall);
324+
try {
325+
const toolResponse = await selectedTool.invoke(toolCall);
325326

326-
logger.debug(`Tool ${toolCall.name} returned response`, {
327-
toolResponse,
328-
});
329-
if (!toolResponse) {
330-
logger.warn(`Tool ${toolCall.name} didn't return a response`);
331-
return null;
327+
logger.debug(`Tool ${toolCall.name} returned response`, {
328+
toolResponse,
329+
});
330+
if (!toolResponse) {
331+
logger.warn(`Tool ${toolCall.name} didn't return a response`);
332+
return null;
333+
}
334+
335+
// Add tool message to messages
336+
messages.push(toolResponse);
337+
} catch (error) {
338+
const errorMessage = error instanceof Error ? error.message : String(error);
339+
logger.error(`Error invoking tool ${toolCall.name}:`, error);
340+
341+
// Send log message about the tool error
342+
project.sendLogMessage('error', `Tool ${toolCall.name} failed: ${errorMessage}`);
343+
344+
// Add user message to messages for next iteration
345+
messages.push(new HumanMessage(errorMessage));
332346
}
333347

334-
// Add tool message to messages
335-
messages.push(toolResponse);
336-
337348
// Update the last tool call time after the delay
338349
this.lastToolCallTime = Date.now();
339350
}

0 commit comments

Comments
 (0)