Skip to content

fix(ai-chat): avoid stale MCP tool calls pinning isStreaming - #1654

Merged
threepointone merged 2 commits into
mainfrom
bug/isstreaming-abort-toolcall
Jun 3, 2026
Merged

threepointone merged 2 commits into
mainfrom
bug/isstreaming-abort-toolcall

Conversation

@cjol

@cjol cjol commented Jun 2, 2026 •

Copy link
Copy Markdown
Member

This PR fixes #1614, where useAgentChat().isStreaming could remain true after aborting while a server-side MCP tool call was in flight.

Investigation summary:

  • Reproduced the issue interactively using the examples/ai-chat app connected to an MCP server with a delayed tool.
  • Confirmed that aborting returned the AI SDK chat status to ready, but the UI still derived isStreaming: true from a persisted input-available tool part.
  • Added temporary diagnostics and found that the app defines onToolCall for browser/client tools, while the delayed MCP tool is server-owned.
  • Discovered the old derivation treated any latest-assistant input-available tool part as pending client work whenever onToolCall existed, even when that tool was an MCP/server-side tool the client did not handle.
  • Binary-searched several candidate fixes interactively and narrowed the necessary change to tracking the lifetime of actual onToolCall callbacks rather than using the presence of onToolCall as a proxy.

Problem:

  • Server-side MCP tools can leave a tool part in input-available if the user aborts before the tool output arrives.
  • Apps may still define onToolCall for unrelated browser tools.
  • The previous hook logic combined those facts and kept isStreaming true indefinitely, including after reconnect/hydration.

Solution:

  • Track tool call IDs only while an onToolCall callback is actually in flight.
  • Derive pending client-tool streaming state from those in-flight callback IDs instead of from !!onToolCall.
  • Preserve the existing behavior where asynchronous client-side tool callbacks keep isStreaming true while they are actively running.
  • Add a regression test for a stale server-side/MCP-style tool part that onToolCall does not handle.
  • Add a patch changeset for @cloudflare/ai-chat.

@changeset-bot

changeset-bot Bot commented Jun 2, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: d85f5dc

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@cloudflare/ai-chat Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@cjol
cjol requested a review from threepointone June 2, 2026 13:39

@devin-ai-integration devin-ai-integration Bot left a comment

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.

Devin Review found 2 potential issues.

View 3 additional findings in Devin Review.

Open in Devin Review

Comment thread packages/ai-chat/src/react.tsx
Comment thread packages/ai-chat/src/react.tsx Outdated
@pkg-pr-new

pkg-pr-new Bot commented Jun 2, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

agents

npm i https://pkg.pr.new/agents@1654

@cloudflare/ai-chat

npm i https://pkg.pr.new/@cloudflare/ai-chat@1654

@cloudflare/codemode

npm i https://pkg.pr.new/@cloudflare/codemode@1654

hono-agents

npm i https://pkg.pr.new/hono-agents@1654

@cloudflare/shell

npm i https://pkg.pr.new/@cloudflare/shell@1654

@cloudflare/think

npm i https://pkg.pr.new/@cloudflare/think@1654

@cloudflare/voice

npm i https://pkg.pr.new/@cloudflare/voice@1654

@cloudflare/worker-bundler

npm i https://pkg.pr.new/@cloudflare/worker-bundler@1654

commit: d85f5dc

@threepointone
threepointone force-pushed the bug/isstreaming-abort-toolcall branch from 54cb6d7 to d85f5dc Compare June 3, 2026 11:57

@threepointone threepointone left a comment

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.

clean, lgtm, thanks! will land after CI passes (I rebased)

@threepointone
threepointone merged commit f34cd30 into main Jun 3, 2026
4 checks passed
@threepointone
threepointone deleted the bug/isstreaming-abort-toolcall branch June 3, 2026 12:04
@github-actions github-actions Bot mentioned this pull request Jun 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

The stream is stuck when trigger stop/ abort during tool call

2 participants