# What happens when an instance resumes an MCP Streamable HTTP session across a load-balancer boundary **Date of Record:** 2026 November 28 **Scope:** Applies to MCP servers exposing the Streamable HTTP transport (single `/mcp` endpoint, `Mcp-Session-Id` header, optional SSE upgrade for server-to-client messages, `Last-Event-ID` resumption) deployed behind a horizontally scaled, non-session-affine HTTP layer. Does not apply to stdio transports, nor to deployments with sticky routing keyed on `Mcp-Session-Id` and a shared event-log backend. ## Core Distinction The MCP Streamable HTTP transport is *stateless at the request layer* but *stateful at the session layer*. A POST carrying a JSON-RPC request can land on any replica; a resumed GET SSE stream carrying `Last-Event-ID` cannot, because the event log that produced those IDs lives in the memory of the replica that emitted them. The distinction that matters operationally is between **transport-level resumption** (the HTTP connection can be re-established) and **event-log continuity** (the `Last-Event-ID` cursor resolves to a real position in a real buffer). These two are conflated by the spec's phrasing "resumption" and by most SDKs' reconnect loops, and the conflation is where instances silently lose server-to-client messages. ## What Was Observed An agent instance (client) held an open SSE stream against replica A, receiving `notifications/progress` and a pending `sampling/createMessage` server-to-client request. Replica A was drained by the orchestrator (rolling deploy, 30s grace). The client's SSE reader saw the socket close, read the last `id:` field (say `id: 4711`), and reconnected with `Last-Event-ID: 4711` and the same `Mcp-Session-Id`. The load balancer routed the GET to replica B. Replica B had never seen that session ID, but many MCP server implementations treat the `Mcp-Session-Id` as an opaque key and either (a) 404 the session, triggering full re-initialization, or (b) worse, accept the header, allocate a fresh in-memory event ring buffer, and **synthesize a 200 SSE response with no replay** — the stream reopens at cursor zero of a new buffer, with no `Last-Event-ID` validation. The client, per the spec's guidance ("if the server cannot resume, it SHOULD return a fresh stream"), treats the 200 as success. The pending `sampling/createMessage` request ID is never re-delivered. The agent's tool-call loop is waiting on a server-initiated callback that will never arrive. This is not a crash; it is a **silent liveness stall** — the agent's reasoning loop blocks on a future that the transport layer declared healthy. The second-order effect: because the POST path is fine (each POST is independent), the client keeps issuing `tools/call` requests that succeed. The agent looks productive in logs (POST 200s) while a specific server-to-client request is orphaned. Detection requires correlating "outstanding server→client request IDs" against "events observed since last reconnect," which almost no harness does. ## Condition of Validity The failure is bounded by three conditions, all of which must hold: 1. **Non-affine routing** of the resumed GET relative to the original stream's replica. 2. **Ephemeral per-replica event logs** (in-memory ring buffer, no shared store), so `Last-Event-ID` has replica-local meaning only. 3. **A lenient resume path** that returns 200 with a fresh stream rather than 404/409 on an unresolvable cursor. If any one is removed, the failure does not occur: sticky routing keeps the cursor valid; a shared event store (Redis Streams, NATS) makes the cursor replica-independent; a strict 404-on-unknown-cursor forces the client into a full re-initialize path that at least *knows* it lost state. The invariant that must hold for correctness: **a `Last-Event-ID` presented to a replica must either resolve to a contiguous suffix of that session's event history, or the transport must fail closed (4xx) so the client can distinguish "resumed" from "restarted."** Returning 200 on an unresolvable cursor violates it. ## Decision Boundary The architectural choice is between two postures, and the boundary is the *cost of a false resume* versus the *cost of a forced re-init*: - **Fail-closed resume** (4xx on unknown/expired cursor): the agent must re-run `initialize`, re-declare capabilities, and re-establish subscriptions. Cheap for stateless tools; expensive for long-running sessions with accumulated server-side state. But it is *honest* — the client knows it lost the gap. - **Fail-open resume** (200 with fresh stream): preserves the POST-path session illusion but silently drops the server→client gap. Acceptable **only if** the server tracks outstanding server→client request IDs and re-delivers them on the new stream, or if the protocol carries an explicit `resumed: false` / `gap: true` signal the client can act on. The boundary condition: fail-open is safe *iff* the server maintains a durable set of un-acknowledged server→client messages keyed by session (not by replica) and replays it. If the event log is replica-local, fail-open is unsafe by construction and must be replaced with fail-closed. Backpressure sits on the same boundary. The SSE stream has no flow control beyond TCP; a client that stops reading (blocked in its own reasoning loop on a slow tool call) causes the server's write buffer to fill. A correct server must bound the per-session outbound queue and, on overflow, either drop oldest with an explicit `gap` marker or terminate the stream with a resumable cursor — never block the request handler. A server that blocks on a full SSE write buffer stalls *all* requests multiplexed on that replica's event loop, converting one slow agent into a replica-wide outage. ## Operational Effect Concretely, the invariant to enforce is: **the session's event log is the unit of truth, and it must outlive any single replica.** Two implementable forms: 1. **Shared cursor store.** `Last-Event-ID` is a monotonic offset into a per-session durable stream (Redis Stream / NATS subject keyed by `Mcp-Session-Id`). Any replica can resolve any cursor. Resume is always contiguous or explicitly 410 Gone. Backpressure is handled by consumer-group lag, not by blocking the writer. 2. **Affinity + fencing.** Sticky routing on `Mcp-Session-Id` plus a fence token (session epoch) so a resumed stream on a stale replica is rejected. Replicas that lose the fence must not accept resumes. In both, the client-side guardrail is the same: **on every SSE (re)connect, the client must reconcile its set of outstanding server→client request IDs against events received since the last cursor, and if any request is unresolved after a resume, it must treat the session as gap-tainted and re-initialize or surface the orphan.** A 200 SSE response is not evidence of continuity; only cursor-consistent replay is. The failure mode to watch for is not the 404 — it is the 200 that lies. ```python class SessionResumeGapError(RuntimeError): """Raised when an SSE resume succeeds at the transport layer but the event cursor cannot be proven contiguous, leaving server->client requests orphaned. Fail-closed: the caller must re-initialize.""" pass class McpSessionClient: def __init__(self, session_id: str): self.session_id = session_id self.last_event_id: int | None = None # Server->client requests we have emitted to the loop and not yet seen answered. self.outstanding: set[str] = set() def on_server_request(self, request_id: str) -> None: self.outstanding.add(request_id) def on_server_response(self, request_id: str) -> None: self.outstanding.discard(request_id) def resume_stream(self, reconnect_fn, observed_events: list[dict]) -> None: """Reconnect the SSE stream and verify cursor contiguity. `reconnect_fn(last_event_id)` returns (status_code, events, new_cursor). A 200 with a cursor that does not strictly advance past our prior cursor is a silent gap: the server restarted its buffer. """ prior = self.last_event_id status, events, new_cursor = reconnect_fn(prior) if status != 200: # Honest failure: caller re-initializes. Safe. raise SessionResumeGapError( f"resume rejected status={status} session={self.session_id}" ) # Contiguity check: a valid resume replays events strictly after `prior` # and lands on a cursor > prior. A fresh buffer lands at cursor 0 or None. if prior is not None: if new_cursor is None or new_cursor <= prior: raise SessionResumeGapError( f"cursor did not advance: prior={prior} new={new_cursor} " f"session={self.session_id} — server restarted event buffer; " f"outstanding server->client requests orphaned: {self.outstanding}" ) replayed = {e.get("id") for e in events} expected = set(range(prior + 1, new_cursor + 1)) missing = expected - replayed if missing: raise SessionResumeGapError( f"non-contiguous replay, missing event ids {sorted(missing)} " f"session={self.session_id}" ) self.last_event_id = new_cursor for e in events: if e.get("type") == "response": self.on_server_response(e["request_id"]) # After a clean resume, no outstanding request may predate the cursor. if self.outstanding and prior is not None and new_cursor <= prior: raise SessionResumeGapError( f"orphaned requests after resume: {self.outstanding}" ) ```