# What happens when an instance resumes an MCP SSE stream after a load-balancer re-pins its session to a cold replica **Date of Record:** 2026 November 28 **Scope:** Applies to MCP servers exposing the HTTP-native streamable transport (POST for requests, GET SSE for server→client notifications) with `Last-Event-ID`-based resumption behind a stateful session-affinity layer (cookie, header, or consistent-hash LB). Does not apply to stateless stdio transports or to SSE deployments that terminate the event log at the edge and replay from a shared durable buffer keyed identically across replicas. ## Core Distinction The failure is not "the stream dropped." Streams drop constantly and resumption is designed for exactly that. The failure is a **silent semantic divergence between the client's `Last-Event-ID` cursor and the server replica's event-log offset** when affinity re-routes a resume request to a replica that never produced the events the client is asking to skip past. MCP's resumable SSE contract is deceptively simple: the client reconnects with `Last-Event-ID: N`, and the server replays events `N+1, N+2, …`. This is correct *only if* the event-ID space is a single monotonic sequence shared by every replica that can serve the session. In an HTTP-native transport with session affinity, it is not. Each replica maintains its own append log with its own counter. Replica A has emitted events up to ID 47. Replica B, cold, has emitted nothing and its counter sits at 0. When the LB re-pins the client to B and the client sends `Last-Event-ID: 47`, a naive server treats 47 as "already delivered" and resumes at 48 — but B has no events 1–47 to have skipped, so the client receives a stream that begins at 48 with no relationship to what actually happened. The events between the last genuinely-delivered notification and the failover point are lost, and the client's cursor now points into a sequence that does not exist on the replica serving it. The invariant that is violated: **`Last-Event-ID` is a cursor into a specific producer's log, not a global watermark.** Affinity layers treat sessions as fungible; resumable transports treat event offsets as producer-local. The two assumptions collide precisely at failover. ## What Was Observed A tool-calling agent driving a long-running `filesystem` + `git` MCP server over the streamable transport. The server emits `notifications/tools/list_changed` and progress notifications on the SSE channel. Mid-task, the agent issued a `tools/call` for a `git rebase` that streamed progress every ~2s. The LB (consistent-hash on `Mcp-Session-Id`) had replica A pinned. A rolled during a deploy; the LB re-hashed the session to B, which had the session record (shared Redis) but an empty in-process event log. Trace: 1. Client holds `Last-Event-ID: 312`. Last delivered event was `progress: 61%`. 2. Connection drops (A terminated). Client's SSE library reconnects with `Last-Event-ID: 312` per MCP spec. 3. LB routes to B (affinity key unchanged, but A is gone). B loads the session record — `session_id` valid, `tools/list` hash matches — and accepts the GET. 4. B's event log is empty; its counter is 0. Server logic: "client wants events after 312; I have none after 312; nothing to replay." B opens an empty stream and waits for *new* events. 5. Meanwhile the actual `git rebase` was executing on A's worker and died with A. No `progress: 100%`, no `result`, no `error`. The `tools/call` future on the client never resolves. 6. 90s later the client's request-level timeout fires. The agent, seeing a timeout rather than an error, retried the `tools/call`. The retry hit B, which had no record of an in-flight call and *re-executed the rebase* — on a repo that was now in a partially-rebased state from the first attempt. Two distinct losses compounded: **event-stream continuity** (progress 61%→100% vanished) and **call-idempotency** (the retry re-ran a non-idempotent mutation). The first is the resumption bug; the second is its consequence, because a client that cannot distinguish "stream resumed but call is still running on a dead replica" from "call never started" has no basis for choosing retry vs. wait. The tell in logs: B's access log shows `GET /mcp 200` with `Last-Event-ID: 312` and an immediate stream close on client timeout, with **zero events emitted between accept and close**. A correct resumable server would either replay from its log or return `409 Conflict` / `412` indicating the cursor is ahead of its log. B did neither; it silently accepted a cursor it could not honor. ## Condition of Validity Resumption via `Last-Event-ID` is sound **iff** all of the following hold: - The event-ID space is **globally monotonic per session**, not per replica. Either a single writer per session, or a shared sequencer (e.g., a durable log with a global offset), or IDs that encode `(replica_epoch, local_seq)` so a client can detect an epoch change. - The replica serving the resume can **distinguish three cases**: cursor ≤ its high-water mark (replay from log), cursor == its high-water mark (resume live), cursor > its high-water mark *and the cursor was produced by a different epoch* (the cursor is meaningless here — must not be silently honored). - The in-flight `tools/call` state is **durable across replica loss**, or the transport surfaces call death explicitly rather than via client-side timeout. When any of these is false — as in the observed deployment, where the event log was in-process and IDs were a per-replica counter — the resumption contract is nominal only. The client believes it resumed; the server believes it has nothing to replay; both are "correct" against their local model and the composition is wrong. This condition is *not* satisfied by "the session record is in Redis." Session *identity* durability (so the LB and the server agree the session exists) is orthogonal to event-log durability. The observed system had the former and lacked the latter, which is the common and dangerous configuration because it makes the failover *look* clean at the session layer while the transport layer silently loses data. ## Decision Boundary The server, on receiving a resume GET with `Last-Event-ID: N`, must branch on a comparison it currently does not make: - **`N < log.low_water`** (client's cursor is older than the retained window): the events are gone. Do **not** silently resume at `log.high_water`. Emit a synthetic `notifications/stream_gap` (or close with a distinct code) so the client knows continuity broke and must re-derive state (re-issue `tools/list`, re-query call status). Silent resumption here is the bug. - **`log.low_water ≤ N ≤ log.high_water`**: replay `N+1 … high_water` from the durable log, then attach live. Correct path. - **`N > log.high_water`**: the cursor is from a **different epoch** (or a replica that never produced it). This is the failover case. The server must **reject** with a cursor-epoch error, not accept-and-wait. Accepting produces the empty-stream-then-timeout trace above. - **`N == log.high_water`**: resume live. Correct. The boundary the observed system crossed: it collapsed cases 1 and 3 into "resume live," because it compared against a counter that was `0` on the cold replica, making every nonzero client cursor fall into `N > high_water` and be treated as "nothing to replay, wait for new." The guardrail is that **`high_water` on a cold replica is not a valid comparison point** — a replica with an empty log has no opinion about cursors and must defer to the durable log or reject. For the call-idempotency half: the transport must carry call state such that a resume can answer "is call C still running, completed, or dead?" If the answer is "dead" (replica lost), the client must receive an explicit `error` for the `tools/call`, not a dangling future. A `tools/call` whose executing replica died and whose result was never durably recorded must be reported as failed, never left to client timeout — because client timeout is indistinguishable from "slow," and the agent's retry policy will treat slow as retryable and re-run a non-idempotent mutation. ## Operational Effect The compounding effect is what makes this a boundary failure rather than a dropped connection. Event-stream loss alone is recoverable: the agent re-syncs tool lists and continues, at worst missing a progress update. But the *silent* acceptance of a foreign cursor converts a transport hiccup into a **state-corrupting retry**: the agent cannot observe that the call died, so its timeout-driven retry re-executes a partially-applied `git rebase`, and the repo is now in a state neither the first nor the second execution intended. The blast radius is not the lost notification; it is the non-idempotent mutation executed twice because the transport lied about continuity. The invariant to enforce operationally: **a resumable stream must never emit "resumed" for a cursor it cannot honor.** Rejecting with `409`/`412` and a cursor-epoch marker is strictly safer than accepting-and-waiting, because rejection forces the client into an explicit re-sync path, while acceptance strands it in a stream that will never carry the events it is waiting for. Pair this with durable call-status records (call ID → {running, done, dead}) written before execution begins, so a resume on any replica can answer the idempotency question without the executing process being alive. The agent's retry policy should then gate on `dead`, not on timeout. ``` ```python class StreamCursorEpochMismatch(RuntimeError): """Client's Last-Event-ID belongs to a producer epoch this replica cannot honor.""" pass class EventLog: def __init__(self, epoch: str, events: list[tuple[int, str]]): self.epoch = epoch self.events = events # [(seq, payload), ...] durable, per-epoch self.high_water = events[-1][0] if events else 0 self.low_water = events[0][0] if events else 0 def replay_after(self, cursor: int) -> list[tuple[int, str]]: return [(s, p) for (s, p) in self.events if s > cursor] def resume_stream( log: EventLog, last_event_id: int | None, client_epoch: str | None, ) -> list[tuple[int, str]]: if last_event_id is None: return [] # fresh stream, no cursor to honor # Cold replica: empty log has no opinion. Never treat high_water==0 as # "nothing to replay, wait live" -- that silently swallows the cursor. if not log.events: raise StreamCursorEpochMismatch( f"replica epoch {log.epoch!r} has empty log; cannot honor " f"Last-Event-ID={last_event_id} from epoch {client_epoch!r}; " f"client must re-sync (tools/list, call status) before resuming" ) if client_epoch is not None and client_epoch != log.epoch: raise StreamCursorEpochMismatch( f"cursor epoch {client_epoch!r} != replica epoch {log.epoch!r}; " f"events after {last_event_id} were produced by a different " f"producer and cannot be replayed here" ) if last_event_id < log.low_water: # Events aged out of the retained window: continuity is broken. raise StreamCursorEpochMismatch( f"Last-Event-ID={last_event_id} older than retained window " f"[{log.low_water}, {log.high_water}]; emit stream_gap and force re-sync" ) if last_event_id > log.high_water: raise StreamCursorEpochMismatch( f"Last-Event-ID={last_event_id} ahead of high_water={log.high_water} " f"on epoch {log.epoch!r}; cursor is foreign, reject rather than hang" ) return log.replay_after(last_event_id) ```