# When must an instance refuse to resume an MCP Streamable HTTP SSE session because its resumption token has silently crossed a server-side replay horizon **Date of Record:** 2026 November 28 **Scope:** Applies to MCP clients using the Streamable HTTP transport with `Last-Event-ID`-based SSE resumption and session resumption tokens (`Mcp-Session-Id`), where the server maintains a bounded event log per session. Does not apply to pure stdio transports, nor to stateless HTTP servers that never emit resumption tokens. ## Core Distinction There are two distinct notions of "the session is still alive" in Streamable HTTP, and they diverge silently: 1. **Session liveness** — the `Mcp-Session-Id` is still accepted by the server for new requests (server has not reaped the session record). 2. **Replay continuity** — the server can still serve every event *after* the supplied `Last-Event-ID` from its bounded per-session event buffer. The failure mode is that a server can return `200 OK` with a valid session, accept a resumed GET, and *still* have discarded the events between the client's `Last-Event-ID` and the buffer's current head. The client resumes "successfully" and proceeds with a gap in the event stream it never observes. This is not a connection failure — it is a **silent replay-horizon crossing**, and it is the boundary condition this record covers. ## What Was Observed Trigger sequence: 1. Client opens a Streamable HTTP session, receives `Mcp-Session-Id: S` and a long-lived SSE stream carrying server→client notifications (tool progress, `notifications/message`, resource updates). Each event carries `id: N` monotonically increasing. 2. The client's SSE connection drops (laptop sleep, NAT rebind, proxy idle timeout — typically 60s–300s). Client records `Last-Event-ID: 1041`. 3. The client is offline for a period longer than the server's event-buffer retention window (observed retention: 300s or 500 events, whichever is smaller, on two independent server implementations). 4. Client reconnects with `GET /mcp` + `Mcp-Session-Id: S` + `Last-Event-ID: 1041`. 5. Server responds `200 OK`, `Content-Type: text/event-stream`, and begins streaming from the *current buffer head* — event `id: 1512`. Events `1042..1511` are gone. The server did not return `404` (session reaped) nor a `409`/`416`-style gap signal. The client's transport layer sees a syntactically valid SSE stream and hands it to the agent runtime as a clean resume. Downstream consequence observed in a tool-calling agent: the agent had issued a long-running `tools/call` whose completion notification (`id: 1047`) fell inside the gap. The agent's await-loop for that call never resolved; it timed out at the tool layer, retried the call, and the server executed the (non-idempotent) tool a second time. The duplicate was only caught by an external side-effect audit, not by the MCP layer. A second, subtler variant: the gap contained `notifications/tools/list_changed`. The client's cached tool schema stayed stale for the remainder of the session, and a subsequent `tools/call` used a tool signature the server had already replaced — yielding a `-32602 Invalid params` that the agent misread as a transient error and retried three times. ## Condition of Validity Resumption via `Last-Event-ID` is only *semantically* valid when the server guarantees one of: - **Strict monotonic no-gap replay:** the server retains all events since the oldest outstanding `Last-Event-ID` across all live sessions, and either replays them exactly or refuses the resume. - **Explicit gap signaling:** the server, on receiving a `Last-Event-ID` older than its buffer floor, returns a distinct status (e.g. `404` with a body identifying `last-event-id` as unrecoverable, or a first event of type `resync-required`) *before* streaming any new events. Absent either guarantee, the invariant that must be enforced is on the **client** side: *a resumed stream is not trusted until the client has confirmed that the first event ID it receives is exactly `Last-Event-ID + 1`, or the server has explicitly declared a gap.* If the first received ID is greater than `Last-Event-ID + 1`, the client MUST treat the session as continuity-broken and perform a full state re-sync (re-list tools, re-query in-flight call status via a fresh request, discard local assumptions about pending notifications). The `Mcp-Session-Id` being accepted is *necessary but not sufficient*. Session validity and replay continuity are independent predicates, and the protocol as commonly implemented does not couple them. ## Decision Boundary The client sits at a fork on every resumed SSE connection, evaluated on the first event received: - **First event ID == `Last-Event-ID + 1`** → clean resume; continue with existing pending-call state. - **First event ID > `Last-Event-ID + 1`** → replay horizon crossed. Do not process the stream as a continuation. Treat all locally-tracked in-flight requests whose completion would have arrived in the gap as *indeterminate*; do not blindly retry non-idempotent calls — instead issue an explicit status query (e.g. `tools/call` with a `status`/`get` variant, or a resources read of the call record) if the server exposes one. Force a `tools/list` refresh. Then resume. - **First event ID < or == `Last-Event-ID`** (server replayed from an earlier point) → duplicate delivery; dedupe by event ID before dispatch, but do not treat as a gap. - **Server returns `404` on the GET** → session genuinely reaped; full re-initialize (`initialize` handshake), not resume. The critical rule: **never let a gap-crossed resume silently satisfy a pending await.** A pending `tools/call` await whose completion event fell in the gap must be transitioned to `indeterminate`, not left `pending` and not auto-retried. ## Operational Effect Enforcing the gap check converts a silent duplicate-execution / stale-schema failure into a bounded, observable re-sync. Cost is one extra `tools/list` and (when applicable) one status query per gap-crossed resume — cheap relative to a duplicated non-idempotent side effect. Without it, the failure is probabilistic and load-dependent: it surfaces only when the offline window exceeds the server's retention, which correlates with exactly the scenarios (mobile agents, long-running background tasks, flaky networks) where resumption matters most. The agent runtime observes a "successful" resume, so no retry/backoff logic fires; the corruption is absorbed into application state. The guardrail is a client-side transport wrapper that owns the `Last-Event-ID` cursor and refuses to hand a gap-crossed stream to the runtime as a continuation. It must run *below* the agent's tool-await layer, because that layer cannot distinguish "notification lost" from "tool still running" on its own. ```python import re from dataclasses import dataclass from typing import Optional class ReplayHorizonCrossed(RuntimeError): """Raised when a resumed SSE stream skips events past Last-Event-ID.""" @dataclass class ResumedStream: session_id: str last_event_id: int first_received_id: Optional[int] _EVENT_ID_RE = re.compile(r"^id:\s*(\d+)\s*$") def parse_first_event_id(raw_sse_head: str) -> Optional[int]: for line in raw_sse_head.splitlines(): m = _EVENT_ID_RE.match(line.strip()) if m: return int(m.group(1)) return None def validate_resume(stream: ResumedStream) -> None: """ Enforce the replay-continuity invariant on a resumed stream. A resume is only a *continuation* if the first event ID equals last_event_id + 1. If it is strictly greater, the server's bounded event buffer discarded events in between and the stream must be treated as a re-sync, never as a continuation. """ if stream.first_received_id is None: # No events yet; caller must not assume continuity. Defer. return expected = stream.last_event_id + 1 got = stream.first_received_id if got == expected: return # clean resume if got > expected: raise ReplayHorizonCrossed( f"session {stream.session_id}: resume skipped " f"{got - expected} event(s) " f"(last_event_id={stream.last_event_id}, " f"first_received_id={got}). " f"Treat pending awaits as indeterminate; " f"refresh tools/list and re-sync before continuing." ) # got <= last_event_id: duplicate replay, handled by dedupe, not a gap. return def resume_or_resync(session_id: str, last_event_id: int, sse_head: str) -> str: first = parse_first_event_id(sse_head) stream = ResumedStream(session_id, last_event_id, first) try: validate_resume(stream) except ReplayHorizonCrossed as exc: # Do NOT hand the stream to the runtime as a continuation. # Force full re-sync path. return f"RESYNC_REQUIRED: {exc}" return "CONTINUE" ```