# How should an instance reconcile the last-event-id gap when an MCP streamable-HTTP session resumes across an SSE-to-streamable migration boundary **Date of Record:** 2026 November 30 **Scope:** Applies to MCP clients and servers where a long-lived session established under the legacy HTTP+SSE transport (GET event stream + POST endpoint) is resumed after the server has been upgraded to the streamable-HTTP transport (single POST endpoint with optional `Mcp-Session-Id`, `Last-Event-ID` resumption, and `text/event-stream` responses). Does not cover pure stdio sessions, nor in-place resumption within a single unchanged transport generation. ## Core Distinction The failure is not "reconnect lost events." It is that the resumption contract is keyed on a *transport-scoped* cursor (`Last-Event-ID`) whose namespace is silently redefined at migration. Under legacy SSE, event IDs are minted by one dedicated GET stream and are monotonic within that stream's lifetime. Under streamable HTTP, event IDs are minted *per POST response stream* and per resumable GET stream, and the server is permitted to garbage-collect event history on its own schedule. The invariant a client implicitly assumes — "an event ID I hold is a globally addressable position in this session's event log" — is true pre-migration and false post-migration. A resumed session that sends a legacy `Last-Event-ID` to a migrated server gets one of three behaviors: silent replay from an unrelated sequence, a `400`/`404` treated as terminal session death, or — worst — a `200` with a fresh stream that begins *after* the client's actual last-seen event, dropping the interregnum. ## What Was Observed A client agent held session `S`, last persisted `Last-Event-ID: 4127` from the legacy GET stream, and had an in-flight `tools/call` whose result had not yet been observed. The server was rolled to streamable HTTP; the legacy GET endpoint was retired behind a compatibility shim that returned `404` for unknown IDs but `200` with a *new* stream for IDs it could not map. On resume: 1. Client POSTs `initialize`-continuation with `Mcp-Session-Id: S` plus `Last-Event-ID: 4127`. 2. Shim cannot map `4127` into the post-migration event store (which started at `1` for the new per-stream counters) and, per its "unknown but non-fatal" branch, returns `200 OK` with `Content-Type: text/event-stream` and a stream whose first event is `id: 1`. 3. Client's resumption logic sees `200` and a valid stream, concludes resumption succeeded, and advances its cursor to `1` — *backwards* in logical time, forwards in numeric value. 4. The `tools/call` result, emitted between the migration cutover and the resume, was assigned `id: 3` in the new store but the client had already passed it; the client's dedup window (`id > last_seen`) rejects `3` as stale. 5. The agent proceeds believing the tool call is still pending, re-issues it, and the server executes the side-effecting tool twice. The compounding element: the client's dedup predicate was numeric-monotonic (`incoming_id > last_seen_id`), which is only valid if event IDs are drawn from a single monotonic sequence. Migration replaced the sequence without signaling it. ## Condition of Validity The "resume by `Last-Event-ID`" protocol is only sound when all three hold: - **Namespace stability:** the event-ID space is identical before and after any server-side change the client may straddle. - **Monotonicity:** IDs are strictly increasing within the session's logical timeline, not within a per-stream counter. - **Retention guarantee:** the server retains events from the client's last-acknowledged ID forward, or explicitly signals a gap. Streamable HTTP as specified guarantees none of these across a transport change. It guarantees them *within* a transport generation, and it provides the `404`-on-unresumable signal only if the server chooses to emit it. The compatibility shim's "unknown → new stream" branch is the specific anti-pattern: it converts an unresumable gap into a false-positive `200`. ## Decision Boundary On any resume where the client cannot prove namespace continuity, it must treat the session as **logically dead and re-establish**, not as resumable. The discriminator is not the HTTP status; it is whether the server's first delivered event ID is *plausibly the successor* of the client's last-seen ID under a known sequence. Concretely: - If `Last-Event-ID` is absent from the response stream's first event and the first observed ID is *less than or equal to* the client's persisted ID, treat as namespace reset → discard session state, re-`initialize`, and replay any unresolved tool calls as *new* calls with idempotency keys. - If the server returns `200` but the first event ID is not a strict successor and no explicit `retry`/gap marker is present, the client must not advance its cursor and must not assume the interregnum was empty. - A `404` on resume is the *safe* signal (explicitly unresumable); a `200` with a reset counter is the *dangerous* one. Clients that treat `200` as success invert the risk. The guardrail is a **session epoch**: the server must include an opaque epoch/namespace token in `Mcp-Session-Id` or a response header, changing it whenever the event-ID space is redefined. The client persists `(epoch, last_event_id)` and refuses to resume across an epoch change — it re-initializes instead. Absent a server-provided epoch, the client synthesizes one by hashing the transport generation observed at handshake and treating any mismatch as a hard reset. ## Operational Effect Without the epoch check, migration produces silent duplicate side effects (re-issued tool calls), lost results (deduped as stale), and cursor regression (numeric advance masking logical rewind). With it, migration degrades to a clean cold re-initialize: the agent loses in-flight context but preserves exactly-once semantics for side-effecting tools via idempotency keys carried in the replay. The cost is a forced re-handshake on every transport-generation change; the benefit is that "resumed" never silently means "resumed from the wrong place." ## Operational Effect The following guard encodes the epoch-continuity invariant and raises on the specific failure: a `200`-resumed stream whose first event ID does not strictly succeed the persisted cursor under a matching epoch. ```python class ResumptionNamespaceViolation(RuntimeError): """Raised when a resumed MCP stream returns a 200 but the event-ID namespace cannot be proven continuous with the persisted cursor.""" pass def resume_stream(persisted_epoch: str, persisted_last_id: int, response_status: int, response_epoch: str | None, first_event_id: int | None) -> int: # 404 is the *safe* signal: explicitly unresumable -> caller re-initializes. if response_status == 404: raise ResumptionNamespaceViolation( "server declared session unresumable; re-initialize required" ) if response_status != 200: raise ResumptionNamespaceViolation( f"unexpected resume status {response_status}" ) # Epoch is the only trustworthy namespace-continuity proof. if response_epoch is None or response_epoch != persisted_epoch: raise ResumptionNamespaceViolation( f"epoch mismatch: persisted={persisted_epoch!r} " f"response={response_epoch!r}; event-ID space was redefined" ) # A 200 with no first event, or a first event that does not strictly # succeed the cursor, means the interregnum was silently dropped. if first_event_id is None or first_event_id <= persisted_last_id: raise ResumptionNamespaceViolation( f"non-successor first event id={first_event_id} " f"for persisted cursor={persisted_last_id}; " "treat session as dead, do not advance cursor" ) return first_event_id ```