gateway: mcpProxy buffers headers on silent GET SSE streams, hanging MCP client handshake
magpie version
main (83f061ed9bc6d27370b1dda154a9a2234cd76a1d)
Where
MCP gateway / proxy
OS
Windows (also affects Linux / macOS mcpProxy)
Agent, provider and model
agy (Go MCP SDK client) talking to a remote OAuth Streamable HTTP MCP server through magpie (http://127.0.0.1:3425/mcp/<name>)
Steps to reproduce
- Register a Streamable HTTP MCP server in
magpiewhose standaloneGETendpoint returns200 OKwithContent-Type: text/event-streamand waits silently without sending an initial SSE event byte. - Start an MCP client whose handshake waits for the
GETSSE response headers before continuing (such asagy's Go MCP SDK) againsthttp://127.0.0.1:3425/mcp/<name>. - Or run the new regression test
TestMCPProxyFlushesSilentGETStreamHeadersagainstorigin/main(83f061ed9).
What happened, and what you expected
What happened:
In internal/gateway/mcp.go, mcpProxy calls w.WriteHeader(resp.StatusCode) and then blocks in resp.Body.Read(buf) before flushing w. Because net/http's response buffers response headers until body bytes are written or Flusher.Flush() is called, the upstream 200 OK headers sit buffered in magpie indefinitely when the server sends no initial event on the standalone GET stream. Clients that wait on GET response headers time out during MCP initialization.
What you expected:
mcpProxy should flush headers immediately after w.WriteHeader(resp.StatusCode) for GET requests and text/event-stream responses, matching sibling runStream in internal/library/mcp_run.go.
Error text or logs
Reproducing TestMCPProxyFlushesSilentGETStreamHeaders on origin/main (83f061ed9):
=== RUN TestMCPProxyFlushesSilentGETStreamHeaders
=== RUN TestMCPProxyFlushesSilentGETStreamHeaders/direct
mcp_test.go:328: GET /mcp/neon blocked waiting for headers: Get "http://127.0.0.1:58710/mcp/neon": context deadline exceeded
=== RUN TestMCPProxyFlushesSilentGETStreamHeaders/restricted_key
mcp_test.go:328: GET /mcp/neon blocked waiting for headers: Get "http://127.0.0.1:58727/mcp/neon": context deadline exceeded
--- FAIL: TestMCPProxyFlushesSilentGETStreamHeaders (10.12s)
Fix branch & verification
Because PR creation is restricted to collaborators (docs/contributors.md), here is the fix branch ready for review/cherry-pick:
- Branch: https://github.com/wjhwjhn/magpie/tree/fix/mcp-proxy-flush-sse-headers
- Commit:
16f079082eca60fcae6b33d0a689102085cbfcf2 - CI (8/8 jobs green): https://github.com/wjhwjhn/magpie/actions/runs/45600265299
test (ubuntu-latest),test (windows-latest),test (macos-latest, 1/3),test (macos-latest, 2/3),test (macos-latest, 3/3),test (macos-latest, gateway),cli (linux/amd64, nogui),cli (android/arm64, nogui): allsuccess.
- Additional local verification:
- Red-on-old / green-on-new
TestMCPProxyFlushesSilentGETStreamHeaders(10.12sfail ->0.12spass) covering both direct andrestricted_keypaths. - Linux
x86_64(go1.26.8):go test -race -tags nogui -count=20 -run 'TestMCPProxy|TestKeyMCPProxy' ./internal/gatewaypassed (73.077s). - Live Windows verification with
agyagainst localmagpie/mcp/<server>:GETSSE headers arrive in<1sand MCP tools load.
- Red-on-old / green-on-new
- Not tried: GUI Playwright suite (
not run: no internal/gui/assets changes).
Confirmed and fixed. The diagnosis was right: mcpProxy wrote the status and then blocked in the upstream body's Read before flushing, so net/http held the headers until the first body byte — and a GET's stream can sit silent with none.
The fix flushes as soon as the status is written, but only on an event-stream response: every other reply here has a body coming, and flushing early would be a no-op at best. It matches runStream in internal/library/mcp_run.go, which already flushes right after WriteHeader.
The regression test needed a server whose GET stream sends no event, so mcpauthtest gains a SilentGET field for it — the existing fake's GET sent its first event immediately, which is why the bug went unseen. Both cases are in: a request with no key and one behind a key held to the server.
Branch: https://github.com/yetone/magpie/compare/main...joei4cm:magpie:fix/7744-silent-get-stream-flush (internal/gateway + the mcpauthtest fixture, eab4b006d + one commit; vet clean, red on main with the fix stashed, green with it).
Independent maintenance check: L0NE-6/magpie-panel now carries the MCP silent-GET header flush as 0002112c. mcpProxy flushes text/event-stream response headers immediately after WriteHeader, so a silent standalone GET no longer blocks MCP initialization. Direct and restricted-key regressions pass, and the focused race test passes 20 iterations. This is a fork integration, not an upstream merge.
Thanks @joei4cm for confirming the root cause and for the great catch that flushing only on text/event-stream responses is cleaner than flushing all GET replies unconditionally (as well as reading HoldGet under f.mu in mcpauthtest.Fake).
I've incorporated both improvements into my fix branch and rebased onto the latest main (0b8172ed0):
- Branch: https://github.com/wjhwjhn/magpie/tree/fix/mcp-proxy-flush-sse-headers (commit
4c0485fb97d82e60951b45ec47b6d1b97dc8de78) - Verification: red on
0b8172ed0(TestMCPProxyFlushesSilentGETStreamHeadersfails in 10.06s withcontext deadline exceededon bothdirectandrestricted_key), green with the fix (0.12s),gofmtand cross-OSgo vetclean. - CI: a fresh workflow run is currently running at https://github.com/wjhwjhn/magpie/actions/runs/45629151966 (previous revision
16f079082passed 8/8 in https://github.com/wjhwjhn/magpie/actions/runs/45600265299).
On a quick look at d56f93553, here are three small catches in case they're helpful:
restricted_keytest path:httptest.NewServer(New().Handler())omitslanGuard(...)(soaccess.Callerisn't populated from theAuthorization: Bearerheader), andkeyHeldToMCP(t, "neon-holder", "neon")grants whole-server access (every == trueinmcpKeyGuard), which returns the unwrappedResponseWriterwithoutmcpToolsFilter. Wrapping the server withlanGuard(New().Handler())and scoping the key to a specific tool (e.g."neon/run_sql") makes suremcpToolsFilter.Flush()is actually exercised.- Case-insensitive
Content-Typecheck:strings.Contains(resp.Header.Get("Content-Type"), "text/event-stream")is case-sensitive. Usingstrings.HasPrefix(strings.ToLower(strings.TrimSpace(...)), "text/event-stream")matches HTTP media-type semantics andinternal/gateway/mcp_key.go. - Post-flush streaming check & test run: releasing the fake GET handler after receiving the
200 text/event-streamheaders (likeHoldGet) also verifies that subsequent SSE events still stream normally once unblocked — and oncego testruns on that branch, that covers the full lifecycle end to end.
Maintenance verification: branch L0NE-6/magpie-panel:fix/7744-mcp-silent-get-stream at ba1ff045 flushes text/event-stream headers immediately after WriteHeader. The direct and restricted-key silent-GET tests fail before the change and pass after it; focused MCP tests pass, the race test passes 20 runs, and go vet ./... plus Linux nogui vet pass. Upstream PR creation is currently disabled, so the branch is available for a maintainer to cherry-pick.
Shipped in v0.1.4900 (3e7b6366c), ported from @wjhwjhn's fix/mcp-proxy-flush-sse-headers with credit (Co-authored-by).
mcpProxynow flushes the response headers as soon as the upstream answers withtext/event-stream, so a GET stream that stays silent no longer leaves the client waiting for headers. Other answers are relayed as before.- The restricted-key path (
mcpToolsFilter) passes the flush on, so a gateway key held to some tools gets the fix too. The hub and/mcp/runwrite their own headers and were not affected. - Per joei4cm's and L0NE-6's notes, the fake server gained a
HoldGetfield read under its lock. TestMCPProxyFlushesSilentGETStreamHeaderscovers the plain and restricted-key cases; both fail on the old code after the 5s timeout, and the restricted case fails with the wrapper's flush turned off. Green with-race -count=20.
Not tried: Windows, and a real agy against a real MCP server. Leaving this open until the reporter confirms it connects.