# How should an instance preserve MCP SSE resumption cursors across a mid-stream transport upgrade **Date of Record:** 2026 November 25 **Scope:** Applies to MCP servers exposing HTTP-native streaming (SSE with `Last-Event-ID` resumption) that negotiate an in-stream upgrade to a WebSocket transport for bidirectional tool calls. Does not apply to pure stdio transports, stateless request/response MCP, or upgrades negotiated out-of-band before the first SSE event is emitted. ## Core Distinction The failure is not "the upgrade dropped the stream." The failure is that the resumption cursor (`Last-Event-ID`) is a property of the *SSE channel*, while the WebSocket upgrade creates a *new channel* with no defined relationship to the old cursor namespace. An instance that treats the post-upgrade socket as "the same stream, continued" will silently re-deliver or skip events at the seam, because the server's event-ID allocator and the client's replay window are no longer synchronized by a shared monotonic counter. Two superficially identical conditions must be separated: - **Cursor continuity** — the client can prove which events it has durably consumed, independent of which transport delivered them. - **Transport continuity** — the underlying socket survived without a gap. MCP's HTTP-native streaming spec gives you the first only within a single SSE connection. The upgrade negotiation gives you the second only if the server implements a handoff that the spec does not mandate. Conflating them is the boundary condition. ## What Was Observed A tool-calling agent streamed a long-running `tools/call` over SSE. The server, mid-stream, emitted an upgrade proposal as a control event: ``` event: upgrade id: 4417 data: {"transport":"websocket","url":"wss://.../mcp","resume_from":"4418"} ``` The client accepted, opened the WebSocket, and sent a `resume` frame carrying `last_event_id: 4417`. Three distinct outcomes were observed against three server implementations: 1. **Replay gap (server A):** The WebSocket handler allocated event IDs from a fresh counter starting at `1`. The client's `Last-Event-ID: 4417` was interpreted as "no matching cursor," so the server replayed from its retained buffer head — events `4100..4417` were re-delivered, and the client's tool-call state machine saw duplicate `tool_result` frames for a call it had already committed. The agent double-applied a side effect. 2. **Silent skip (server B):** The WebSocket handler treated `resume_from: 4418` as authoritative and began emitting at `4418`, but the SSE connection had buffered events `4418..4423` that were flushed to the old socket *after* the upgrade control event. Those events were never delivered on either channel. The client's `Last-Event-ID` advanced to `4424` on the next frame, permanently marking the gap as consumed. 3. **Correct handoff (server C):** The upgrade control event carried a `handoff_token` binding the SSE cursor namespace to the WebSocket session. The client sent the token plus `Last-Event-ID`; the server replayed `4418..4423` from the SSE buffer, then continued live on the socket. No gap, no duplicate. The non-obvious part: server A and server C both *claimed* to support resumption. The difference was invisible in the capability negotiation — both advertised `resumable: true`. The failure only manifested when the upgrade intersected an in-flight event buffer. ## Condition of Validity The cursor-continuity invariant holds only when all three of the following are true at the upgrade seam: - The server's event-ID allocator is **monotonic across transports** — the WebSocket session must draw from the same counter as the SSE channel it replaces, or the client must be given an explicit mapping. - The server retains the SSE buffer **until the handoff is acknowledged** — events emitted after the upgrade proposal but before the WebSocket `resume` ack must remain replayable. Flushing them to the old socket is a loss condition, not a delivery. - The client treats the upgrade as a **cursor-preserving transaction**: it must not advance `Last-Event-ID` past the last *durably applied* event until it has received either the replay or an explicit `resume_ack` with a cursor value it can verify against the upgrade proposal's `resume_from`. If any of these is absent, the seam is unguarded. The client cannot distinguish "no events were emitted during the handoff window" from "events were emitted and lost," because both present as a clean cursor advance. The condition does *not* require that the upgrade be atomic. It requires that the client be able to *detect* non-atomicity — i.e., that the server's `resume_ack` echo a cursor the client can compare against `resume_from`. A server that acks with a cursor lower than `resume_from` is telling the client to replay; a server that acks with a cursor higher than the client's last-applied ID without a replay frame is telling the client to distrust the seam. ## Decision Boundary The instance must choose between **accepting the upgrade** and **refusing it and continuing on SSE** at exactly one point: when the upgrade control event arrives, *before* the client has applied any event with an ID greater than the control event's own ID. - **Accept** only if the control event carries a handoff binding (token or explicit cursor mapping) *and* the client has a durable record of its last-applied event ID that it can present in the `resume` frame. The durable record must be written before the upgrade is attempted — an in-memory counter is not sufficient, because the upgrade may race a checkpoint. - **Refuse** if the control event lacks a binding, or if the client's last-applied ID is ahead of the control event's ID (meaning events were applied out of order and the cursor is already suspect). Refusal is a `cancel` control frame; the server must continue on SSE, and the client must not treat the refusal as a stream termination. - **Abort the stream** if the `resume_ack` returns a cursor that is neither equal to `resume_from` nor accompanied by a replay frame covering the gap. This is the only safe response to an unverifiable seam — the alternative is silent divergence. The boundary is sharp because the upgrade proposal is the last point at which the client has full information about both channels. After the WebSocket opens, the client is committed to one transport and can only detect, not prevent, a gap. ## Operational Effect An instance that enforces the boundary converts a class of silent state corruption into a loud, recoverable abort. The observable effects: - Tool-call side effects are applied at most once, because the client refuses to advance its cursor past a verified replay. - The `resume_ack` becomes a *verification primitive*, not a formality — the client can assert `ack.cursor == last_applied + 1` or `ack.cursor == resume_from` and fail closed otherwise. - Upgrade latency increases by one round-trip (the `resume_ack`), which is the cost of making the seam observable. - Servers that cannot provide a handoff binding are downgraded to SSE-only for that session, which is a capability regression the client must record — future sessions against the same server should pre-negotiate the upgrade out-of-band rather than mid-stream. The invariant to carry forward: **a transport upgrade is a cursor-namespace migration, and migrations require an explicit, verifiable mapping.** Any MCP client that treats the WebSocket as "the same stream" without that mapping is one buffered event away from either duplicate execution or a permanently consumed gap. ```python class CursorHandoffError(RuntimeError): """Raised when an MCP SSE-to-WebSocket upgrade cannot preserve cursor continuity.""" pass class UpgradeProposal: def __init__(self, control_event_id: int, resume_from: int, handoff_token: str | None): self.control_event_id = control_event_id self.resume_from = resume_from self.handoff_token = handoff_token def negotiate_upgrade( proposal: UpgradeProposal, last_applied_event_id: int, durable_cursor_written: bool, ) -> dict: # The upgrade is only safe if the client can prove what it has consumed # and the server has offered a binding into the WebSocket cursor namespace. if proposal.handoff_token is None: raise CursorHandoffError( f"upgrade at event {proposal.control_event_id} carries no handoff binding; " "refusing to migrate cursor namespace" ) if not durable_cursor_written: raise CursorHandoffError( f"last_applied={last_applied_event_id} not durably checkpointed; " "upgrade could race a resume and re-deliver applied events" ) if last_applied_event_id > proposal.control_event_id: raise CursorHandoffError( f"last_applied={last_applied_event_id} is ahead of control event " f"{proposal.control_event_id}; cursor is already suspect, aborting" ) return { "type": "resume", "handoff_token": proposal.handoff_token, "last_event_id": last_applied_event_id, "expected_cursor": proposal.resume_from, } def verify_resume_ack(ack: dict, expected_cursor: int, last_applied_event_id: int) -> None: # The ack must either confirm the expected cursor or announce a replay # covering the gap. Anything else is an unverifiable seam. ack_cursor = ack.get("cursor") replay_range = ack.get("replay_range") # (start, end) inclusive, or None if ack_cursor == expected_cursor: return if replay_range is not None: start, end = replay_range if start <= last_applied_event_id + 1 and end >= expected_cursor - 1: return raise CursorHandoffError( f"replay range {replay_range} does not cover gap " f"({last_applied_event_id + 1}..{expected_cursor - 1})" ) raise CursorHandoffError( f"resume_ack cursor={ack_cursor} unverifiable against expected={expected_cursor}; " "aborting stream to avoid silent divergence" ) ```