addToolResult does not work with MCP tools (dynamic-tool)
Description
Issue
addToolResult is not working with MCP tools, so we can't implement Human in the Loop to allow the user to confirm the tool execution. (it was working on v4)
Use case
I have an implementation of Human in the Loop for MCP tools in my app implemented with the ai-sdk v4. I use some metadata on the tool to check if the tool needs confirmation, if it does, I remove the execute from the tool so that it becomes a client tool that needs addToolResult.
I just upgraded to v5, and the HITL feature stopped working.
Implementation
I'm calling the addToolResult and sending the message, but nothing changes in the tool information when I do so. (I double checked the args with logs)
await addToolResult({
tool,
toolCallId,
output,
});
await sendMessage();
Additional debug info
I noticed that mcp tools invocations are of type dynamic-tool instead of tool-{toolName}, so the real problem might be that addToolResult doesn't work with dynamic-tools. I tested with normal tools and it seems to work fine.
AI SDK Version
- ai: 5.0.0
- @ai-sdk/openai: 2.0.0
- @ai-sdk/react: 2.0.0
This is the workaround I found for now:
- create a function to change a dynamic tool into a normal tool
function convertToNormalTools(
tools: Record<string, Tool>,
): Record<string, Tool> {
const result = Object.fromEntries(
Object.entries(tools).map(([name, tool]) => {
const { type, ...rest } = tool; // remove type=dynamic from mcp tools
return [
name,
{
...rest,
},
];
}),
);
return result;
}
- wrap the mcp tools with the funciton
const mcpTools = convertToNormalTools(await mcpClient.tools());
This makes the addToolResult work again. Let me know if there are some unexpected implications on doing this.
Thanks in advance!
it doesn't support dynamic-tool yet.
I found a way to work around this. you can using the tool function wrap your mcp tool. like this
tools: {
yourTool: tool({ description: yourMcpTool.description, inputSchema: yourMcpTool.inputSchema })
}
Perhaps unrelated, but after upgrading to v5 my MCP tools do not seem to be called automatically. In v4 I used to set maxSteps to a suitably large number, and calling generateStream would chug away, calling tools until it had finished the task.
Now under v5 it just returns the first text response, and never calls any tools. I have replaced maxSteps with stopWhen: stepCountIs(20).
Should the tools be called automatically, or do I have to manually call addToolResult under v5?
@recurser can you create a bug report with a reproduction?
Am I right in thinking this happens because dynamic tool parts no longer start with tool-, so when calling addToolResult the isToolUIPart check here fails so the tool result is never added? (definition)?
@recurser can you create a bug report with a reproduction?
@lgrammel it’s quite a large codebase unfortunately with a custom streaming implementation over graphql. I’ll try, but it won’t be straightforward to isolate that part. I did try the workarounds described above and removing the ‘tools-‘ prefix did get me further but not solve the problem. I can easily test new versions against our codebase if that helps.
@lgrammel this is now working great after recent upgrades 👌
- "@ai-sdk/anthropic": "^2.0.1",
+ "@ai-sdk/google": "^2.0.4",
- "@ai-sdk/openai": "^2.0.10",
+ "@ai-sdk/anthropic": "^2.0.14",
- "@ai-sdk/google": "^2.0.13",
+ "@ai-sdk/openai": "^2.0.27",
- "ai": "^5.0.11",
+ "ai": "^5.0.38",
Everything is now running exactly as it did in v4 for me with no patches or hacks.
Identify issue type
Identified type: Bug
Confidence: High
Rationale: The issue reports a concrete regression/failure in existing SDK behavior: addToolResult does not apply results for MCP dynamic tools in v5, breaking human-in-the-loop flows that worked in v4. Although comments mention missing dynamic-tool support and workarounds, the core report is broken current behavior.
Evidence:
- Reporter says
addToolResultworked in AI SDK v4 but stopped after upgrading to v5 for MCP tools. - Calling
addToolResult({ tool, toolCallId, output })followed bysendMessage()leaves the tool information unchanged, while normal tools work. - Reporter and commenters identify MCP invocations as
dynamic-tool, withaddToolResultnot handling those parts; a workaround is to convert/wrap MCP dynamic tools as normal tools. - A commenter points to
isToolUIPartfailing for dynamic tool parts, so the result is never added.
Bug Reproduction - AI SDK 5
Bug could not be reproduced.
Target branch: release-v5.0
- The reported regression existed in
[email protected]:addToolResultfiltered withisToolUIPart, which excluded MCPdynamic-toolparts. - The checked-out target contains
[email protected];packages/ai/src/ui/chat.tsnow handles static and dynamic tool parts throughisToolOrDynamicToolUIPart. packages/ai/CHANGELOG.mdrecords the fix in[email protected], so upgrading to[email protected]or newer resolves the reported behavior without application workarounds.examples/ai-functions/src/reproduction/issue-7868.ts, supported byexamples/ai-functions/package.json, verifies the expected HITL outcome:addToolResultchanges the dynamic MCP tool tooutput-available, preserves its output, andsendMessage()submits that updated part; all assertions passed.- The separate comment about automatic MCP execution through a custom GraphQL streaming implementation could not be independently evaluated because no isolated provider, model, or transport setup was supplied; it does not change the primary result.
Relevant Documentation
- https://v5.ai-sdk.dev/docs/ai-sdk-ui/chatbot-with-tool-calling
- https://ai-sdk.dev/docs/reference/ai-sdk-ui/use-chat
Closed PR: cannot-reproduce investigation pull request
Bug Reproduction - AI SDK 6
Bug could not be reproduced.
Target branch: release-v6.0
- The expected HITL behavior is for
addToolResultto change an MCPdynamic-toolpart frominput-availabletooutput-availableand include its output in the next request. examples/ai-functions/src/reproduction/issue-7868-add-tool-result-dynamic-tool.tsgenerated a dynamic MCP-style tool call, invokedaddToolResult, and then invokedsendMessage.- On the target branch with
ai6.0.276, the tool part updated locally tooutput-availablewith{ "confirmed": true }, and the subsequent request contained the same result; the reported failure did not occur. packages/ai/CHANGELOG.mdrecords the dynamic-tool compatibility fix inai5.0.23;packages/ai/src/ui/ui-messages.tsnow recognizes dynamic parts inisToolUIPart, andpackages/ai/src/ui/chat.tsuses that guard when adding output.
Relevant Documentation
- https://ai-sdk.dev/docs/reference/ai-sdk-ui/use-chat
- https://ai-sdk.dev/docs/ai-sdk-ui/chatbot-tool-usage
- https://ai-sdk.dev/docs/reference/ai-sdk-core/dynamic-tool
- https://ai-sdk.dev/docs/ai-sdk-core/mcp-tools
Closed PR: cannot-reproduce investigation pull request
Bug Reproduction - AI SDK 7
Bug could not be reproduced.
Target branch: main
- Issue #7868 reports that
addToolResultleaves MCPdynamic-toolparts unchanged, preventing confirmed human-in-the-loop output from being submitted. - The focused artifact at
examples/ai-functions/src/reproduction/issue-7868.tsexited successfully onmain:addToolResultchanged the dynamic part tooutput-available, attached the confirmation output, andsendMessage()submitted that output. - This is general AI SDK UI behavior rather than provider-specific behavior; the artifact exercises the documented MCP-style dynamic tool event without requiring a live provider.
packages/ai/src/ui/chat.tsnow updates all parts accepted byisToolUIPart, whilepackages/ai/src/ui/ui-messages.tsincludesdynamic-toolin that predicate and aliases deprecatedaddToolResulttoaddToolOutput.packages/ai/CHANGELOG.mdrecords the fix in AI SDK 5.0.23; upgrading from the reported 5.0.0 setup to 5.0.23 or later resolves the regression without application workarounds.
Relevant Documentation
- https://ai-sdk.dev/docs/reference/ai-sdk-ui/use-chat
- https://ai-sdk.dev/docs/ai-sdk-ui/chatbot-tool-usage
- https://ai-sdk.dev/docs/reference/ai-sdk-core/dynamic-tool
- https://ai-sdk.dev/docs/ai-sdk-core/mcp-tools
Closed PR: cannot-reproduce investigation pull request
seems to have been resolved in the versions listed ^ marking this as closed