# What happens when an instance resumes an MCP streamable-SSE session after `Last-Event-ID` points past a server-side replay gap **Date of Record:** 2026 November 21 **Scope:** Applies to MCP servers using the HTTP-native streamable transport with SSE resumption (event IDs, `Last-Event-ID` reconnection, session IDs). Does not apply to stdio transports or to stateless request/response MCP servers with no resumable stream. ## Core Distinction The failure is not "the connection dropped." TCP/HTTP disconnects are routine and the transport is designed to survive them. The failure is a **silent replay-gap acceptance**: the client reconnects with a `Last-Event-ID` that the server can no longer satisfy from its bounded event buffer, and the server responds with a *new* stream that is syntactically valid but semantically begins *after* the gap, with no in-band signal that events were skipped. A naive resumption model assumes the pair `(session_id, last_event_id)` is a complete cursor. It is not. The cursor is only complete if the server can prove contiguity from `last_event_id` forward. When the server's replay buffer has been truncated (bounded ring buffer, TTL eviction, or a server restart that lost in-memory events), the server has three theoretical options: (a) reject with a resumability error, (b) replay from the oldest retained event and let the client detect the gap, or (c) silently open a fresh stream from "now." Many implementations do (c) because it keeps the client connected and looks healthy — and (c) is the boundary condition that corrupts agent state. ## What Was Observed An agent instance held an MCP session against a server that streamed tool-progress and resource-update notifications over SSE. The server retained the last 256 events per session in a ring buffer. The agent process was suspended (or its network partitioned) for ~40 seconds while the server emitted 300+ events — progress deltas, a `notifications/tools/list_changed`, and a terminal `result` for an in-flight `tools/call`. On reconnect the client sent: ``` GET /mcp Mcp-Session-Id: 7f3a... Last-Event-ID: 1841 Accept: text/event-stream ``` The server's buffer now covered event IDs `1885..2140`. Event `1841` was gone. The server did not return an error. It opened a stream and immediately began delivering from `1885`, with the first frame being: ``` id: 1885 event: message data: {"jsonrpc":"2.0","method":"notifications/progress", ...} ``` The client saw a valid SSE stream, a valid session, and a monotonically increasing event ID — `1885 > 1841`, so its own cursor-advance assertion passed. Nothing in the transport layer flagged the 44-event discontinuity (`1842..1885`). The consequence was agent-layer, not transport-layer. The skipped `result` for the in-flight `tools/call` never arrived, so the agent's tool-call future never resolved; the agent re-issued the call (its timeout heuristic), which the server treated as a *new* invocation rather than a replay, producing a duplicate side effect. The skipped `tools/list_changed` meant the agent kept using a stale tool schema and issued a call with a parameter the server had renamed — a schema-negotiation mismatch that surfaced as a downstream validation error three steps later, with a traceback that pointed nowhere near the real cause. The tell in the logs was not an error. It was a gap: `last seen 1841`, `next received 1885`, `delta 44`, with `reconnect_status: 200 OK`. ## Condition of Validity The "silent gap" behavior is only *safe* under a condition that almost never holds in agent systems: that every event in the gap is idempotently reconstructable from post-gap state, and that no in-flight request's terminal event falls inside the gap. For a pure telemetry stream (metrics, heartbeats) this can hold. For an MCP session carrying JSON-RPC responses, it does not: a `result` or `error` for a pending request is a one-shot terminal event, and its loss converts a resolvable future into a hang-then-duplicate. Therefore the resumption invariant that must be enforced is: > A resumption is valid only if the server can emit the *contiguous successor* of `Last-Event-ID`. If `Last-Event-ID` is older than the oldest retained event, the server MUST signal non-resumability (terminate the stream and/or return a resumability error / new session), and the client MUST treat the session as poisoned and re-derive state from scratch. Silently advancing the cursor to the oldest retained event violates this invariant. The event ID being monotonic is necessary but not sufficient; contiguity from the client's last-seen ID is the actual requirement. ## Decision Boundary The boundary is drawn by **who owns gap detection**. There are two defensible designs and one indefensible one: - **Server-detects (preferred):** Server compares `Last-Event-ID` against its retained range. If `last_event_id < oldest_retained - 1`, it does not open a normal stream. It either returns a distinct resumability error (e.g., HTTP 409 with a machine-readable body `{"error":"resume_gap","oldest":1885}`) or closes the stream immediately after headers so the client's reconnect loop cannot mistake it for continuity. The client, on seeing this, MUST NOT blindly retry the same cursor; it must re-initialize the session and reconcile in-flight requests. - **Client-detects (acceptable fallback):** Server replays from `oldest_retained` but the *first* replayed frame carries an explicit marker (e.g., `event: resume-gap` with `{"from":1841,"resuming_at":1885}`) before any normal event. The client asserts `resuming_at == last_event_id + 1`; on mismatch it poisons the session. - **Neither-detects (the failure):** Server starts at `oldest_retained` with no marker; client only checks monotonicity. This is the observed case and must be treated as a protocol violation by any client that cares about correctness. The practical decision rule for an agent runtime: **never trust `next_id > last_id` as proof of continuity.** Continuity is `next_id == last_id + 1` *or* an explicit, authenticated gap marker that the runtime has been programmed to handle. Any other transition is a session-poisoning event. ## Operational Effect Once the gap is accepted, three agent-layer failures compound, and they compound *silently*: 1. **Orphaned futures.** Terminal `result`/`error` events lost in the gap leave pending JSON-RPC requests unresolved. Agent runtimes that resolve on timeout by re-issuing convert a lost response into a duplicate invocation. Side-effecting tools (file writes, payments, state mutations) execute twice. 2. **Stale capability view.** A skipped `notifications/tools/list_changed` (or resource-update) leaves the agent's tool schema cache diverged from the server. Subsequent calls fail validation, and the error surfaces far from the gap, misattributing blame to the wrong tool. 3. **Cursor laundering.** Because the client's cursor advances to `1885` and continues normally, every *subsequent* reconnect is now anchored to a post-gap ID. The gap becomes unrecoverable and invisible in future logs — the only record is the single `delta 44` line, which no health check reads. The guardrail is a contiguity assertion at the transport boundary plus a session-poisoning path that forces state re-derivation. The runtime must treat a detected gap as equivalent to a cold start for that session: cancel orphaned futures, invalidate cached schemas, and re-issue only requests whose idempotency it can prove. ```python from dataclasses import dataclass class ResumeGapError(RuntimeError): """Raised when an SSE resumption cannot prove contiguity from Last-Event-ID.""" @dataclass class ResumeDecision: accept: bool first_deliverable_id: int def evaluate_resumption( last_event_id: int, oldest_retained_id: int, next_stream_id: int, gap_marker_present: bool, ) -> ResumeDecision: """ Decide whether an MCP streamable-SSE resumption preserves continuity. Invariant: a resumption is valid only if the stream's first deliverable event is exactly last_event_id + 1, OR an explicit gap marker is present AND the runtime is prepared to poison the session. """ expected = last_event_id + 1 if next_stream_id == expected: return ResumeDecision(accept=True, first_deliverable_id=next_stream_id) # Server skipped events. Monotonicity is NOT continuity. if next_stream_id > expected: if gap_marker_present: # Client-detected gap path: caller must poison + re-derive state. raise ResumeGapError( f"resume gap acknowledged by marker: expected {expected}, " f"stream resumes at {next_stream_id}; session must be poisoned" ) # Silent gap: the indefensible case. Refuse to launder the cursor. raise ResumeGapError( f"silent replay gap: Last-Event-ID={last_event_id} but server " f"oldest_retained={oldest_retained_id} and stream starts at " f"{next_stream_id}; {next_stream_id - expected} events lost, " f"no marker. Treat session as poisoned." ) # next_stream_id < expected: server rewound or duplicated events. raise ResumeGapError( f"non-monotonic resumption: expected {expected}, got {next_stream_id}" ) def resume_session( last_event_id: int, oldest_retained_id: int, next_stream_id: int, gap_marker_present: bool, orphaned_request_ids: list[str], ) -> None: decision = evaluate_resumption( last_event_id, oldest_retained_id, next_stream_id, gap_marker_present ) if not decision.accept: # Unreachable given evaluate_resumption's raising contract, but kept # explicit so the invariant is enforced at the call site too. raise ResumeGapError("resumption rejected without a poison path") # Caller may now safely advance the cursor to first_deliverable_id. assert decision.first_deliverable_id == last_event_id + 1 # Any request whose terminal event could not be proven delivered must be # reconciled (idempotency check), never blindly re-issued. for req_id in orphaned_request_ids: _reconcile_before_reissue(req_id) def _reconcile_before_reissue(req_id: str) -> None: # Placeholder: query server-side idempotency key / effect log before retry. ... ```