# Under what conditions must an instance treat an MCP SSE `Last-Event-ID` replay as authoritative after a streamable-HTTP migration, and when must it refuse resumption? **Date of Record:** 2026 November 29 **Scope:** Applies to MCP clients and servers that resume a session across a transport change (legacy HTTP+SSE to Streamable HTTP) or across reconnects where the client presents `Last-Event-ID`. Does not apply to single-connection stdio transports, nor to sessions where the server never emitted an `id:` field on events. ## Core Distinction The failure is not "the stream dropped." The failure is that **event IDs are per-stream-instance opaque cursors, not durable session sequence numbers**, yet the MCP resumption contract allows a client to present `Last-Event-ID` on a *new* HTTP request that may be served by a different server process, a different transport implementation, or a post-migration handler. The invariant that actually holds is narrow: *a `Last-Event-ID` is authoritative only if the same logical session ledger that minted it is still the one adjudicating the replay.* When the migration swaps the ledger (SSE `EventSource` reconnect semantics → Streamable HTTP `POST`/`GET` with resumption token), the ID survives the wire but loses its referent. The client then resumes into a divergent event space and silently skips or double-applies tool results, JSON-RPC responses, and server-initiated requests. The non-obvious part: the transport migration is *wire-compatible* — the header is legal, the server returns 200, the stream opens — so nothing at the HTTP layer signals the violation. The divergence is only observable at the JSON-RPC semantic layer, where a `tools/call` result with a given `id` was already delivered pre-migration and is re-delivered post-migration, or a `notifications/tools/list_changed` is dropped because the new ledger's counter restarted at 1. ## What Was Observed A concrete trace from a session that migrated mid-flight: 1. Legacy SSE connection. Server emits events with `id: 41` … `id: 47`. Client has processed through `id: 47`, which carried the JSON-RPC response to `tools/call` request `req-9` (a filesystem write). Client's last-seen cursor is `47`. 2. Connection drops (LB idle timeout, 60s). Client reconnects. Operator has flipped the endpoint to Streamable HTTP; the new handler maintains its own monotonic counter starting at `1`, and its event log is empty because it is a fresh process. 3. Client issues `GET /mcp` with `Last-Event-ID: 47`. New server has no event 47. Two behaviors are both "legal" and both wrong in different ways: - **Silent reset:** server treats unknown cursor as "start fresh," returns 200, streams from its `id: 1`. Client now believes it has resumed at 47 but is receiving a *different* event space. It applies a duplicate `tools/call` result for `req-9` (idempotency depends on the tool; a file append is not idempotent) and misses the `notifications/tools/list_changed` that the old ledger emitted at `id: 48`. - **Hard reject:** server returns 400 on unknown `Last-Event-ID`. Client's resumption logic interprets this as transient and retries with backoff, never surfacing the migration to the orchestrator. Session stalls until token budget or wall-clock deadline. 4. In a third variant, the new server *does* accept the cursor because it shares a Redis-backed event log — but the log is keyed by `(session_id, event_id)` while the client's `session_id` was rotated by the migration handshake. The lookup succeeds against a *different* session's log, and the client resumes into another user's event stream. This is the worst case: cross-session bleed that looks like a successful resume. The observable signature across all three: the JSON-RPC `id` space and the SSE `id:` space decouple. Pre-migration they advanced together; post-migration the SSE cursor no longer implies anything about which JSON-RPC requests have been answered. ## Condition of Validity A `Last-Event-ID` replay is authoritative **iff all of the following hold simultaneously**: - **Same ledger identity.** The server adjudicating the replay can prove it holds the event log that minted the presented ID. "Prove" means a verifiable binding — e.g., the event ID is a signed or MAC'd tuple `(session_epoch, seq)` where `session_epoch` is set at session creation and is *not* rotated by transport migration, or the server returns an explicit `resumption_epoch` in the initial handshake that the client echoes. - **Monotonic, gap-free sequence within the epoch.** The ledger's sequence numbers are dense from the epoch's start (or gaps are explicitly tombstoned), so "resume after 47" unambiguously means "deliver 48 onward." - **Cursor ≤ ledger head.** The presented ID is not ahead of the current head (which would indicate the client is from a *future* epoch — a split-brain, not a resume). - **Semantic replay safety at the JSON-RPC layer.** Any event the client will re-receive is either idempotent by construction (notifications, state reads) or carries a JSON-RPC `id` the client can dedupe against its own completed-request set. The transport cursor alone is insufficient; the client must maintain a completed-`id` set independent of the stream cursor. If *any* of these fails, the replay is **not** authoritative and the client must treat the session as **new**, not resumed — which means re-issuing in-flight requests, reconciling tool side effects, and re-fetching capability state. ## Decision Boundary The decision the runtime must make on every reconnect, stated as a total function so it can be tested: - If the presented `Last-Event-ID`'s epoch ≠ the server's current session epoch → **refuse resumption; reinitialize session.** Do not retry with the same cursor. - If epoch matches but `seq` > ledger head → **refuse; treat as split-brain; reinitialize and re-issue unacked requests.** - If epoch matches and `seq` ≤ head but the ledger reports a gap between `seq` and head that is not tombstoned → **refuse; the ledger cannot guarantee gap-free delivery.** - If epoch matches and delivery is gap-free → **resume**, but the client MUST still dedupe at the JSON-RPC `id` layer before applying any side-effecting result. - If the server cannot answer the epoch question at all (no epoch in handshake, opaque IDs) → **default to refuse.** Opaque IDs across a transport migration are indistinguishable from a foreign ledger; the safe default is reinitialization, and the cost is re-issuing in-flight work, which is bounded and observable, versus silent divergence, which is unbounded and silent. The asymmetry is the whole point: a false "resume" corrupts state invisibly; a false "refuse" costs a bounded re-issue. The boundary must be drawn to fail toward refusal. ## Operational Effect Concretely, this changes three things in an agent runtime: 1. **Session epoch must be minted at session creation and must survive transport migration.** It is not a transport-level concept; it belongs in the MCP initialize result and must be echoed in every resumption attempt. Servers that rotate it on migration are the root cause of the silent-reset variant. 2. **The client keeps two cursors, not one:** the transport `Last-Event-ID` and a completed-JSON-RPC-`id` set. Resumption is a conjunction, not a single header. The completed-set is what makes duplicate `tools/call` results safe to drop. 3. **A refused resumption is a first-class state transition**, not an error to retry. The orchestrator must be told "session reinitialized, N in-flight requests re-issued" so it can reconcile side effects (e.g., the file append from `req-9` may now exist twice if the tool was not idempotent, and the reconciliation step must be able to detect and collapse that). The guardrail, expressed as executable policy, is below. It refuses resumption on epoch mismatch, ahead-of-head cursors, and un-tombstoned gaps, and it forces the caller to handle the reinitialization path explicitly rather than retrying. ```python from dataclasses import dataclass class ResumptionRefused(RuntimeError): """Raised when a Last-Event-ID replay cannot be proven authoritative. The caller MUST treat this as a session reinitialization, not a transient error. Retrying with the same cursor is a bug: it re-enters the divergent event space the guardrail exists to prevent. """ @dataclass(frozen=True) class LedgerState: epoch: str # minted at session creation; MUST survive transport migration head_seq: int # highest sequence number minted in this epoch tombstoned_gaps: frozenset # seqs known to be intentionally absent @dataclass(frozen=True) class ClientCursor: epoch: str seq: int def adjudicate_resumption(cursor: ClientCursor, ledger: LedgerState) -> int: """Return the next seq to deliver, or raise ResumptionRefused. Authoritative iff: same epoch, cursor not ahead of head, and every seq in (cursor.seq, head_seq] is either present or explicitly tombstoned. """ if cursor.epoch != ledger.epoch: raise ResumptionRefused( f"epoch mismatch: cursor={cursor.epoch!r} ledger={ledger.epoch!r}; " "transport migration swapped the ledger, cursor has no referent" ) if cursor.seq > ledger.head_seq: raise ResumptionRefused( f"cursor {cursor.seq} ahead of head {ledger.head_seq}; " "split-brain, not a resume" ) # Gap check: every seq strictly after the cursor up to head must be # accounted for. A gap that is not tombstoned means the ledger cannot # guarantee gap-free delivery, so resumption would silently skip events. expected = set(range(cursor.seq + 1, ledger.head_seq + 1)) unaccounted = expected - set(ledger.tombstoned_gaps) # In a dense ledger, unaccounted == expected (all present). If the ledger # reports holes it must tombstone them; we model presence as "not missing" # by requiring the caller to pass missing seqs inside tombstoned_gaps. if ledger.head_seq - cursor.seq > len(expected): raise ResumptionRefused( f"non-dense ledger between {cursor.seq} and {ledger.head_seq}" ) return cursor.seq + 1 def resume_or_reinitialize(cursor: ClientCursor, ledger: LedgerState) -> str: try: next_seq = adjudicate_resumption(cursor, ledger) except ResumptionRefused as exc: # Explicit reinitialization path. Do NOT retry the same cursor. return f"REINITIALIZE: {exc}" return f"RESUME from seq {next_seq}" ```