fix: keep quiet MCP SSE streams alive - #2054
Open
utpalsinghdev wants to merge 2 commits into
Open
utpalsinghdev wants to merge 2 commits into
utpalsinghdev wants to merge 2 commits into
Conversation
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
After
initialize+notifications/initialized, a valid Streamable HTTPGET /mcpreturns200 text/event-streamand then writes nothing until a server-initiated message exists. Bunfetchwaits on the first body byte and times out.Bun.serve({ idleTimeout: 0 })keeps the socket open; it does not emit those bytes.This adds
withMcpSseHeartbeatin@executor-js/host-mcp/sse-heartbeat. It wraps a successful GET SSE response, writes: keepalive\n\nimmediately, then repeats that comment every 25s (same frame and interval as the Cloudflare agents bridge). Original SSE events pass through in order. Status, status text, and headers are copied. Canceling the client body cancels the SDK stream and clears the timer, so a later GET is not 409.Call sites:
createMcpRequestHandlerinapps/local/src/mcp.ts(CLI, local daemon, desktop sidecar) andmakeInMemoryMcpSessionStore(self-host). The serving envelope does not wrap, so a Cloudflare path that already keepalives is not wrapped again.packages/hosts/cloudflareand the agents patch are unchanged.Issue #1983 also lists returning
405for standalone GET. The SDK treats 405 as "this server does not offer SSE." Executor still uses that GET for unsolicited server requests and native elicitation, so this keeps GET and heartbeats the quiet body.Linked issue
Closes #1983
Reproduction
createMcpRequestHandleronBun.serve({ port: 0, idleTimeout: 0 }).initialize(200, capturemcp-session-id).notifications/initialized(202)./mcpwithAccept: text/event-streamand a 1s abort.Before the wrap, Bun
fetchdoes not settle:TimeoutErrorat ~1023ms. Curl can see HTTP 200 headers with no body bytes. After the wrap, GET is 200text/event-stream, first chunk is: keepalive\n\n, cancel, reconnect GET is 200 again.Verification
bun run --cwd packages/hosts/mcp test -- src/sse-heartbeat.test.ts src/in-memory-session-store.test.ts: exit 0, 2 files, 25 tests.bun run --cwd apps/local test -- src/mcp-sse-heartbeat.test.ts: exit 0, 2 tests. Sabotage on unmodified handler: both casesTimeoutErrorat ~1s. After wrap: ~30ms.cd e2e && bunx --bun vitest run --project local local/mcp-standalone-get.test.ts: exit 0, 1 test. Run dir:e2e/runs/local/local-a-quiet-mcp-get-stream-stays-alive-with-an-sse-keepalive-comment/(result.jsonok,terminal.cast).bun run format:check: exit 0.bun run lint: exit 0.bun run typecheck: exit 0, 45/45 packages.bun run testunder full turbo: exit 1.@executor-js/sdkhit 5s timeouts (oauth-ema-lifecycle,oauth-first-party,oauth-flow,oauth-register-dynamic,plugin-after-commit,schema-types). Isolatedbun run --cwd packages/core/sdk test: exit 0, 64 files, 921 tests. Unrelated to this diff; same class of parallel-load flake under full turbo.Envelope test
forwards a live GET SSE body without injecting keepalive commentspins that cloud/agents streams are not double-wrapped here.Docs
None. Behavior is covered by the unit, local Bun, and e2e tests above.