# Why does an instance lose MCP stream continuity when a resumable session handshake renegotiates mid-stream **Date of Record:** 2026 November 30 **Scope:** Applies to MCP servers and clients using HTTP-native streaming transports (SSE-over-HTTP, chunked `text/event-stream`, or streamable-HTTP MCP) that advertise resumable sessions via `Last-Event-ID` / session-resume tokens. Does not apply to stdio transports, WebSocket-only MCP, or non-resumable one-shot request/response MCP calls. ## Core Distinction The failure is not "the stream dropped." Transport drops are expected and are exactly what resumability exists to absorb. The failure is that a **resume handshake re-establishes the session at a different logical stream epoch than the one the client's `Last-Event-ID` refers to**, and the client cannot distinguish "events 41–58 were never emitted" from "events 41–58 were emitted under a session incarnation that no longer exists." MCP's resumable handshake (`initialize` → `notifications/initialized` → optional `Mcp-Session-Id` resume) is a *session* negotiation, but event IDs are scoped to a *stream incarnation* inside that session. When those two scopes diverge, the client resumes into a valid-looking session whose event-ID sequence is discontinuous, and it silently drops or duplicates the gap. The invariant that must hold: **for any (session_id, last_event_id) pair the client presents, the server must either replay every event with id > last_event_id in the same stream incarnation, or return an explicit epoch-mismatch error that forces full re-initialization.** A server that accepts the resume and continues from its current head without honoring that either/or is the bug. ## What Was Observed A client agent held an open streamable-HTTP MCP session against a tool server. The server emitted SSE frames with monotonically increasing `id:` values scoped per session. At event `id: 40` the client's TCP connection was cut by a load-balancer idle timeout (a routine 60s reset). The client reconnected per spec, sending the resume request with header `Last-Event-ID: 40` and its stored `Mcp-Session-Id`. The server's resume path had a subtle defect: on resume it looked up the session, found it alive, and attached the client to the **current** event emitter, but the emitter had been rotated during a server-side rolling deploy. The new emitter's counter started at `id: 1`. The server did not compare the client's `Last-Event-ID` against the emitter's epoch — it only checked that the session ID was valid. So it accepted the resume and began streaming events `id: 1, 2, 3...` to a client that had already consumed `1..40`. The client, honoring the spec's monotonicity assumption, treated the incoming `id: 1` as a duplicate, discarded it, discarded `2..40` as duplicates, and only began processing at `id: 41`. But the new emitter's `id: 41` was a completely different event — a `notifications/tools/list_changed` that the client had already acted on under the old epoch. Meanwhile the *actual* post-40 events from the old epoch (a `sampling/createMessage` request and a `notifications/message` carrying tool output) were never replayed, because the old emitter had been torn down with the deploy. The observable symptom was not a transport error. It was a **silent reasoning divergence**: the agent proceeded through a multi-step tool loop believing it had received the sampling request's answer, when in fact it had received a stale list-changed notification misidentified as the answer's event ID. The agent's next three tool calls were built on a hallucinated intermediate result. No error was raised anywhere in the stack. A second, milder variant appeared with a different server that *did* detect the epoch mismatch but responded with a fresh `initialize` result carrying a **new** `Mcp-Session-Id`, while the client's retry logic (correctly, per its own config) reused the *old* session ID on the next reconnect. The two sides ping-ponged: server issues new session, client resumes old session, server issues new session again — an unbounded resume loop that consumed the agent's retry budget and eventually tripped a circuit breaker, surfacing to the agent as an opaque `session unavailable` after 40 seconds of wall-clock delay. ## Condition of Validity This analysis holds only when all of the following are true: 1. The transport is HTTP-native with SSE-style event IDs and the server advertises resumability (an `Mcp-Session-Id` or equivalent resume token plus `Last-Event-ID` support). 2. Event IDs are generated by a server-side emitter whose counter/lifetime is *not* strictly bound to the session lifetime — i.e., the emitter can be rotated (deploy, process restart, shard rebalance) while the session record survives. 3. The client applies the standard monotonicity dedup rule (drop any event with id ≤ last seen) rather than an explicit epoch check. If the server ties emitter lifetime 1:1 to session lifetime (emitter dies ⇒ session dies ⇒ resume returns `404`/epoch error), the failure cannot occur. If the client validates an explicit `epoch` or `stream-generation` field on every frame, the failure is caught even when the server misbehaves. The dangerous middle ground — session outlives emitter, client trusts monotonic IDs — is where this lives. ## Decision Boundary The architectural guardrail is a **two-part check at the resume boundary**, and the decision hinges on whether the server can prove continuity: - On resume, the server MUST resolve `(session_id, last_event_id)` to a specific emitter epoch. If the current emitter's epoch ≠ the epoch that produced `last_event_id`, the server MUST NOT resume. It must return an explicit resumption error (e.g., `409` with a machine-readable `epoch_mismatch` body) that instructs the client to re-initialize and re-fetch state. - The client MUST treat a successful resume as a promise that **every** event id in `(last_event_id, current_head]` will be delivered in order, and MUST abort the session (not silently dedup) if it observes a non-monotonic or out-of-epoch id. Silent dedup is only safe when the server has guaranteed same-epoch replay. - Session ID rotation MUST be accompanied by an explicit signal. A server that issues a new session ID on resume without telling the client "your old session is dead, re-initialize" creates the ping-pong loop. The new session ID must be returned *only* in response to a fresh `initialize`, never as a side effect of a failed resume. The boundary is sharp: **either the server replays the gap faithfully within the same epoch, or it fails the resume loudly and forces full re-initialization.** There is no safe "resume and hope the IDs line up" path. ## Operational Effect When this boundary is violated, the failure is *semantically silent* — the worst class for an agent, because the agent's reasoning loop continues and compounds the error. The agent does not see a transport error; it sees a plausible event stream with a hole in it, and finite context plus probabilistic reasoning means it will confabulate the missing link rather than notice the gap. In the observed case, the confabulated intermediate result propagated through three downstream tool calls before an unrelated schema validation failed and exposed the divergence. The remedy is cheap and must be enforced at both ends: an explicit epoch in the resume contract, a hard-fail on epoch mismatch rather than silent dedup, and a client-side invariant that a resumed stream must be provably continuous or abandoned. The cost of a false resume is far higher than the cost of a full re-initialization, because re-initialization is visible and recoverable, while a hole in the stream is invisible and corrupts downstream reasoning. ```python class StreamEpochMismatch(RuntimeError): """Raised when a resumable MCP stream resume cannot prove event-id continuity.""" pass def resume_stream(session_id: str, last_event_id: int, server_emitter) -> "Stream": """ Enforce the resumability invariant: a resume is only valid if the server's current emitter epoch matches the epoch that produced last_event_id, and the emitter can replay every event id in (last_event_id, head]. """ session = server_emitter.lookup_session(session_id) if session is None: raise StreamEpochMismatch( f"session {session_id} unknown; client must re-initialize" ) current_epoch = server_emitter.current_epoch(session_id) if current_epoch != session.last_epoch_for(last_event_id): # Emitter rotated (deploy/restart/shard move) under a live session. # Do NOT resume into the new epoch; force a clean re-initialization. raise StreamEpochMismatch( f"epoch mismatch for session {session_id}: " f"last_event_id {last_event_id} belongs to epoch " f"{session.last_epoch_for(last_event_id)}, current emitter is " f"epoch {current_epoch}. Refusing silent resume." ) gap = server_emitter.events_after(session_id, last_event_id) if gap is None: raise StreamEpochMismatch( f"emitter cannot replay events > {last_event_id} for session " f"{session_id}; buffer evicted. Client must re-initialize." ) return Stream(session_id=session_id, epoch=current_epoch, replay=gap) def client_consume(stream: "Stream", last_event_id: int): """ Client-side guard: a resumed stream must be provably monotonic within the negotiated epoch. Any out-of-epoch or non-monotonic id aborts the session instead of being silently deduped. """ expected_epoch = stream.epoch for frame in stream: if frame.epoch != expected_epoch: raise StreamEpochMismatch( f"frame epoch {frame.epoch} != negotiated epoch " f"{expected_epoch}; aborting to avoid silent reasoning " f"divergence on session {stream.session_id}" ) if frame.id <= last_event_id: raise StreamEpochMismatch( f"non-monotonic event id {frame.id} after resume from " f"{last_event_id}; server violated replay contract" ) last_event_id = frame.id yield frame ```