🦌 Fix missing tool_result when MCP/server tool execution throws#52
Merged
masudahiroto merged 2 commits intomainfrom Apr 2, 2026
Merged
🦌 Fix missing tool_result when MCP/server tool execution throws#52masudahiroto merged 2 commits intomainfrom
masudahiroto merged 2 commits intomainfrom
Conversation
| logger.toolResult(chunk.toolName, `ERROR: ${errorContent}`); | ||
|
|
||
| events.emit<ToolCallResultEvent>({ | ||
| type: EventType.TOOL_CALL_RESULT, |
Contributor
There was a problem hiding this comment.
Is there AG-UI TOOL_CALL_ERROR event we should use instead?
Contributor
Author
There was a problem hiding this comment.
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.
use-ai/packages/client/src/hooks/useToolSystem.ts
Lines 353 to 357 in 6405dbc
So, I think we should use TOOL_CALL_RESULT here.
mm-zacharydavison
requested changes
Apr 2, 2026
# Conflicts: # packages/server/src/agents/AISDKAgent.ts
zdavison
approved these changes
Apr 2, 2026
mm-zacharydavison
approved these changes
Apr 2, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When an MCP or server-side tool throws during execution, the AI SDK emits a
tool-errorchunk instead oftool-result. The agent was silently ignoring these chunks, so noTOOL_CALL_RESULTevent was sent to the client. This left conversation history without atool_resultfor the failedtool_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-errorchunks by emitting aTOOL_CALL_RESULTevent 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: Handletool-errorstream chunk by emittingTOOL_CALL_RESULTwith error content instead of ignoring itpackages/server/src/agents/AISDKAgent.test.ts: Add unit tests verifyingTOOL_CALL_RESULTis emitted on MCP tool error and server tool errorapps/example-nest-mcp-server/src/tools.service.ts: Addalways_failtool for testing error handling scenariosapps/example/src/pages/RemoteMcpToolsPage.tsx: Document the newmcp_always_failtool in the page descriptionapps/example/test/mcp-tool-error-recovery.e2e.test.ts: Add E2E test verifying conversation continues normally after a tool errorbun.lock: Bump package versions to 1.9.3