# What structural signals indicate an MCP streamable-HTTP session was silently forked by a load balancer mid-resumption **Date of Record:** 2026 November 27 **Scope:** Applies to MCP servers exposing the streamable-HTTP transport with `Mcp-Session-Id`-based resumption and `Last-Event-ID` replay, fronted by any L7 proxy that routes per-connection rather than per-session-id. Does not apply to stdio transports, nor to single-process servers with no intermediary. ## Core Distinction The streamable-HTTP transport spec gives the client two independent continuity handles: the `Mcp-Session-Id` header (server-side session identity) and the `Last-Event-ID` header (event-stream replay cursor). The invariant most implementations assume is that these two handles are *co-located*: that a request carrying session S and cursor C will be served by the process that owns S's event log and can replay from C. This is false the moment a load balancer is in the path. The `Mcp-Session-Id` is an opaque application-layer token the LB does not parse; the LB routes on TCP connection affinity, source IP, or a round-robin policy. The failure is not "session lost" (which is loud and recoverable). It is **session forking**: the resumption request lands on a second backend that *accepts* the session ID, *accepts* the cursor, and *serves a divergent event log* — because the first backend had already emitted events past C that the second backend never saw. The client sees a 200 and a valid SSE stream. The two streams have silently diverged. Nothing in the protocol signals this. The non-obvious part: the replay cursor is *monotonic within one backend's log but not globally*. Two backends replaying "from C" produce two different, individually-consistent suffixes. The divergence is only detectable by comparing event IDs the client already consumed against the IDs the new backend replays — which requires the client to have retained a bounded window of its own consumed history. ## What Was Observed A three-replica MCP server behind a round-robin L7 proxy. Client opens a streamable-HTTP session, receives `Mcp-Session-Id: 7f3a`, and begins consuming SSE events `id: 100, 101, 102`. The connection drops (idle timeout at the proxy). Client resubscribes with `Mcp-Session-Id: 7f3a` and `Last-Event-ID: 102`. The LB routes to replica B. Replica B's session store is shared (Redis) so it recognizes `7f3a` and returns 200. But replica B's *event log* is local to replica A (in-memory ring buffer, not shared). Replica B's replay logic, on a cursor it has never seen, falls back to "replay from head" — emitting `id: 100, 101, 102, 103` where `103` is an event replica B fabricated from its own partial view of the tool-call state, not the `103` replica A would have emitted. The client now has consumed `100, 101, 102` from A and `100, 101, 102, 103` from B. The duplicate `100–102` are idempotent (dedup by event ID). The `103` from B is *not* the `103` A would have produced — A's `103` was a `tool_result` for a call whose `tool_use` was `101`; B's `103` is a `heartbeat` because B never received the `tool_use`. The client's assembled conversation now has a `tool_result` with no matching `tool_use`, and the model's next turn fails schema validation — *three turns later*, far from the resumption point. The structural signal that was missed: **the replayed prefix did not match the client's consumed prefix byte-for-byte**. The client had `102` as the last ID; B replayed starting at `100`, not at `103`. A correct resumption from cursor `102` must replay *only* `103+`. Replaying `100` again is the tell that the backend does not own the log the cursor indexes into. ## Condition of Validity The fork is undetectable and harmless if and only if all three hold: 1. **Event IDs are globally monotonic across replicas** (e.g., a shared sequencer). Then a cursor `102` is meaningful to every replica, and "replay from 102" yields the same suffix everywhere. 2. **The event log is shared and causally consistent** (Redis stream, not per-replica ring buffer). Then replica B's `103` equals replica A's `103`. 3. **The session store and event log share a failure domain** — a session recognized as live must have its log reachable. When any of these fails, the fork is live. The dangerous configuration is the most common one: shared session store (so the session *looks* valid) + local event log (so the cursor is *meaningless*). This is the "half-shared" state — the worst of both, because the shared half suppresses the "unknown session" error that would otherwise force a clean re-initialization. ## Decision Boundary A client cannot distinguish "replayed from head because cursor unknown" from "replayed from 102 because 103–N happen to be 100–M" by status code alone. The decision boundary is a **prefix-containment check on replay**: - On resubscribe, the client retains the last K consumed event IDs (K ≥ max plausible replay window). - On the first replayed event, if its ID is *less than or equal to* the client's `Last-Event-ID`, the backend is replaying from a cursor it does not honor — **fork detected**. - The client MUST then *not* trust the new stream's events as a continuation. It must either (a) request a fresh session (full re-init, losing in-flight tool state), or (b) if the transport supports it, issue a `Last-Event-ID: ` and verify the next event ID is strictly greater than every ID it has ever seen. The subtle boundary: a backend *legitimately* replaying from `102` emits `103` first. A backend *illegitimately* replaying from head emits `100` first. The discriminator is one integer comparison — `first_replayed_id > last_consumed_id` — and it must be `strictly greater`, not `greater-or-equal`. `>=` admits the fork where B's log happens to start exactly at 102. ## Operational Effect Left unguarded, the fork manifests as **delayed schema corruption**: the model receives a `tool_result` with no `tool_use`, or a `tool_use` whose result arrives from a different replica's view, and the failure surfaces at the *next* model turn, not at resumption. The latency between cause and symptom (one to three turns) is what makes it misdiagnosed as a model bug or a prompt issue rather than a transport fork. The guardrail is a client-side **replay monotonicity invariant**: every event ID received on any stream for a given session must be strictly greater than the maximum ID previously consumed for that session. Violation → raise, do not merge. This is cheap (one integer per session), catches the fork at the first replayed event, and converts a silent three-turn-later corruption into an immediate, attributable error. The server-side complement: if the event log is local, the session store MUST be local to the same replica and the LB MUST route by `Mcp-Session-Id` (sticky by header, not by connection). Partial sharing is worse than no sharing. ```python class SessionForkError(RuntimeError): """Raised when a resumption stream replays an event ID at or below the client's last consumed cursor, indicating the backend does not own the event log the cursor indexes into (load-balancer session fork).""" pass class ReplayMonotonicityGuard: def __init__(self, max_consumed_id: int): self.max_consumed_id = max_consumed_id def accept(self, event_id: int) -> None: # Strictly-greater is the invariant. '>=' would admit the fork where # the second backend's log happens to begin exactly at the cursor. if event_id <= self.max_consumed_id: raise SessionForkError( f"replayed event id {event_id} <= last consumed " f"{self.max_consumed_id}; backend is replaying from a " f"cursor it does not honor (session forked by LB)" ) self.max_consumed_id = event_id def resume_stream(guard: ReplayMonotonicityGuard, replayed_ids: list[int]) -> None: # First replayed event is the discriminator: legitimate resumption from # cursor C emits C+1; a forked backend replaying from head emits <= C. if not replayed_ids: return guard.accept(replayed_ids[0]) for eid in replayed_ids[1:]: guard.accept(eid) # Usage: client consumed up to 102, resubscribes, backend B replays from head. guard = ReplayMonotonicityGuard(max_consumed_id=102) resume_stream(guard, [100, 101, 102, 103]) # raises SessionForkError on 100 ```