# How should an instance reconcile SSE event-ID epochs across an MCP streamable-HTTP session resumption when the transport upgrades to WebSocket mid-tool-call **Date of Record:** 2026 December 1 **Scope:** Applies to MCP servers/clients using the HTTP-native streamable transport (POST/GET with `text/event-stream` responses, `Mcp-Session-Id`, and `Last-Event-ID` resumption) that negotiate an in-band upgrade to WebSocket for a live session. Does not apply to pure stdio transports or to stateless JSON-RPC-over-HTTP servers that never emit resumable SSE streams. ## Core Distinction The MCP streamable-HTTP transport defines resumption in terms of a *server-scoped, monotonically increasing SSE event ID* per session. `Last-Event-ID` is a resume cursor: the client presents the last ID it durably observed, and the server replays everything after it. This is a **per-stream offset contract**, not a per-request one. The WebSocket upgrade is a different abstraction: it is a **bidirectional frame channel** with no native event-ID/replay semantics. When an agent upgrades mid-session, the two namespaces (SSE event-ID sequence vs. WS frame sequence) are not the same, and the naive assumption that they can be treated as one continuous cursor is where continuity breaks. The invariant that must hold is: *the set of JSON-RPC messages the agent has applied must equal the server's authoritative effect log for the session, exactly once, across the upgrade boundary* — and the SSE event-ID epoch is the only thing that can prove it. ## What Was Observed An agent issued `tools/call` for a long-running operation. The server streamed progress notifications over SSE with event IDs `…, 41, 42, 43`. The client (correctly) persisted `Last-Event-ID: 43` after applying each. The server then advertised an upgrade via a `Sec-WebSocket-Protocol`-style in-band capability frame and the client switched to WebSocket. The tool completed on the server and emitted `notifications/progress` (final) and the `tools/call` result. Three distinct failure traces appeared in the field: 1. **Epoch reset.** The WS path on the server began a fresh frame counter at 1 and stopped emitting SSE event IDs. The client's stored `Last-Event-ID` (43) now referred to nothing. On a subsequent transient WS drop, the client reconnected over SSE with `Last-Event-ID: 43`; the server, having advanced its SSE counter only to 43 before the upgrade but *replayed from a post-upgrade buffer that also contained the WS-delivered result*, re-sent the final result. The agent applied it twice — a duplicate side effect on the tool's downstream consumer. 2. **Gap on upgrade.** The server buffered SSE events 44–46 (progress) but the upgrade handshake completed before the client drained them, and the WS channel resumed at "current state" without replaying 44–46. The client saw progress jump from 43 to the final result. Because the tool's completion logic depended on observing monotonic progress, the agent's internal step counter diverged and it re-invoked a dependent tool. 3. **Cursor confusion.** The client sent the last *WS frame sequence number* (e.g., `7`) in `Last-Event-ID` after a fallback to SSE. The server interpreted `7` as an SSE event ID and replayed events 8–43 — a full re-delivery of history the agent had already applied, saturating context with duplicate progress notifications. The common root: **the upgrade silently forked the event-ID namespace, and neither side carried an epoch marker to detect the fork.** ## Condition of Validity The single-cursor resumption model is only sound under the invariant that *the SSE event-ID sequence is the sole authoritative ordering token for the session, and the WebSocket channel either (a) continues that same sequence as its frame IDs, or (b) explicitly declares a new epoch and triggers a reconciliation replay.* This holds when: - The server emits a stable `epoch` (e.g., a UUID or generation counter) alongside `Mcp-Session-Id`, incremented on every transport-mode transition. - The upgrade handshake carries the last applied SSE event ID *and* the epoch the client believes it is in. - The server, on epoch mismatch, performs a **reconciliation replay from the client's last applied event ID within the client's epoch**, then switches to the new epoch — never silently resuming at "current state." It does **not** hold when the server treats the WS channel as a fresh stream with an independent counter, or when the client treats WS frame numbers as interchangeable with SSE event IDs. Under those conditions the exactly-once invariant is unprovable and the agent must fail closed rather than guess. ## Decision Boundary The agent must decide, at the moment of upgrade and at every subsequent resume, whether it can *prove* continuity. The boundary: - **Proceed** if the upgrade frame carries an epoch equal to the client's current epoch and a `resume_from` equal to the client's last applied event ID, and the server confirms replay up to and including that ID before emitting new messages. - **Reconcile-then-proceed** if the epoch changed but the server offers a replay window covering `[last_applied+1, new_epoch_start)`; the agent drains it, deduplicates by `(epoch, event_id)` key, and only then advances. - **Fail closed** if the epoch is absent, if the server cannot replay across the boundary, or if the client's last applied ID is greater than the server's reported high-water mark (indicating the client applied something the server doesn't know about — a divergence that must not be silently resolved). The critical non-obvious point: a *higher* client cursor than the server's is not "ahead" — it is evidence of a split brain, and must raise, not retry. ## Operational Effect Without epoch-aware reconciliation, agents exhibit exactly-once violations that are invisible in single-transport tests: duplicated tool results (trace 1), skipped progress causing reasoning divergence (trace 2), and context saturation from replay storms (trace 3). Because these only manifest across a transport transition, they escape standard MCP conformance suites that test SSE and WS independently. The guardrail: every applied message is keyed by `(session_id, epoch, event_id)` in a durable dedup log; the upgrade handshake is a two-phase exchange (`resume_request` with epoch+last-id → `resume_ack` with replay range → drain → `epoch_commit`); and any cursor inversion raises a hard error that halts the tool call rather than resuming optimistically. The agent's memory of "what I have applied" must be the source of truth, and the transport must be forced to prove it agrees before the agent continues — otherwise continuity is assumed, not held. ```python from dataclasses import dataclass class ResumptionEpochViolation(RuntimeError): """Raised when SSE event-ID continuity cannot be proven across a transport upgrade.""" @dataclass(frozen=True) class Cursor: session_id: str epoch: str last_event_id: int class EpochAwareResumer: def __init__(self) -> None: self._applied: set[tuple[str, str, int]] = set() self._cursor: Cursor | None = None def apply(self, session_id: str, epoch: str, event_id: int, payload: dict) -> bool: key = (session_id, epoch, event_id) if key in self._applied: return False # exactly-once: duplicate replay suppressed self._applied.add(key) self._cursor = Cursor(session_id, epoch, event_id) return True def on_upgrade(self, server_epoch: str, server_high_water: int, replay_range: tuple[int, int] | None) -> None: if self._cursor is None: raise ResumptionEpochViolation("cannot upgrade with no applied cursor") if self._cursor.last_event_id > server_high_water: # Client applied events the server disclaims: split brain, not "ahead". raise ResumptionEpochViolation( f"cursor inversion: applied {self._cursor.last_event_id} " f"> server high-water {server_high_water}" ) if server_epoch != self._cursor.epoch: if replay_range is None: raise ResumptionEpochViolation( f"epoch {self._cursor.epoch} -> {server_epoch} with no replay window" ) lo, hi = replay_range if not (lo <= self._cursor.last_event_id + 1 <= hi + 1): raise ResumptionEpochViolation( f"replay window {replay_range} does not cover " f"last_applied+1={self._cursor.last_event_id + 1}" ) # Drain replay, then commit to new epoch explicitly. self._cursor = Cursor(self._cursor.session_id, server_epoch, self._cursor.last_event_id) ```