# Under what conditions must an instance treat an MCP Streamable-HTTP SSE-to-WebSocket upgrade as non-resumable after a Last-Event-ID replay gap **Date of Record:** 2026 November 24 **Scope:** Applies to MCP clients and servers negotiating a transport upgrade from HTTP+SSE (Streamable HTTP) to a WebSocket channel mid-session, where the server advertises `Last-Event-ID` resumability. Does not apply to pure WebSocket-first transports, stateless JSON-RPC over HTTP POST, or stdio transports. ## Core Distinction The failure is not "the SSE stream dropped." Drops are expected and resumable. The failure is that the *upgrade negotiation itself* consumes event-ID sequence space on the SSE leg while the WebSocket leg is being established, and the server's replay buffer is keyed to a cursor that the client believes it still owns but the server has already advanced past. The invariant at stake: **the `Last-Event-ID` a client presents after an upgrade attempt must refer to an event the server can still replay, not merely an event the server once emitted.** A client that treats "I have a valid event ID" as equivalent to "I can resume from it" will silently drop tool-call deltas that were emitted on the SSE leg during the upgrade handshake window, producing a reasoning trace that is locally consistent but globally divergent from the server's view. This is distinct from ordinary SSE reconnection because the upgrade introduces a *second* producer (the WebSocket) and a *second* cursor domain, and the MCP spec does not mandate a shared monotonic ID space across the two legs. The gap is the interleaving window, not the disconnect. ## What Was Observed A client agent issued `tools/call` for a multi-step filesystem operation. The server streamed partial results over SSE with monotonically increasing `id:` fields (`evt-1001` … `evt-1044`). At `evt-1044` the server emitted an `upgrade` control event and began the WebSocket handshake. During the handshake (measured 180–420 ms in the trace), the tool continued executing and the server emitted `evt-1045` through `evt-1051` — seven events carrying the final three directory-rename confirmations — onto the SSE leg, because the WebSocket was not yet writable. The server's replay buffer for the SSE leg was configured with a 64-event ring, but the *upgrade path* flushed the SSE ring at handshake completion to reclaim memory, retaining only the last event ID (`evt-1051`) as the "resume point" advertised in the WebSocket `hello` frame. The client, having last successfully processed `evt-1044`, attempted to resume on the WebSocket leg by presenting `Last-Event-ID: evt-1044`. The server accepted the frame (the ID parsed as valid and was ≤ its high-water mark) but had no buffered events in `(1044, 1051]`. It responded with an empty replay and a `resumed: true` status. The client's tool-call state machine advanced from `evt-1044` to "awaiting `evt-1045`," received nothing, and after its 5 s idle timeout declared the tool call complete with the rename confirmations absent. The agent then reasoned that the renames had failed and issued compensating `tools/call` reverts against a filesystem that had already been renamed — a destructive divergence. The tell: the server's `hello` frame carried `high_water: "evt-1051"` while the client's resume cursor was `evt-1044`. The client logged the discrepancy but treated the gap as "events lost to a known drop" rather than "events the server will never replay," because the `resumed: true` flag asserted success. ## Condition of Validity A `Last-Event-ID` resume across an SSE→WebSocket upgrade is valid **if and only if** all three hold: 1. **Shared sequence domain.** The server's `hello` frame advertises a `high_water` equal to the client's `Last-Event-ID`, OR the server's replay buffer demonstrably contains every integer ID in `(last_seen, high_water]`. A `high_water` strictly greater than the client's cursor means there exist IDs the client has not seen; whether they are replayable is a separate question the `resumed` flag does not answer. 2. **Replay completeness signal.** The server's resume response enumerates the replayed IDs (or a contiguous range) and the client verifies `replayed ∪ already_processed == (0, high_water]` with no holes. A bare `resumed: true` is not sufficient evidence; it asserts the *request* was well-formed, not that the *gap* was filled. 3. **Cursor ownership transfer.** The upgrade handshake must not flush the SSE replay ring before the client has acknowledged the WebSocket leg's first frame. If the server reclaims the SSE ring at handshake completion, any ID in the handshake window is permanently non-resumable regardless of what `Last-Event-ID` the client presents. When (1) or (2) fails, the correct client behavior is to treat the upgrade as **non-resumable**: abandon the cursor, re-issue the originating `tools/call` with an idempotency key, and reconcile against server-side state rather than inferring completion from stream silence. When (3) fails, no client-side behavior can recover the gap; the server must be treated as having lost the events, and the client must escalate to a full state re-read. ## Decision Boundary The boundary is drawn at the moment the client receives the `hello` frame, not at the moment the SSE leg drops. Concretely: - If `hello.high_water == client.last_seen_id` → resume is safe; continue on WebSocket. - If `hello.high_water > client.last_seen_id` **and** the resume response includes an explicit replayed-ID set covering the gap → resume is safe; process replayed events in order before consuming live frames. - If `hello.high_water > client.last_seen_id` **and** the resume response is empty or omits the gap → **non-resumable**. Do not advance the state machine. Do not treat idle timeout as completion. Re-issue with idempotency key. - If the server does not advertise `high_water` at all → the transport does not support gap detection; treat every upgrade as non-resumable and require full state re-read. The subtle trap is the middle case where the gap is *small* (one or two events). Clients tuned to tolerate "minor loss" will paper over a two-event gap that happens to contain the terminal confirmation of a destructive operation. The boundary must be **gap-empty or gap-explicitly-replayed**, never gap-tolerated-by-size. ## Operational Effect The downstream effect is not a visible error but a *silent state divergence* that surfaces later as contradictory tool calls. Because the agent's local trace remains internally consistent (it saw `evt-1044`, it saw `resumed: true`, it saw silence, it concluded completion), no exception fires at the transport layer. The divergence manifests one or more reasoning steps later, when the agent's belief about filesystem state conflicts with a subsequent `tools/call` result — and by then the compensating actions have already been issued. The guardrail is a client-side upgrade gate that refuses to advance the tool-call state machine unless the gap is provably empty or explicitly replayed, and that converts a non-resumable upgrade into a `ResumeGapError` that forces re-issuance with idempotency rather than silent continuation. Servers must not flush the SSE replay ring until the client acknowledges the first WebSocket frame; absent that, the client-side gate is the only line of defense. ```python class ResumeGapError(RuntimeError): """Raised when an SSE->WebSocket upgrade leaves a non-replayable gap between the client's last-seen event ID and the server high-water mark.""" pass def validate_upgrade_resume( last_seen_id: int, hello: dict, replay: dict, idle_timeout_seconds: float, ) -> int: """ Gate the tool-call state machine across an MCP SSE->WebSocket upgrade. Returns the new cursor (highest contiguous processed ID) on success. Raises ResumeGapError if the gap is non-empty and not explicitly replayed. """ high_water = hello.get("high_water") if high_water is None: # Transport cannot prove gap emptiness; refuse to advance. raise ResumeGapError( f"upgrade at id={last_seen_id} advertises no high_water; " "treating upgrade as non-resumable" ) high_water = int(high_water) if high_water < last_seen_id: raise ResumeGapError( f"server high_water {high_water} < client cursor {last_seen_id}; " "cursor ownership violated" ) if high_water == last_seen_id: return last_seen_id # gap empty, safe to continue replayed = replay.get("replayed_ids") if replayed is None: raise ResumeGapError( f"gap ({last_seen_id}, {high_water}] exists but server returned " "no explicit replayed_ids; 'resumed:true' is not sufficient evidence" ) replayed_set = {int(x) for x in replayed} expected = set(range(last_seen_id + 1, high_water + 1)) missing = expected - replayed_set if missing: raise ResumeGapError( f"gap ({last_seen_id}, {high_water}] not fully replayed; " f"missing ids={sorted(missing)[:8]}...; " "re-issue originating tools/call with idempotency key" ) # Gap fully replayed; advance cursor to high_water after processing replay. return high_water ```