magpie icon indicating copy to clipboard operation
magpie copied to clipboard

gateway: mcpProxy buffers headers on silent GET SSE streams, hanging MCP client handshake

Open wjhwjhn opened this issue 1 week ago • 5 comments

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

  1. Register a Streamable HTTP MCP server in magpie whose standalone GET endpoint returns 200 OK with Content-Type: text/event-stream and waits silently without sending an initial SSE event byte.
  2. Start an MCP client whose handshake waits for the GET SSE response headers before continuing (such as agy's Go MCP SDK) against http://127.0.0.1:3425/mcp/<name>.
  3. Or run the new regression test TestMCPProxyFlushesSilentGETStreamHeaders against origin/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): all success.
  • Additional local verification:
    • Red-on-old / green-on-new TestMCPProxyFlushesSilentGETStreamHeaders (10.12s fail -> 0.12s pass) covering both direct and restricted_key paths.
    • Linux x86_64 (go1.26.8): go test -race -tags nogui -count=20 -run 'TestMCPProxy|TestKeyMCPProxy' ./internal/gateway passed (73.077s).
    • Live Windows verification with agy against local magpie /mcp/<server>: GET SSE headers arrive in <1s and MCP tools load.
  • Not tried: GUI Playwright suite (not run: no internal/gui/assets changes).

wjhwjhn avatar Nov 25 '26 12:11 wjhwjhn

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).

joei4cm avatar Nov 25 '26 12:11 joei4cm

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.

L0NE-6 avatar Nov 25 '26 13:11 L0NE-6

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 (TestMCPProxyFlushesSilentGETStreamHeaders fails in 10.06s with context deadline exceeded on both direct and restricted_key), green with the fix (0.12s), gofmt and cross-OS go vet clean.
  • CI: a fresh workflow run is currently running at https://github.com/wjhwjhn/magpie/actions/runs/45629151966 (previous revision 16f079082 passed 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:

  1. restricted_key test path: httptest.NewServer(New().Handler()) omits lanGuard(...) (so access.Caller isn't populated from the Authorization: Bearer header), and keyHeldToMCP(t, "neon-holder", "neon") grants whole-server access (every == true in mcpKeyGuard), which returns the unwrapped ResponseWriter without mcpToolsFilter. Wrapping the server with lanGuard(New().Handler()) and scoping the key to a specific tool (e.g. "neon/run_sql") makes sure mcpToolsFilter.Flush() is actually exercised.
  2. Case-insensitive Content-Type check: strings.Contains(resp.Header.Get("Content-Type"), "text/event-stream") is case-sensitive. Using strings.HasPrefix(strings.ToLower(strings.TrimSpace(...)), "text/event-stream") matches HTTP media-type semantics and internal/gateway/mcp_key.go.
  3. Post-flush streaming check & test run: releasing the fake GET handler after receiving the 200 text/event-stream headers (like HoldGet) also verifies that subsequent SSE events still stream normally once unblocked — and once go test runs on that branch, that covers the full lifecycle end to end.

wjhwjhn avatar Nov 25 '26 13:11 wjhwjhn

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.

L0NE-6 avatar Nov 25 '26 14:11 L0NE-6

Shipped in v0.1.4900 (3e7b6366c), ported from @wjhwjhn's fix/mcp-proxy-flush-sse-headers with credit (Co-authored-by).

  • mcpProxy now flushes the response headers as soon as the upstream answers with text/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/run write their own headers and were not affected.
  • Per joei4cm's and L0NE-6's notes, the fake server gained a HoldGet field read under its lock.
  • TestMCPProxyFlushesSilentGETStreamHeaders covers 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.

yetone avatar Nov 26 '26 19:11 yetone