fix(codemode): preserve MCP request context#1793
Merged
Merged
Conversation
🦋 Changeset detectedLatest commit: d167fe4 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: |
threepointone
approved these changes
Jun 24, 2026
a2b5b19 to
ff15a40
Compare
e705c3d to
6549d28
Compare
added 3 commits
June 25, 2026 14:35
Adds a TestCodemodeMcpAgent that registers openApiMcpServer and elicits via McpAgent.elicitInput with the forwarded relatedRequestId. The test drives a real Streamable HTTP POST with no standalone GET stream through the Worker Loader executor RPC boundary, asserts the elicitation rides the originating POST and the tool result follows it, and checks the keepAlive lease is held during the nested wait and released afterward.
The request() callback runs while the sandbox is suspended on its codemode.request() RPC, so the DynamicWorkerExecutor timeout bounds the human-in-the-loop elicitation wait. The default executor timeout (60s, #1806) already matches the elicitation timeout; document that lowering it below the elicitation timeout aborts the call before the user can respond.
6549d28 to
d167fe4
Compare
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
openApiMcpServernow passes the outer MCPexecutetool-call context to its host-side request callback:This lets host callbacks associate elicitation, sampling, roots, and notifications with the originating tool call instead of sending them as standalone messages. The context stays in trusted host code and is not exposed to generated sandbox code. Existing callbacks that only accept
optionsremain compatible.Why
In #1510 tool registration is delegated to
@cloudflare/codemode'sopenApiMcpServer. The MCP SDK suppliesRequestHandlerExtrato every tool handler, but Codemode previously discarded that second argument before invoking the application's request callback. The callback therefore had no request id to pass asrelatedRequestId, so server-to-client requests could not use the originating POST response stream. This PR preserves that SDK context in the per-execution host closure and exports it asOpenApiMcpRequestContext.Relationship to #1734 (now merged)
The
McpAgent.serve()+ Dynamic Worker RPC path reported in #1510 needs both halves:StreamableHTTPServerTransportsend after the child-to-host RPC boundary drops agent ALS.createMcpHandler/WorkerTransportonly needs this PR.Hibernation note
For the
McpAgentdeployment, preferMcpAgent.elicitInput(params, { relatedRequestId: context.requestId }). It wraps the wait inkeepAliveWhile, which holds an alarm-backed keepAlive lease so the Durable Object is not evicted mid-elicitation. The pending elicitation resolver is in-memory by design: an elicitation cannot be resumed across a hard eviction, so the contract is to prevent hibernation during the bounded wait, not to persist the pending request. The new integration test asserts that lease is held during the wait and released after.Tests
Codemode unit/contract (
packages/codemode/src/tests/mcp.test.ts, in-memory transport + realDynamicWorkerExecutor):Real
McpAgentintegration (packages/agents/src/tests/mcp/codemode-context.test.ts), no mocks of the transport, executor, or RPC boundary:TestCodemodeMcpAgentregistersopenApiMcpServerand elicits viaMcpAgent.elicitInput;elicitation/createrequest arrives on the originating POST stream;1while waiting and returns to0after completion.Validation on the rebased branch:
pnpm run build(24 projects)pnpm run check(exports, format, lint, 113 TypeScript projects)Related: #1510, #1490, #1734