feat(mcp): add transport: "auto" for McpAgent.serve()#1207
Merged
threepointone merged 1 commit intomainfrom Mar 27, 2026
Merged
feat(mcp): add transport: "auto" for McpAgent.serve()#1207threepointone merged 1 commit intomainfrom
transport: "auto" for McpAgent.serve()#1207threepointone merged 1 commit intomainfrom
Conversation
Introduce an auto-negotiating MCP transport that serves both Streamable HTTP and legacy SSE on the same endpoint. Adds createAutoHandler with routing rules (POST /message → SSE, POST / → streamable HTTP, GET w/ mcp-session-id → streamable HTTP, GET without → SSE, DELETE → streamable HTTP), exposes transport: "auto" in ServeOptions/types, and wires it into McpAgent. Updates docs to describe the Auto transport, adds comprehensive tests, and updates the test worker to mount an /auto endpoint. Also adds a changeset entry and updates the invalid-transport error message to include `auto`.
🦋 Changeset detectedLatest commit: ce385cf 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
hono-agents
@cloudflare/shell
@cloudflare/think
@cloudflare/voice
@cloudflare/worker-bundler
commit: |
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
Adds a new
transport: "auto"option toMcpAgent.serve()that serves both Streamable HTTP and legacy SSE on the same endpoint. Capable clients get the modern Streamable HTTP protocol automatically, while older SSE-only clients continue to work transparently — no client changes required.How it works
The auto handler composes the existing
createStreamingHttpHandlerandcreateLegacySseHandlerwith a thin routing layer that discriminates by request shape:DELETEPOST {path}/message/messagesub-path is SSE-only)POST {path}GETwithmcp-session-idheaderGETwithoutmcp-session-idThe two protocols have completely non-overlapping HTTP signatures, so routing is deterministic — no heuristics or content negotiation.
What changed
packages/agents/src/mcp/utils.ts— AddedcreateAutoHandlerthat composes both existing handlerspackages/agents/src/mcp/types.ts— WidenedServeOptions.transportfromBaseTransportTypetoTransportType(which already included"auto")packages/agents/src/mcp/index.ts— Added"auto"case toserve()switchdocs/mcp-transports.md— Documented auto transport and updated comparison tableDesign decisions
serveSSE()unchanged — stays astransport: "sse". Auto is opt-in viaserve(path, { transport: "auto" }). No silent behavior changes for existing users.{path}/messageis reserved — in auto mode, this sub-path is captured by the SSE handler. This is the same reservation SSE already makes.sse:orstreamable-http:) at the edge, sogetTransportType()/initTransport()inside the DO work as-is.Test plan
17 new tests in
auto-transport.test.tscovering:All 791 tests pass (47 test files).
Made with Cursor