fix(mcp): retain transport owner outside agent context#1734
Merged
Conversation
🦋 Changeset detectedLatest commit: 6e58373 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
agents
@cloudflare/ai-chat
@cloudflare/codemode
create-think
hono-agents
@cloudflare/shell
@cloudflare/think
@cloudflare/voice
@cloudflare/worker-bundler
commit: |
StreamableHTTPServerTransport previously recovered its owning agent from AsyncLocalStorage at send time. Host callbacks reached through Worker Loader RPC have no ALS ancestry, so server-to-client requests such as elicitation failed with "Agent was not found in send".\n\nRetain the owning McpAgent for the transport lifetime, matching McpSSETransport. Explicit relatedRequestId routing works without ALS; request-scoped context remains an optional disambiguator when a client reuses an active request id across streams.\n\nAdd request-scoped and standalone elicitation regression coverage and document context behavior.\n\nFixes #1490.
6a74ede to
6e58373
Compare
mattzcarey
approved these changes
Jun 24, 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.
Fixes #1490.
Problem
McpAgentserver-to-client requests could fail when invoked from a host callback reached through Worker Loader RPC, a service binding, or another entrypoint that does not inherit the original agentAsyncLocalStoragecontext.The Streamable HTTP transport looked up its owning agent from ALS at send time and threw:
This affected requests such as elicitation, sampling, and roots initiated from sandbox-backed tool execution.
Fix
StreamableHTTPServerTransportnow retains its owningMcpAgentwhen the transport is created. The transport and agent share the same Durable Object lifetime, and this matches the existingMcpSSETransport, which already captures its owner.The transport still reads the request-scoped connection from ALS when available, but only to disambiguate a protocol-invalid case where multiple live streams claim the same active request ID. Normal explicit
relatedRequestIdrouting uses the sole matching stream and does not depend on ALS. If context is absent and routing is ambiguous, the existing code fails closed rather than guessing.No public API is added.
Why not re-enter ALS?
Re-entering agent context at a cross-isolate callback would synthesize an agent store without restoring the original connection or request. The transport already owns the agent reference it needs, so retaining that owner fixes the correct boundary without asking applications to wrap callbacks or import internal context state.
Tests
Added regressions that clear the agent ALS store before issuing an SDK elicitation and verify:
relatedRequestIdcompletes on the originating POST stream;relatedRequestIdcompletes on the standalone GET stream.Validation after rebasing onto current
main:packages/agentsbuildRelated Codemode work
#1793 preserves the outer MCP tool-call context through
openApiMcpServer, allowing a host callback to supply the exactrelatedRequestId. Together, the two changes cover theMcpAgent.serve()+ Dynamic Worker RPC setup reported in #1510: