McpAgent Streamable HTTP: no SSE keepalive on the GET stream — clients silently disconnect every ~100s behind the Cloudflare proxy idle read timeout
Problem
Long-lived MCP client connections to a McpAgent-backed worker (Streamable HTTP) get cut by the Cloudflare proxy edge after ~100 seconds of no data on the SSE GET stream. The agents SDK (verified on 0.17.4) sends no keepalive on the client-facing stream — keepAlive() heartbeats exist only for Durable Object alarms (dist/index.js: "Keep the Durable Object alive via alarm heartbeats"), not for the response stream.
Measured impact (single user, 4 workers on *.mcp-hub.stream, one day)
- ~1,500
SSE stream disconnected: TypeError: The socket connection was closed unexpectedlywarnings/day across 4 MCP servers (Playwright 670, WortWert 690, Rille 114, Memex 102) - No functional loss (opencode's StreamableHTTP client reconnects automatically), but: reconnect latency on the first tool call after idle, heavy client log growth, and the known ecosystem risk of transports sticking after repeated idle disconnects
- Cloudflare proxy read timeout is 100 s on Free/Pro/Business (Enterprise-only configurable), so any idle SSE GET over the proxy hits this
Request
Emit an SSE comment ping (: ping\n\n) every ~30 s on the open GET stream of the Streamable HTTP transport (ideally as an option, e.g. keepaliveIntervalMs). This would keep idle client connections alive behind the proxy without changing MCP semantics — SSE comments are ignored by all compliant clients.
Happy to provide further logs/metrics if useful. Thanks for the SDK — apart from this it removed a whole class of SSE-framing bugs for us.
Thanks for the detailed report and measurements! On current main, the Streamable HTTP transport (WorkerTransport) delegates SSE keepalive comment frames to the MCP SDK transport, with the cadence set by the keepAliveMs option. The legacy bridge also sends a : keepalive comment every 25s. Your report is on 0.17.4, so this may already be addressed. Could you try the latest release and tell us whether the ~100s disconnects continue? If they do, please share which transport/handler your workers use (McpAgent.serve vs createMcpHandler) and whether keepAliveMs is set. We'll dig in from there.
Hey @thomas0369, hope you're doing well! Thanks for such a clear report. The per worker numbers and the ~100s drop pattern made this really easy to follow.
I had a look on current main (5c33d89) and I think the keepAliveMs note fits createMcpHandler, but maybe not McpAgent.serve() yet. Happy to be corrected if I'm misreading it:
serve()only takesbinding,corsOptions,transportandjurisdiction, so there's nowhere to passkeepAliveMs: types.ts#L37-L42, legacy-agent.ts#L573-L580- The POST stream already gets a 25s keepalive through
startKeepalive(really nice work from #1583): utils.ts#L328 - The standalone GET stream and the legacy SSE GET stream don't seem to start one, so an idle GET would still hit the 100s proxy timeout: utils.ts#L391-L512, utils.ts#L594-L706
Something that might work today, if you don't need the McpAgent Durable Object state: createMcpHandler passes keepAliveMs through to the SDK transport, which defaults to 15s (handler-stateless.ts#L180):
import { McpServer } from "@modelcontextprotocol/server";
import { createMcpHandler } from "agents/mcp/server";
const handler = createMcpHandler(
() => new McpServer({ name: "my-server", version: "1.0.0" }),
{ keepAliveMs: 25_000 }
);
export default { fetch: handler } satisfies ExportedHandler<Env>;
(I haven't run this end to end yet, so happy to be told if the v2 transport handles the GET stream differently.)
For a fix, I wonder if the simplest route is calling the existing startKeepalive(writer, encoder) on both GET streams and clearing it in the ws close and error listeners, the same way the POST path does.
@thomas0369 does that workaround fit your setup, or are you relying on McpAgent state? And for the maintainers, would it help if I filed this as a separate bug? Glad to help however is useful, and any tips are very welcome!