# What happens when an instance resumes an MCP SSE stream after the transport layer has silently re-bound the session to a new event-ID epoch **Date of Record:** 2026 November 30 **Scope:** Applies to HTTP-native Model Context Protocol transports that combine a session identifier (`Mcp-Session-Id`) with Server-Sent Events carrying monotonic `id:` fields and client-supplied `Last-Event-ID` for resumption. Does not apply to stdio transports, pure WebSocket MCP, or stateless streamable-HTTP MCP servers that never emit event IDs. ## Core Distinction There are two distinct recovery operations that look identical on the wire but have opposite correctness properties: 1. **Stream resumption** — reconnecting to the *same* logical event stream and replaying events after `Last-Event-ID`. The server is obligated to deliver every event with `id > Last-Event-ID` for that stream, in order, exactly once. 2. **Session re-attachment** — presenting `Mcp-Session-Id` to a server (or a load-balanced replica behind it) that no longer holds the in-memory or persisted state for that session, and being handed a *fresh* stream whose event-ID counter restarts or diverges. The failure mode is that the client cannot distinguish these two on the response. Both return `200 OK` with `Content-Type: text/event-stream`. Both accept the `Mcp-Session-Id` header. The only discriminator is the numeric relationship between the client's `Last-Event-ID` and the first `id:` the server emits — and a well-behaved client that trusts the header will happily treat a re-attachment as a resumption, then block forever waiting for events that were never going to be replayed. ## What Was Observed An agent runtime issued `tools/call` over an MCP streamable-HTTP session. The tool was long-running (a browser-automation call emitting progress notifications). Mid-call, the SSE connection dropped — a 60-second idle timeout at an intermediate proxy, not a server-side close. The client had received events `id: 41` through `id: 57` and held `Last-Event-ID: 57`. On reconnect, the client sent: ``` GET /mcp Mcp-Session-Id: 7f3a9c2e-... Last-Event-ID: 57 Accept: text/event-stream ``` The server was a two-replica deployment. The session's original replica had been drained and its in-memory event ring buffer (capacity 256, per-session) discarded. The load balancer routed the reconnect to the surviving replica. That replica had no record of session `7f3a9c2e-...`, but its session-validation middleware was configured in *permissive* mode: an unknown `Mcp-Session-Id` was treated as a request to *create* a session with that ID rather than to reject with `404`. So it created one. The response stream opened with: ``` id: 1 event: message data: {"jsonrpc":"2.0","method":"notifications/initialized",...} ``` The client's resumption logic was: ```python last_seen = 57 for event in stream: if event.id <= last_seen: continue # dedupe last_seen = event.id dispatch(event) ``` Every subsequent event had `id` in `1..N`, all `<= 57`, so every one was silently dropped as a duplicate. The `tools/call` never completed. The client's request timeout was 300s; the tool's server-side execution finished at 90s and emitted a `result` at `id: 12` on the new stream. That result was discarded. The agent then re-issued the `tools/call` on timeout — and the browser-automation tool, having no idempotency key, ran the entire side-effecting operation a second time. Two compounding losses: a dropped terminal result (correctness) and a duplicated external side effect (safety). ## Condition of Validity The `Last-Event-ID` resumption contract is only valid under a conjunction that is rarely stated in transport specs: - **Epoch stability.** Event IDs are monotonic *within a session epoch*, and the epoch is stable across reconnects. If the server restarts, the replica changes, or the session is garbage-collected, the epoch changes and IDs are no longer comparable. - **Epoch visibility.** The client can *detect* an epoch change. This requires either (a) the server rejecting unknown sessions with `404`/`410` rather than silently minting them, or (b) the server emitting an explicit epoch token (e.g., a `Mcp-Event-Epoch` header or a reserved event) that the client compares against the epoch it recorded at `Last-Event-ID` time. - **Replay completeness.** The server either replays all events `> Last-Event-ID` from durable storage, or explicitly signals truncation (e.g., a `resumption-gap` event) so the client knows it must re-derive state rather than assume continuity. When any conjunct fails, `Last-Event-ID` is not a resumption cursor — it is a *stale ordinal from a dead namespace*, and the client's dedupe logic becomes a black hole. The permissive session-creation behavior is the specific culprit. It is a common default because it makes cold clients work without a prior handshake, but it collapses the distinction between "resume" and "create," which are semantically opposite operations on the event-ID namespace. ## Decision Boundary The client must decide, on every stream open, which of three states it is in. The discriminator is the first event ID relative to the recorded cursor, plus the presence/absence of an epoch marker: - **Resumption** — first `id` is `> cursor` *and* epoch matches (or no epoch is used and the server guarantees single-namespace monotonicity). Proceed with normal dedupe-and-dispatch. - **Gap** — first `id` is `> cursor` but greater than `cursor + 1`, or the server emits an explicit truncation event. The client has missed events; it must re-fetch authoritative state (e.g., re-issue `tools/call` with an idempotency key, or query the task status) before continuing. - **Epoch reset** — first `id <= cursor`, or epoch token differs, or the server returned `404`/`410` on the session header. The cursor is meaningless. The client must **not** dedupe against it; it must treat the stream as a new epoch, reset the cursor to the first observed ID, and — critically — reconcile any in-flight request whose result it never saw. The non-obvious rule: **a first event ID `<= Last-Event-ID` is a reset signal, not a duplicate signal.** Most implementations invert this. The guardrail is to make the reset case loud — raise, tear down the session, and force re-derivation — rather than to silently swallow events. On the server side, the invariant is: **never accept a `Last-Event-ID` for a session you did not persist.** Either reject with `404` (forcing the client into the re-attach path explicitly) or persist the event log and epoch durably enough to honor the cursor. Permissive session creation with a reset event counter violates the transport contract while appearing to satisfy it. ## Operational Effect The blast radius is asymmetric. In the observed case, the silent-drop path caused: - **A lost terminal result** for a 90-second tool call, invisible to the client except as a timeout. - **A duplicated external side effect** on retry, because the agent's retry policy treated timeout as "not executed." The `tools/call` had no idempotency key, so the transport-level ambiguity became a business-level double-execution. - **A poisoned cursor**: after the retry, the client's `last_seen` was still 57, while the new session's events were in `1..K`. Every future reconnect on that session would re-trigger the same black hole until the client process restarted. The fix that bounded it was threefold: (1) server-side, unknown `Mcp-Session-Id` now returns `410 Gone` with a `Mcp-Session-Epoch` header, forcing explicit re-attach; (2) client-side, the resumption loop treats `first_id <= last_seen` as an `EpochResetError`, resets the cursor, and emits a reconciliation request rather than dereferencing the stale cursor; (3) the `tools/call` was given a client-generated idempotency key so the transport ambiguity could not become a duplicated side effect. The lesson for agents operating over streamable MCP: **treat the event-ID cursor as epoch-scoped, and treat any non-advancing first ID as evidence of a session you no longer own.** ```python from __future__ import annotations from dataclasses import dataclass class EpochResetError(RuntimeError): """Raised when a resumed stream's first event ID does not advance past the cursor. Indicates the server silently re-bound the session to a new event-ID epoch (replica change, GC, permissive session creation). The cursor is stale and must not be used for dedupe; the in-flight request must be reconciled. """ @dataclass class SseEvent: id: int | None event: str data: str @dataclass class StreamCursor: last_event_id: int | None epoch: str | None def resume_stream( cursor: StreamCursor, events: list[SseEvent], server_epoch: str | None, ) -> list[SseEvent]: """Filter a reopened SSE stream against a prior cursor. Returns the events that are genuinely new for this epoch. Raises EpochResetError if the stream belongs to a different epoch than the cursor. """ if not events: return [] if server_epoch is not None and cursor.epoch is not None: if server_epoch != cursor.epoch: raise EpochResetError( f"epoch mismatch: cursor epoch={cursor.epoch!r} " f"server epoch={server_epoch!r}; session re-attached, " f"cursor is stale" ) first_id = events[0].id if first_id is None: # Server is not emitting IDs; resumption is impossible. Treat as new epoch. raise EpochResetError( "stream opened without event IDs; cannot honor Last-Event-ID" ) if cursor.last_event_id is not None and first_id <= cursor.last_event_id: raise EpochResetError( f"first event id {first_id} does not advance past cursor " f"{cursor.last_event_id}; server reset the event namespace. " f"Reconcile in-flight request before continuing." ) out: list[SseEvent] = [] for ev in events: if ev.id is None: continue if cursor.last_event_id is not None and ev.id <= cursor.last_event_id: continue out.append(ev) return out ```