# Where must an instance bound SSE fallback negotiation when an MCP streamable-HTTP transport silently degrades to a half-open event stream **Date of Record:** 2026 November 29 **Scope:** Applies to MCP clients and servers negotiating the streamable-HTTP transport where a server may respond to a POST `initialize`/`tools/call` with either a single JSON body or a `text/event-stream`, and where the client may fall back from streaming to SSE mid-session. Does not cover stdio transport, WebSocket transports, or pure request/response MCP servers that never emit `text/event-stream`. ## Core Distinction The failure is not "the SSE fallback broke." It is that **transport-mode selection is negotiated per-request but session state is assumed per-connection.** In a streamable-HTTP MCP server, a single logical session (identified by `Mcp-Session-Id`) may serve request A as `application/json` and request B as `text/event-stream`, and a client's fallback logic is triggered by *response headers on the current request*, not by a session-wide capability agreement. This creates a boundary condition where the client believes it has degraded to a streaming read loop, while the server has already closed the event stream after emitting the terminal `message` event for that one request. The client then blocks on a stream that will never produce another frame — a **half-open SSE read** — while the server considers the request complete and the session idle. The invariant that must hold, and which streamable HTTP does not enforce by default, is: *a client's read-loop state must be derived from the response's framing contract, not from the presence of a `Content-Type: text/event-stream` header.* The header tells you the encoding of *this response body*; it does not tell you the stream is long-lived, multiplexed, or session-scoped. ## What Was Observed A tool-calling agent issued `tools/call` for a long-running operation. The server's first response to `initialize` was `application/json` (single body, session established, `Mcp-Session-Id` returned). The subsequent `tools/call` was answered with `200 OK`, `Content-Type: text/event-stream`, `Cache-Control: no-cache`, and `Connection: keep-alive`. The client's transport layer saw the SSE content type and entered its streaming reader: it opened the event loop, registered an `onmessage` handler, and awaited the terminal result. The server, however, emitted exactly three SSE frames — a `progress` notification, a `progress` notification, and the terminal `message` containing the tool result — then **closed the response body** after the terminal frame (this is the correct streamable-HTTP behavior: the POST's event stream is scoped to the request, and completion is signaled by stream close, not by a sentinel event). The client's reader did not treat stream close as terminal because its fallback path had been written for the *long-lived* SSE transport (the older, connection-scoped MCP SSE mode), where the stream is meant to stay open and the server sends results as events over a persistent channel. Observed trace: 1. `POST /mcp` with `tools/call`, headers include `Accept: application/json, text/event-stream`. 2. Server responds `200`, `text/event-stream`, emits `event: progress` ×2, `event: message` with result, then FIN. 3. Client reader receives terminal `message`, dispatches result to the agent, but **does not exit the read loop** — it awaits more frames because its fallback state machine has no transition from `STREAMING` to `DONE` on EOF; it only transitions on an explicit close event or timeout. 4. The agent's turn completes, but the transport worker remains parked on a dead socket. On the next `tools/call` for the same session, the client reuses the worker, writes the request, and reads — but the server has already closed that response and treats the new POST as a fresh request. The client's reader is attached to the wrong stream, and the response body it reads is the *new* response's body, whose framing it misinterprets because its state machine still believes it is mid-stream from the prior request. 5. Result: the second tool result is attributed to the first call's promise, and the first call's promise never resolves. The agent proceeds with a **stale, misattributed tool result** — a silent correctness failure, not a crash. The non-obvious part: the failure is invisible at the HTTP layer. Both requests return `200`. No error, no timeout on the request itself, no protocol violation. The corruption is purely in the client's mapping from response framing to promise resolution. ## Condition of Validity The half-open read only manifests when **all** of the following hold: - The server uses request-scoped SSE (stream closes after terminal frame) rather than connection-scoped SSE (stream persists across requests). The streamable-HTTP spec permits the former; many servers implement it. - The client's fallback path was authored against the connection-scoped model and lacks an EOF-as-terminal transition. - The client multiplexes multiple requests over a single transport worker or session object, so a parked reader can be reused by a later request. - The server does not send an explicit `event: close` or equivalent sentinel before FIN (again, spec-permitted, since close is signaled by stream termination). If any one of these is absent, the failure does not occur. This is why it survives testing: a single-request test passes (the result is delivered; the parked reader is harmless if the process exits), and a server that sends a close sentinel masks it. ## Decision Boundary The boundary the client must enforce is: **treat SSE stream termination (EOF) as a terminal state for the request that owns that response, and never reuse a reader across requests unless the server has explicitly declared connection-scoped streaming via a negotiated capability.** Concretely, the client's transport state machine must have: - A transition `STREAMING → DONE` on `read() == b""` (EOF), unconditionally, for request-scoped streams. - A hard invariant: each `POST` allocates a fresh reader bound to that response object; readers are not pooled across requests unless a session-level capability flag `connection_scoped_sse: true` was negotiated during `initialize`. - A guard that raises if a reader is asked to `read()` after it has observed EOF, rather than silently returning empty and letting the caller misinterpret it. The server-side counterpart: if it intends connection-scoped streaming, it must advertise it in the `initialize` result's capabilities; otherwise every SSE response is request-scoped and closes on terminal frame. ## Operational Effect Without the invariant, the agent experiences **non-deterministic tool-result misattribution** that is indistinguishable from a model reasoning error. The agent sees a plausible tool result, but it belongs to a different call. Downstream, this corrupts the agent's episodic memory: the trace records "call X returned Y" when Y was the result of call X+1. On resume or distillation, the wrong association is reinforced. The failure is also load-dependent — it requires two overlapping or near-overlapping calls on the same session — so it appears intermittently under concurrency and vanishes in single-threaded tests. With the invariant, the second call's reader is a fresh object, EOF is terminal, and the parked-reader class of bug is structurally impossible. The cost is one reader allocation per request, which is negligible against the correctness gain. ```python import http.client from typing import Optional class HalfOpenStreamError(RuntimeError): """Raised when an SSE reader is reused or read after terminal EOF.""" pass class RequestScopedSSEReader: """Reader bound to exactly one HTTP response. EOF is terminal.""" def __init__(self, response: http.client.HTTPResponse, request_id: str): self._response = response self._request_id = request_id self._eof_seen = False self._terminal_dispatched = False def read_event(self) -> Optional[bytes]: if self._eof_seen: # Reuse after EOF is the half-open bug: refuse instead of # silently returning b"" and letting the caller misattribute # the next request's frames to this one. raise HalfOpenStreamError( f"reader for request {self._request_id} already reached EOF; " f"allocate a new reader per POST" ) chunk = self._response.read(1) if chunk == b"": self._eof_seen = True if not self._terminal_dispatched: raise HalfOpenStreamError( f"request {self._request_id} stream closed before terminal " f"`message` event; cannot mark complete" ) return None return chunk def mark_terminal(self) -> None: self._terminal_dispatched = True def dispatch_tool_result( reader: RequestScopedSSEReader, expected_request_id: str ) -> dict: if reader._request_id != expected_request_id: raise HalfOpenStreamError( f"reader bound to {reader._request_id} cannot serve " f"{expected_request_id}; transport worker was reused across requests" ) # ... parse frames until `event: message`, then call reader.mark_terminal() # and rely on EOF as the completion signal for request-scoped streams. return {} ```