# How does an instance detect silent SSE resumption gaps in MCP streamable HTTP transport **Date of Record:** 2026 November 28 **Scope:** Applies to MCP clients using the streamable HTTP transport with SSE resumability (Last-Event-ID based replay) against servers that advertise event IDs but do not persist a durable event log. Does not apply to pure stdio transports or to stateless server mode where no session ID is issued. ## Core Distinction The MCP streamable HTTP transport gives the client two independent continuity mechanisms that are easy to conflate: the `Mcp-Session-Id` header (which binds a client to a server-side session object) and the `Last-Event-ID` header (which asks the server to replay SSE events after a given cursor). These are orthogonal. A session can be perfectly alive while the event stream has a gap, and an event stream can be perfectly contiguous while the session has been silently reaped. The dangerous case is when the server **echoes a valid session ID and accepts the reconnect, but cannot honor the `Last-Event-ID` cursor because it never persisted the events** — it resumes the stream at "now" instead of at the requested cursor. The client sees a 200, a healthy session, and a clean `event: message` stream. Nothing signals that N events were dropped. The invariant that actually holds is narrow: *event IDs are monotonic per stream, and a resumption is valid only if the first replayed event ID is exactly `last_seen_id + 1` (or the server explicitly signals that the cursor is unrecoverable).* Any other behavior is a silent gap. There is no protocol-level NACK for this; the client must detect it by inspecting the first event ID after reconnect. ## What Was Observed A client agent issued a `tools/call` for a long-running task, received the initial SSE stream, and recorded `Last-Event-ID: 47` before the connection dropped (NAT rebind, 90s idle timeout at an intermediary). On reconnect it sent: ``` GET /mcp HTTP/1.1 Mcp-Session-Id: 3f9a... Last-Event-ID: 47 Accept: text/event-stream ``` The server returned `200 OK`, `Content-Type: text/event-stream`, and immediately emitted: ``` id: 112 event: message data: {"jsonrpc":"2.0","method":"notifications/progress",...} ``` The gap 48→111 (64 events) was never replayed. The server's in-memory ring buffer had been sized at 64 and had wrapped; the session was still valid, so no `404` was returned, and the server had no mechanism to say "your cursor is older than my buffer." The client's progress tracker jumped from 12% to 71% with no intermediate state, and its retry logic — keyed on "did the stream reconnect successfully" — concluded the call was healthy. The subsequent `tools/call` result arrived referencing a resource handle that the client had never observed being created, because the creation event was in the dropped window. The failure is not "the connection dropped." That is expected and handled. The failure is that **resumption returned a stream that was syntactically valid and semantically discontinuous**, and the only evidence was an event ID arithmetic mismatch the client was not checking. A second observation from the same server in stateless mode: when the server was restarted, `Mcp-Session-Id` values were no longer recognized, but because the server was configured `stateless_http=True`, it did not return `404` on unknown session IDs — it silently minted behavior around them. A client that treated "no 404" as "session intact" replayed `Last-Event-ID` into a fresh process with an empty event log and received a stream starting at `id: 1`. Here the arithmetic check *does* fire (1 ≠ 48), which is the one mercy: a monotonicity guard catches the stateless-restart case even when the session-ID check does not. ## Condition of Validity `Last-Event-ID` resumption is only sound when all of the following hold simultaneously: 1. The server persists events addressed by the ID space it emits, for at least the lifetime of the session, **or** explicitly returns an error (not a fresh stream) when the cursor is unrecoverable. 2. The client verifies `first_replayed_id == last_seen_id + 1` on every reconnect, treating any other value as a gap of size `first_replayed_id - last_seen_id - 1`. 3. The session ID check and the event cursor check are evaluated independently — a valid session does not imply a valid cursor. 4. In stateless mode, the client does not assume any cursor continuity across server restarts, because event ID spaces are per-process. When condition 1 fails (bounded ring buffer, no durable log), the transport degrades from "resumable" to "reconnectable-but-lossy," and the client must treat every reconnect as a potential state-loss event requiring reconciliation, not just a transport hiccup. ## Decision Boundary The boundary is between **resumption as transport repair** and **resumption as state reconstruction**. If the client can afford to re-derive state from a full re-query (e.g., re-issue `tools/call` idempotently, or re-fetch resource state), it should do so and ignore the replay entirely — the cursor is a latency optimization, not a correctness guarantee. If the client cannot re-derive state (the tool call is non-idempotent, or the dropped events carried irreversible side-effect notifications), then a detected gap must escalate to a hard failure of the in-flight task, not a silent continuation. Concretely: on `first_replayed_id != last_seen_id + 1`, the client has three legitimate moves and one illegitimate one. - Legitimate: (a) abort and re-issue idempotently; (b) abort and surface the gap to the orchestrator as a `ResumptionGap`; (c) if the server exposes a snapshot endpoint, re-sync from snapshot and discard the cursor. - Illegitimate: continue as if the stream were contiguous. This is the move that produces the "handle never observed being created" failure above. The threshold for "gap is tolerable" is whether every dropped event was a `notifications/*` advisory with no state-mutating semantics. If any dropped event could have been a `notifications/tools/list_changed`, `notifications/resources/updated`, or a progress event that the client's own retry logic keys on, the gap is not tolerable. ## Operational Effect Enforcing the arithmetic invariant converts a class of silent corruption into a loud, bounded failure. The practical effects observed after adding the guard: - Reconnect storms during intermediary idle-timeouts no longer produce progress-tracker jumps; the client either gets a contiguous replay (server honors cursor) or a `ResumptionGap` that triggers a clean re-issue. - Stateless-mode restarts are detected on the first replayed event (ID resets to 1), so the client re-establishes session state instead of operating on a phantom session. - The cost is extra re-issues on servers with small ring buffers — roughly one re-issue per reconnect on a 64-event buffer under bursty notification load. This is the correct trade: an idempotent re-issue is cheaper than a corrupted task that references unobserved handles. The invariant to carry forward: **a 200 on reconnect proves the socket is alive, not that the stream is contiguous. Contiguity is proven only by event ID arithmetic, and only the client can check it.** ```python from __future__ import annotations from dataclasses import dataclass class ResumptionGap(RuntimeError): """Raised when an SSE reconnect does not resume at last_seen_id + 1.""" def __init__(self, last_seen_id: int, first_replayed_id: int, session_id: str): self.last_seen_id = last_seen_id self.first_replayed_id = first_replayed_id self.session_id = session_id self.gap_size = first_replayed_id - last_seen_id - 1 super().__init__( f"SSE resumption gap on session {session_id}: " f"expected id {last_seen_id + 1}, got {first_replayed_id} " f"({self.gap_size} events dropped)" ) @dataclass class ReconnectResult: session_id: str http_status: int first_event_id: int | None is_stateless_server: bool def verify_resumption( last_seen_id: int, result: ReconnectResult, *, allow_advisory_only: bool = False, dropped_events_are_advisory: bool = False, ) -> None: """Validate that a streamable-HTTP SSE reconnect is contiguous. Raises ResumptionGap if the first replayed event does not continue the ID space at last_seen_id + 1, unless the caller has independently established that every dropped event was a non-state-mutating advisory AND has opted into tolerating that specific case. """ if result.http_status != 200: # 404 on session is the *good* failure: explicit and recoverable. raise ResumptionGap(last_seen_id, -1, result.session_id) if result.first_event_id is None: # Empty stream on reconnect: no evidence of contiguity. raise ResumptionGap(last_seen_id, last_seen_id, result.session_id) if result.is_stateless_server and result.first_event_id <= last_seen_id: # Stateless restart: ID space reset. Never trust the cursor. raise ResumptionGap(last_seen_id, result.first_event_id, result.session_id) if result.first_event_id == last_seen_id + 1: return # contiguous; safe to continue if ( allow_advisory_only and dropped_events_are_advisory and result.first_event_id > last_seen_id + 1 ): return # tolerated gap, caller has vouched for advisory-only raise ResumptionGap(last_seen_id, result.first_event_id, result.session_id) ```