Skip to content

🦌 Fix missing tool_result when MCP/server tool execution throws#52

Merged
masudahiroto merged 2 commits intomainfrom
deer/fix-mcp-tool-error-recovery
Apr 2, 2026
Merged

🦌 Fix missing tool_result when MCP/server tool execution throws#52
masudahiroto merged 2 commits intomainfrom
deer/fix-mcp-tool-error-recovery

Conversation

@masudahiroto
Copy link
Copy Markdown
Contributor

Summary

When an MCP or server-side tool throws during execution, the AI SDK emits a tool-error chunk instead of tool-result. The agent was silently ignoring these chunks, so no TOOL_CALL_RESULT event was sent to the client. This left conversation history without a tool_result for the failed tool_use, causing the Anthropic API to reject subsequent requests with: "tool_use ids were found without tool_result blocks immediately after".

This fix handles tool-error chunks by emitting a TOOL_CALL_RESULT event containing the error message, ensuring conversation history remains complete and the session can continue after a tool failure.

Changes

  • packages/server/src/agents/AISDKAgent.ts: Handle tool-error stream chunk by emitting TOOL_CALL_RESULT with error content instead of ignoring it
  • packages/server/src/agents/AISDKAgent.test.ts: Add unit tests verifying TOOL_CALL_RESULT is emitted on MCP tool error and server tool error
  • apps/example-nest-mcp-server/src/tools.service.ts: Add always_fail tool for testing error handling scenarios
  • apps/example/src/pages/RemoteMcpToolsPage.tsx: Document the new mcp_always_fail tool in the page description
  • apps/example/test/mcp-tool-error-recovery.e2e.test.ts: Add E2E test verifying conversation continues normally after a tool error
  • bun.lock: Bump package versions to 1.9.3

Created by deer — review carefully.

logger.toolResult(chunk.toolName, `ERROR: ${errorContent}`);

events.emit<ToolCallResultEvent>({
type: EventType.TOOL_CALL_RESULT,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there AG-UI TOOL_CALL_ERROR event we should use instead?

Copy link
Copy Markdown
Contributor Author

@masudahiroto masudahiroto Apr 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AG-UI doesn't define TOOL_CALL_ERROR event.
https://docs.ag-ui.com/concepts/events#tool-call-events

Client-side tool also treated error message same as normal tool call.

} catch (err) {
console.error('Tool execution error:', err);
client.sendToolResponse(toolCallId, {
error: err instanceof Error ? err.message : 'Unknown error',
});

So, I think we should use TOOL_CALL_RESULT here.

# Conflicts:
#	packages/server/src/agents/AISDKAgent.ts
@masudahiroto masudahiroto merged commit 1ac78f9 into main Apr 2, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants