ai icon indicating copy to clipboard operation
ai copied to clipboard

addToolResult does not work with MCP tools (dynamic-tool)

Open raviships opened this issue 1 year ago • 12 comments

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

raviships avatar Aug 08 '25 03:08 raviships

This is the workaround I found for now:

  1. 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;
}
  1. 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!

raviships avatar Aug 08 '25 05:08 raviships

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 })
}

lili21 avatar Aug 08 '25 10:08 lili21

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 avatar Aug 13 '25 13:08 recurser

@recurser can you create a bug report with a reproduction?

lgrammel avatar Aug 14 '25 07:08 lgrammel

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)?

peteris avatar Aug 14 '25 17:08 peteris

@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.

recurser avatar Sep 05 '25 15:09 recurser

@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.

recurser avatar Sep 08 '25 23:09 recurser

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 addToolResult worked in AI SDK v4 but stopped after upgrading to v5 for MCP tools.
  • Calling addToolResult({ tool, toolCallId, output }) followed by sendMessage() leaves the tool information unchanged, while normal tools work.
  • Reporter and commenters identify MCP invocations as dynamic-tool, with addToolResult not handling those parts; a workaround is to convert/wrap MCP dynamic tools as normal tools.
  • A commenter points to isToolUIPart failing for dynamic tool parts, so the result is never added.

lgrammel avatar Jul 02 '26 09:07 lgrammel

Bug Reproduction - AI SDK 5

Bug could not be reproduced.

Target branch: release-v5.0

  • The reported regression existed in [email protected]: addToolResult filtered with isToolUIPart, which excluded MCP dynamic-tool parts.
  • The checked-out target contains [email protected]; packages/ai/src/ui/chat.ts now handles static and dynamic tool parts through isToolOrDynamicToolUIPart.
  • packages/ai/CHANGELOG.md records 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 by examples/ai-functions/package.json, verifies the expected HITL outcome: addToolResult changes the dynamic MCP tool to output-available, preserves its output, and sendMessage() 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

ai-sdk-factory[bot] avatar Sep 03 '26 19:09 ai-sdk-factory[bot]

Bug Reproduction - AI SDK 6

Bug could not be reproduced.

Target branch: release-v6.0

  • The expected HITL behavior is for addToolResult to change an MCP dynamic-tool part from input-available to output-available and include its output in the next request.
  • examples/ai-functions/src/reproduction/issue-7868-add-tool-result-dynamic-tool.ts generated a dynamic MCP-style tool call, invoked addToolResult, and then invoked sendMessage.
  • On the target branch with ai 6.0.276, the tool part updated locally to output-available with { "confirmed": true }, and the subsequent request contained the same result; the reported failure did not occur.
  • packages/ai/CHANGELOG.md records the dynamic-tool compatibility fix in ai 5.0.23; packages/ai/src/ui/ui-messages.ts now recognizes dynamic parts in isToolUIPart, and packages/ai/src/ui/chat.ts uses 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

ai-sdk-factory[bot] avatar Sep 03 '26 19:09 ai-sdk-factory[bot]

Bug Reproduction - AI SDK 7

Bug could not be reproduced.

Target branch: main

  • Issue #7868 reports that addToolResult leaves MCP dynamic-tool parts unchanged, preventing confirmed human-in-the-loop output from being submitted.
  • The focused artifact at examples/ai-functions/src/reproduction/issue-7868.ts exited successfully on main: addToolResult changed the dynamic part to output-available, attached the confirmation output, and sendMessage() 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.ts now updates all parts accepted by isToolUIPart, while packages/ai/src/ui/ui-messages.ts includes dynamic-tool in that predicate and aliases deprecated addToolResult to addToolOutput.
  • packages/ai/CHANGELOG.md records 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

ai-sdk-factory[bot] avatar Sep 03 '26 19:09 ai-sdk-factory[bot]

seems to have been resolved in the versions listed ^ marking this as closed

aayush-kapoor avatar Sep 03 '26 19:09 aayush-kapoor