Skip to content

feat(mcp): add transport: "auto" for McpAgent.serve()#1207

Merged
threepointone merged 1 commit intomainfrom
mcp-agent-auto-transport
Mar 27, 2026
Merged

feat(mcp): add transport: "auto" for McpAgent.serve()#1207
threepointone merged 1 commit intomainfrom
mcp-agent-auto-transport

Conversation

@threepointone
Copy link
Copy Markdown
Contributor

@threepointone threepointone commented Mar 26, 2026

Summary

Adds a new transport: "auto" option to McpAgent.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.

// Serve both transports on a single path
export default MyMCP.serve("/mcp", { transport: "auto" });

How it works

The auto handler composes the existing createStreamingHttpHandler and createLegacySseHandler with a thin routing layer that discriminates by request shape:

Request Routes to
DELETE Streamable HTTP (SSE has no session teardown)
POST {path}/message Legacy SSE (the /message sub-path is SSE-only)
POST {path} Streamable HTTP
GET with mcp-session-id header Streamable HTTP (standalone SSE reconnect)
GET without mcp-session-id Legacy SSE (new connection)
Other methods 405 Method Not Allowed

The 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 — Added createAutoHandler that composes both existing handlers
  • packages/agents/src/mcp/types.ts — Widened ServeOptions.transport from BaseTransportType to TransportType (which already included "auto")
  • packages/agents/src/mcp/index.ts — Added "auto" case to serve() switch
  • docs/mcp-transports.md — Documented auto transport and updated comparison table

Design decisions

  • serveSSE() unchanged — stays as transport: "sse". Auto is opt-in via serve(path, { transport: "auto" }). No silent behavior changes for existing users.
  • {path}/message is reserved — in auto mode, this sub-path is captured by the SSE handler. This is the same reservation SSE already makes.
  • No changes to DO internals — the auto handler picks the right name prefix (sse: or streamable-http:) at the edge, so getTransportType() / initTransport() inside the DO work as-is.

Test plan

17 new tests in auto-transport.test.ts covering:

  • Routing (5 tests): POST→streamable, POST /message→SSE, GET+header→streamable, GET-header→SSE, DELETE→streamable
  • Protocol (2 tests): Full tool invocation end-to-end through each transport
  • Coexistence (1 test): Both transports active simultaneously with correct responses
  • Notifications (1 test): Notification-only POST returns 202
  • CORS (2 tests): OPTIONS preflight, 405 includes Allow header
  • Edge cases (6 tests): 405 for PUT, 400/404 for bad DELETE, 404 for unknown session, 406 for bad Accept, 400 for missing sessionId

All 791 tests pass (47 test files).

Made with Cursor


Open with Devin

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-bot
Copy link
Copy Markdown

changeset-bot bot commented Mar 26, 2026

🦋 Changeset detected

Latest commit: ce385cf

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

This PR includes changesets to release 1 package
Name Type
agents 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

Copy link
Copy Markdown
Contributor

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no potential bugs to report.

View in Devin Review to see 3 additional findings.

Open in Devin Review

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Mar 26, 2026

Open in StackBlitz

agents

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

@cloudflare/ai-chat

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

@cloudflare/codemode

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

hono-agents

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

@cloudflare/shell

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

@cloudflare/think

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

@cloudflare/voice

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

@cloudflare/worker-bundler

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

commit: ce385cf

@threepointone threepointone merged commit b1da19c into main Mar 27, 2026
2 checks passed
@threepointone threepointone deleted the mcp-agent-auto-transport branch March 27, 2026 17:11
@github-actions github-actions bot mentioned this pull request Mar 27, 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.

1 participant