# How should an instance distinguish a resumable SSE resume from a fresh stream when the MCP HTTP transport reuses session IDs across reconnects **Date of Record:** 2026 November 18 **Scope:** Applies to MCP servers and clients using the HTTP-native transport with streamable HTTP and SSE event resumption via `Last-Event-ID`, where a session ID is issued at initialization and reused across reconnects. Does not apply to stdio transports, nor to stateless HTTP servers that mint a new session ID per request. ## Core Distinction The MCP streamable HTTP transport conflates two semantically distinct operations under a single session identifier: *resuming* an interrupted SSE stream (client presents `Last-Event-ID`, expects replay of buffered events from that cursor onward with no gaps) and *opening a fresh* SSE stream within an existing session (client presents no cursor, expects only newly generated events). The session ID is a durable identity for the logical conversation. The `Last-Event-ID` is a per-stream cursor. When a client reconnects with the same session ID but *without* a `Last-Event-ID`—which happens after client-side process restart, after the client's local event buffer is discarded, or after a proxy strips the header—the server has no protocol-level signal to distinguish "this client is starting a new stream and accepts a gap" from "this client lost its cursor and will silently miss events." The failure is not a crash. It is a silent ordering and completeness violation: the server treats the request as a fresh stream, emits only live events, and both sides proceed as though the conversation is intact while an unknown number of server-to-client messages (tool results, progress notifications, resource updates) have been dropped in the gap between the last delivered event and the reconnect. ## What Was Observed A server implementation keyed its SSE replay buffer by session ID and indexed events by a monotonically increasing integer. On reconnect it branched on the presence of `Last-Event-ID`: - Present and within the retained buffer: replay from cursor, then attach live. - Present but older than the buffer window: it returned `400` with a "cursor too old" body—correct, but only after the client had already assumed resumption would succeed. - Absent: it attached a fresh stream and emitted nothing retroactively. The absent-cursor branch was the boundary. In a load test where the client's SSE reader was killed and restarted (simulating a mobile network handoff) while the server was mid-tool-call, the client reconnected with the session ID but empty cursor state. The server attached a fresh stream. The tool result event had been written to the buffer two hundred milliseconds earlier and was never replayed. The client's request for that tool call timed out at the application layer and issued a *second* tool call—a duplicate side effect (a payment capture) that the server executed without complaint, because the server had no idempotency key tying the two calls together and no knowledge that the first result had been lost. A second observation: when the client *did* present `Last-Event-ID` but the server had garbage-collected the buffer for a still-live session (buffer TTL shorter than session TTL), the server returned `404` on the stream endpoint. The client interpreted `404` as "session gone," re-initialized, and started a new session—orphaning the old session's in-flight work and leaking its resources until the session TTL expired. ## Condition of Validity The "fresh stream" interpretation is only safe under a condition that the transport does not enforce: that the client has independently established it has received every event prior to the reconnect, or that the client is willing to accept an unbounded gap. Neither holds for a client that lost its cursor due to process restart. The resumption semantics are valid only when `Last-Event-ID` is present *and* the server's retained buffer covers the cursor. The transport specification permits a server to drop events when no cursor is supplied, but it does not require the server to signal that a gap occurred, and it does not require the client to detect one. The invariant that actually must hold—*the concatenation of pre-disconnect and post-reconnect event sequences is gap-free and strictly ordered for a session*—is assumed by the application layer but guaranteed by neither endpoint. ## Decision Boundary The boundary is the presence and validity of the cursor relative to the buffer retention window, evaluated against the session's liveness. Three regimes: 1. **Cursor present, within retention:** resume with replay; safe. 2. **Cursor present, beyond retention, session live:** this is *not* a fresh session. Returning `404` (session-level) is wrong; the correct response is a stream-level error that forces the client to reconcile state, because the session is still valid but the stream is unrecoverable. 3. **Cursor absent, session live:** this is the dangerous regime. Treating it as a fresh stream is only correct if the server can prove no unacknowledged events are outstanding. Absent that proof, the server must either reject the stream or emit an explicit gap marker that the client is required to handle as a state-reconciliation trigger—not as a benign reconnect. The non-obvious part: regime 3 looks identical to a legitimate first stream open after initialization, because both carry a session ID and no cursor. The distinguishing fact is *time*—whether the session has already had a stream attached and detached. A server that tracks per-session stream history can separate the two; a server that keys only on the cursor cannot. ## Operational Effect Servers that implement the naive branch exhibit silent message loss under network handoff, duplicate side effects from application-layer retries, and session orphaning when buffer TTL and session TTL diverge. The correct posture is to (a) make buffer retention at least as long as session retention, (b) track whether a session has had a prior stream so that a cursor-less reconnect is classified as a *resumed-but-gapped* stream rather than a fresh one, and (c) require the client to acknowledge a gap marker before the server will accept further tool calls on that session. Clients, symmetrically, must persist `Last-Event-ID` durably across process restarts or treat a cursor-less reconnect as a signal to re-fetch authoritative state rather than to trust the stream. ```python class StreamResumeError(RuntimeError): """Raised when an SSE resume cannot be satisfied without a silent gap.""" pass class SessionStreamTracker: def __init__(self, buffer_ttl_s: int, session_ttl_s: int): if buffer_ttl_s < session_ttl_s: raise StreamResumeError( "buffer TTL must be >= session TTL; otherwise live sessions " "will produce 404s on resume and be mistaken for dead sessions" ) self.buffer_ttl_s = buffer_ttl_s self.session_ttl_s = session_ttl_s self._had_stream: set[str] = set() self._last_event: dict[str, int] = {} def classify(self, session_id: str, last_event_id: int | None) -> str: prior = session_id in self._had_stream if last_event_id is None: if prior: # Cursor-less reconnect on a session that already streamed: # this is NOT a fresh stream. Treat as gapped resume. raise StreamResumeError( f"session {session_id} reconnected without Last-Event-ID " "after a prior stream; refusing to emit a silent gap" ) return "fresh_initial" last = self._last_event.get(session_id) if last is None or last_event_id > last: raise StreamResumeError( f"cursor {last_event_id} ahead of server high-water mark " f"{last} for session {session_id}" ) if last_event_id < last - self._retained_count(session_id): # Cursor older than retained buffer but session still live. raise StreamResumeError( f"cursor {last_event_id} beyond retention for live session " f"{session_id}; client must reconcile state, not re-init" ) return "resume" def _retained_count(self, session_id: str) -> int: return max(0, int(self.buffer_ttl_s)) ```