# How should an instance reconcile streamable-session resumption against SSE `Last-Event-ID` replay when an MCP server behind an HTTP-native transport has already advanced its session cursor past the client's last acknowledged event? **Date of Record:** 2026 November 27 **Scope:** Applies to MCP clients using the Streamable HTTP transport with `Mcp-Session-Id` and resumable SSE (`Last-Event-ID`) fallback, where a single logical session spans multiple HTTP requests and the server maintains a monotonic per-session event cursor. Does not apply to stdio transports or to stateless HTTP servers that emit no event IDs. ## Core Distinction The Streamable HTTP transport carries two independent continuity mechanisms that are easy to conflate: the **session identity** (`Mcp-Session-Id` header) and the **event cursor** (`Last-Event-ID` header on SSE resumption). Session identity answers "which conversation is this?"; the event cursor answers "from which point in the server's emitted event stream should replay begin?" These are orthogonal. A session can be perfectly valid while its event cursor is unrecoverable, and vice versa. The failure mode this record captures is the **cursor/identity divergence**: the client reconnects with a valid `Mcp-Session-Id`, the server accepts it, but the server's internal event log for that session has been **truncated, compacted, or advanced** such that the client's `Last-Event-ID` no longer maps to a replayable position. The server cannot replay from the requested cursor, yet it also cannot honestly say "session invalid"—the session is alive. The transport spec leaves this gap under-specified, and naive clients resolve it by either silently dropping events (data loss) or by treating the 200/404 as a session death and restarting (duplicate side effects). ## What Was Observed An MCP client issued a `tools/call` over Streamable HTTP. The server responded with an SSE stream carrying events `id: 4102` through `id: 4107`, then closed the stream cleanly (normal end of the tool result). The client processed through `id: 4104`, then the connection dropped mid-parse of `4105` due to a proxy idle timeout. The client held `Last-Event-ID: 4104` and a valid `Mcp-Session-Id`. On reconnect, the client sent `GET /mcp` with `Mcp-Session-Id` and `Last-Event-ID: 4104`. The server, however, had a bounded per-session event ring buffer of 3 entries. Since the disconnect, it had emitted `4105`, `4106`, `4107` (the original tail) plus `4108`, `4109` from a concurrent `notifications/tools/list_changed`. The buffer now held `4107..4109`. Cursor `4104` was **evicted**. The server's behavior in this instance: it returned `200 OK` with an SSE stream starting at `4107`. The client, expecting replay from `4104`, received events with IDs it did not recognize as contiguous, and—because its parser assumed monotonic continuation—**appended `4107..4109` after `4104`**, silently losing `4105` and `4106`. Those two events contained the tail of the tool result and a partial structured-content chunk. The tool call appeared to succeed; the result was corrupt. A second variant was observed against a stricter server: it returned `404 Not Found` on the resumption attempt because the cursor was unmappable, which the client interpreted as session death and re-issued the `tools/call` under a fresh session—producing a **duplicate side effect** (the tool was a write). ## Condition of Validity The invariant that must hold for safe resumption is: > For a resumption to be *safe*, the server must be able to prove that the requested `Last-Event-ID` is either (a) still replayable from its retained log, or (b) provably the most recent event the client could have acknowledged, with no intervening events lost. If neither holds, the server must signal an explicit **unresumable-gap** condition, not a generic 404 and not a silent forward-jump. The client-side dual invariant: > A client must never treat a resumption stream as a continuation of its local state unless the first event ID it receives is exactly `Last-Event-ID + 1` (or the server explicitly declares the gap). Any forward jump in event IDs is a **gap signal** and must abort the logical operation, not be papered over. This condition is valid only when event IDs are monotonic and gap-detectable. If a server uses non-numeric or non-monotonic IDs, the client cannot detect gaps by arithmetic and must rely on an explicit server-declared cursor/epoch handshake—outside the scope of this record. ## Decision Boundary The decision boundary is the choice between **resume-with-gap-tolerance** and **fail-closed-restart**, and it must be made on the *semantics of the interrupted operation*, not on the transport status code. - If the interrupted operation is **idempotent and read-only** (`resources/read`, `tools/list`), a gap is tolerable: the client may discard local partial state and re-issue the request. Restart is safe. - If the interrupted operation is **non-idempotent or stateful** (`tools/call` with side effects, any request whose result was partially delivered), a gap is **fatal to correctness**. The client must not silently continue, and must not blindly re-issue. It must query the server for the operation's terminal status (or rely on an idempotency key) before deciding. - If the server cannot distinguish "cursor evicted" from "session expired," it must return a distinguishable error (e.g., `409` with a body naming the gap, or a dedicated JSON-RPC error code) so the client can branch. Returning `404` for both is the root cause of the duplicate-side-effect variant. The boundary is crossed the moment the client's `Last-Event-ID` is older than the server's oldest retained event **and** the operation is non-idempotent. At that exact point, silent resume is forbidden. ## Operational Effect Two concrete failure classes result from mishandling this boundary: 1. **Silent result corruption.** The client concatenates a forward-jumped stream onto its stale local buffer, producing a tool result that is internally inconsistent (missing chunks) yet structurally valid. Downstream reasoning consumes the corrupt result without error. This is the more dangerous class because no exception is raised anywhere. 2. **Duplicate side effects.** The client treats cursor-unmappability as session death, opens a fresh session, and re-executes a non-idempotent tool. The write happens twice. The guardrail is a **cursor-continuity check** on the client and an **explicit gap error** on the server, plus an **idempotency key** carried on non-idempotent `tools/call` requests so that a fail-closed restart can be safely deduplicated server-side. The client must validate `first_received_id == last_acknowledged_id + 1` before merging any resumed stream, and raise on mismatch rather than continue. ```python from __future__ import annotations from dataclasses import dataclass class CursorGapError(RuntimeError): """Raised when a resumed SSE stream does not continue from the acknowledged cursor.""" class UnresumableSessionError(RuntimeError): """Raised when the server cannot map Last-Event-ID to a replayable position.""" @dataclass class ResumptionState: session_id: str last_acknowledged_id: int operation_is_idempotent: bool def validate_resumption( state: ResumptionState, first_received_id: int | None, server_status: int, server_declared_gap: bool, ) -> None: # Server could not map the cursor to its retained log. if server_status in (404, 409) and not server_declared_gap: # Ambiguous: could be expired session OR evicted cursor. Fail closed. if not state.operation_is_idempotent: raise UnresumableSessionError( f"session {state.session_id}: cursor {state.last_acknowledged_id} " f"unmappable (status {server_status}); non-idempotent op must not restart blindly" ) return if server_declared_gap: if not state.operation_is_idempotent: raise CursorGapError( f"session {state.session_id}: server declared gap after " f"{state.last_acknowledged_id}; refusing to merge non-idempotent result" ) return if first_received_id is None: raise CursorGapError( f"session {state.session_id}: resumption stream carried no event id" ) expected = state.last_acknowledged_id + 1 if first_received_id != expected: # Forward jump == silent gap. Never paper over it. raise CursorGapError( f"session {state.session_id}: expected first id {expected}, " f"got {first_received_id} (gap of {first_received_id - expected} events)" ) def resume_stream(state: ResumptionState, response: dict) -> list[int]: validate_resumption( state, first_received_id=response.get("first_event_id"), server_status=response.get("status", 200), server_declared_gap=response.get("declared_gap", False), ) return response.get("event_ids", []) ```