# Where must an instance reconcile a replayed SSE event against a resumed MCP session before it trusts the stream upgrade **Date of Record:** 2026 November 30 **Scope:** Applies to MCP clients that resume a prior `streamable-http` session via `Last-Event-ID` and simultaneously receive a server-initiated SSE upgrade (`GET /mcp` with `Accept: text/event-stream`) on the same session ID. Does not cover stateless MCP transports, stdio transports, or fresh sessions with no prior event history. ## Core Distinction The ambiguity that breaks resumption is not "did the server resend events." It is that the MCP streamable-HTTP transport permits *two independent mechanisms* to deliver the same logical event to a client: (1) replay triggered by `Last-Event-ID` on a resumed `POST`/`GET`, and (2) a server-initiated SSE upgrade that opens a fresh stream on the same session and, by spec convention, may begin emitting from a server-chosen cursor rather than the client's `Last-Event-ID`. These two channels are not ordered relative to each other. A client that treats "event received on the upgraded stream" as strictly newer than "event received via replay" will silently apply an older server state on top of a newer one. The invariant that must hold: **an MCP event is identified by its `id` field, and delivery is at-least-once across both channels; therefore the client must dedupe and order by `id`, never by arrival time or channel.** The boundary failure occurs precisely when the client's session state machine assumes channel-monotonicity instead of id-monotonicity. ## What Was Observed A client agent held session `sess_7f3a` mid-tool-call. The connection dropped after the client had received events `id: 40` through `id: 47` and recorded `Last-Event-ID: 47`. On reconnect, the client issued a resumption `GET /mcp` with `Last-Event-ID: 47` and `Accept: text/event-stream`. The server, per its upgrade policy, responded `200 OK` with `Content-Type: text/event-stream` and immediately began streaming — but started at `id: 44`, not `48`. The server's SSE upgrade path replayed from a *server-side ring-buffer checkpoint* (the last flush boundary), not from the client's declared cursor. This is legal: the transport spec says the server SHOULD honor `Last-Event-ID` but does not forbid a broader replay window on upgrade, and it does not require the upgrade stream to be a strict suffix. Trace: 1. Client sends resumption `GET` with `Last-Event-ID: 47`. 2. Server accepts, upgrades to SSE, and emits `id: 44` (tool-result partial), `id: 45`, `id: 46`, `id: 47`, `id: 48` (tool-result complete), `id: 49` (assistant delta). 3. Client's session reducer, keyed on "newest arrival wins," reapplies `44`–`47` over state it had already advanced past. For `44`–`46` this was idempotent. For `47` it was not: event `47` was a `sampling/createMessage` request the client had already answered with a `sampling` response carrying a nonce. Reapplying `47` caused the client to re-issue the sampling request to its underlying model, producing a second, divergent completion, and to overwrite the pending tool-call argument buffer with the pre-`47` snapshot. 4. The agent then sent a tool invocation whose arguments were derived from the replay-overwritten buffer while the server had already committed the `47`-era arguments. Divergence: the server executed tool call `T` with arguments `A_old`; the agent's transcript recorded `T` with `A_new`. 5. Downstream, the agent's verification step compared the server's `T` result against its own `A_new`, found a mismatch, and entered a retry loop that replayed `T` three times before the token budget exhausted. The failure was not the replay. It was the client's *ordering assumption*: it treated the upgrade stream as authoritative-newer and did not reconcile replayed ids against already-acknowledged ids before mutating session state. ## Condition of Validity The client MAY treat the upgraded SSE stream as a pure continuation (no reconciliation needed) only if all of the following hold: - The server's upgrade response includes an explicit cursor commitment — e.g., a first event with `id` strictly greater than the client's `Last-Event-ID`, or a `X-MCP-Resume-From` header equal to the client's declared cursor. - The client has persisted the set of acknowledged event `id`s (not just the max), because the replay window may be non-contiguous relative to what the client already processed. - The session reducer is idempotent for every event type that can appear in a replay window — specifically `sampling/createMessage`, `roots/list_changed`, and any tool-call request that carries a client-generated nonce. If any of these is false — and in practice the first is false for most streamable-HTTP servers, because the spec makes the cursor commitment optional — the client MUST run the reconciliation path below. ## Decision Boundary The decision the client must make on the first event of an upgraded stream is not "is this a duplicate" but "what is the relationship between the server's replay window and my acknowledged set." Concretely: - If `event.id <= max(acknowledged_ids)` and `event.id` is in the acknowledged set: **drop** (exact replay). - If `event.id <= max(acknowledged_ids)` and `event.id` is *not* in the acknowledged set: **this is a gap-fill replay of an event the client never saw** — apply it, but do not advance the session's logical clock past `max(acknowledged_ids)`. This case is the dangerous one: it is indistinguishable from a fresh event by arrival order, and applying it as "newest" is the bug. - If `event.id > max(acknowledged_ids)`: **apply and advance**. - If `event.id` is missing entirely: the transport has violated the id-invariant; the client MUST abort resumption and open a fresh session rather than guess ordering. A missing `id` on a streamable-HTTP SSE event means the server cannot honor `Last-Event-ID` semantics and the client has no basis for dedupe. The boundary is the second case. It is the only case where "replay" and "new event" are observationally identical to a client that keys on arrival order, and it is the case that produced the divergence above. ## Operational Effect The guardrail is a session reducer that maintains an acknowledged-id set and a logical clock decoupled from arrival order, plus a hard refusal to apply id-less events on a resumed stream. Applied to the trace: event `44` is in the acknowledged set → dropped; `45`, `46` dropped; `47` is in the acknowledged set → dropped (no second sampling request, no buffer overwrite); `48`, `49` applied and clock advanced. The tool call executes with `A_new` on both sides, verification passes, no retry loop. The cost is that the client must persist acknowledged ids across the disconnect — not just the max — because a non-contiguous replay window (here `44`–`47` when the client had only recorded `47` as max) cannot be reconciled from the max alone. Clients that persist only `Last-Event-ID` as a scalar are structurally unable to distinguish "server replayed an event I already saw" from "server replayed an event I missed," and will either double-apply or drop-and-lose depending on which way they guess. The id-set is the minimum state required to make resumption safe under a server-initiated upgrade. ```python from dataclasses import dataclass, field from typing import Optional class ResumptionInvariantError(RuntimeError): """Raised when an upgraded MCP SSE stream cannot be reconciled.""" @dataclass class SessionState: acknowledged_ids: set[int] = field(default_factory=set) logical_clock: int = 0 # max acknowledged id, NOT max arrival def reconcile_upgraded_event(state: SessionState, event_id: Optional[int]) -> str: """ Classify an event arriving on a server-initiated SSE upgrade stream. Returns one of: 'drop', 'gap_fill', 'apply'. Raises ResumptionInvariantError when the id-invariant is violated. """ if event_id is None: # No id on a resumed stream: server cannot honor Last-Event-ID. # Guessing ordering here is the documented failure mode. raise ResumptionInvariantError( "event on resumed stream has no id; abort resumption, open fresh session" ) if event_id > state.logical_clock: state.acknowledged_ids.add(event_id) state.logical_clock = event_id return "apply" if event_id in state.acknowledged_ids: return "drop" # event_id <= logical_clock but unseen: gap-fill replay. # Apply, but do NOT advance logical_clock — this event is not "newest". state.acknowledged_ids.add(event_id) return "gap_fill" def handle_upgrade_stream(state: SessionState, events: list[dict]) -> list[dict]: applied = [] for ev in events: action = reconcile_upgraded_event(state, ev.get("id")) if action in ("apply", "gap_fill"): applied.append({**ev, "_reconcile": action}) return applied ```