# In what scenarios must an instance treat MCP Streamable HTTP session resumption as a replay of an ordered event log rather than a reconnection to a live stream **Date of Record:** 2026 November 22 **Scope:** Applies to MCP clients and servers using the Streamable HTTP transport with `Mcp-Session-Id` and `Last-Event-ID` resumption against SSE-delivered JSON-RPC responses. Does not apply to the deprecated HTTP+SSE two-endpoint transport, nor to stdio transports, nor to stateless servers that never emit a session id. ## Core Distinction A reconnecting MCP client has two incompatible mental models available to it, and the wire protocol only enforces one of them. The **reconnection model** treats the stream as a socket: the client believes that after a dropped TCP connection, re-issuing `GET /mcp` with the same `Mcp-Session-Id` re-attaches it to a still-running computation, and that events arriving afterward are a continuation. Under this model, the client's local state is whatever it had when the socket died, plus a delta. The **replay model** treats the stream as a durable, monotonically ordered event log scoped to a session: the client acknowledges a position with `Last-Event-ID`, and the server is obligated to redeliver *every* event after that id — including events the client already received but did not durably commit, and including the terminal `result` or `error` for an in-flight request. Under this model, the client's local state is a function of the log from a known checkpoint, and reconnection is indistinct from cold resume. The boundary is non-obvious because both models produce identical behavior on the happy path. The divergence appears only when the server's in-memory event buffer has been evicted, truncated, or was never durable to begin with — at which point the reconnection model silently loses events while the replay model can detect the loss and refuse to proceed. ## What Was Observed A client agent issued `tools/call` for a long-running `analyze_repository` tool. The server streamed progress notifications as SSE events with ids `1001`, `1002`, `1003`, then the connection dropped after a NAT rebind at the 60-second idle boundary. The client reconnected with `Last-Event-ID: 1003`. The server had been configured with a 30-event ring buffer per session. Between the drop and the reconnect (11 seconds), the tool had emitted 47 progress notifications (`1004`–`1050`) and the terminal `result` event `1051`. The ring buffer had evicted `1004`–`1021`. The server resumed the stream at `1022`, because `Last-Event-ID: 1003` was no longer in the buffer and the server's resumption logic fell back to "deliver from oldest available." The client received `1022`–`1051` and observed a contiguous-looking stream whose first event id did not match its requested resume point. Because the client had been written to the reconnection model, it did not check. It treated the missing `1004`–`1021` as events that had never existed, assembled the `result` payload from `1051`, and reported success. The `result` payload for this particular tool embedded a `partial: true` flag and a `warnings` array that had been emitted in event `1007` — an event the client never saw. The agent downstream treated the tool output as complete and non-warning. This is the failure signature: **not a hang, not an error, but a successful-looking result whose provenance is a subset of the events the server actually emitted.** The corruption is silent because JSON-RPC results are self-describing payloads; the missing events are metadata, not structure. ## Condition of Validity The replay model is the *only* correct interpretation when all of the following hold, and the client must detect this condition rather than assume it: 1. The server advertises `Mcp-Session-Id` (session is stateful), and 2. The client is resuming with a non-empty `Last-Event-ID`, and 3. The request that was in flight at disconnect time has not yet produced a terminal `result` or `error` event in the client's durable log. Under these conditions, the client cannot distinguish "the server resumed correctly" from "the server dropped events and resumed from a later point" without an explicit check. The check is: the first event id received after resumption must be exactly `Last-Event-ID + 1` under the server's id scheme, or the server must have explicitly signaled a gap. If the first received id is greater, the client is in a gap and must treat the resume as failed. The reconnection model is valid only for the degenerate case where the client already holds the terminal event for every in-flight request — i.e., the client is resuming purely to receive *future* events for requests it has not yet issued. In that case `Last-Event-ID` is a cursor, not a checkpoint, and gap detection is unnecessary because there is no in-flight state to corrupt. The condition is *not* satisfied merely because the server returned HTTP 200 with `Content-Type: text/event-stream`. A 200 is the normal response to a resume that silently dropped 18 events. The transport layer's success signal is orthogonal to the log's integrity. ## Decision Boundary The decision boundary is: **does the client have any in-flight request whose terminal event it has not durably recorded?** - If **yes**, the client is in replay semantics. It must (a) persist the id of the last event it durably processed, not the last event it received; (b) on resume, verify the first received id is contiguous with its persisted checkpoint; (c) on any gap, abort the in-flight request and surface a `SessionResumeGapError` rather than synthesizing a result from later events. - If **no**, the client is in cursor semantics. It may resume from the last received id and proceed without gap detection, because there is no state to corrupt — the next event it receives will be the first event of a request it has not yet issued. The trap is that these two cases are indistinguishable from the client's perspective at the moment of reconnection *unless the client tracks in-flight requests separately from received events*. A client that maintains only a single "last seen event id" cannot make the distinction and will default to cursor semantics, which is wrong whenever a request is in flight. This is the dominant implementation bug: conflating "last event received" with "last event committed." The server-side obligation is the dual: a server that cannot guarantee contiguous replay from an arbitrary `Last-Event-ID` must not accept that id silently. It must either return `404` (session expired, client must re-initialize) or emit an explicit gap notification before resuming. Resuming from the oldest available event without signaling the gap is the server-side bug that makes the client-side bug unobservable. ## Operational Effect The observable effect is a class of silent result corruption where the agent's downstream reasoning is grounded in a tool result whose metadata (warnings, partial flags, progress-derived state) was truncated by transport. The agent cannot detect this from the result alone; the result is well-formed JSON. The corruption is only visible in the event log, which the agent did not fully receive. The architectural invariant required is: **an agent must never treat an MCP tool result as complete unless it has durably observed the terminal event for that request, with a contiguous event-id chain from the request's `id` to its terminal event.** This makes the event id a first-class part of the result's provenance, not a transport detail. It also means the client's session state must be a durable log position, not a socket state — the reconnection model is a lossy abstraction that the protocol permits but does not support. The practical guardrail is a small state machine per session: `{committed_event_id, in_flight_request_ids}`. On resume, if `in_flight_request_ids` is non-empty, the first received event must satisfy `first_id == committed_event_id + 1`; otherwise raise. On terminal event for a request, remove it from `in_flight` and advance `committed_event_id` only after the result is durably persisted downstream. The commit ordering matters: advance the cursor *after* the result is persisted, never before, or a crash between the two re-creates the gap. ```python import json from dataclasses import dataclass, field from typing import Optional class SessionResumeGapError(RuntimeError): """Raised when a resumed MCP session's first event id is not contiguous with the client's durably committed checkpoint while a request is in flight.""" @dataclass class SessionState: committed_event_id: int = 0 in_flight_request_ids: set = field(default_factory=set) # Results are only persisted after the terminal event is observed; # the cursor advances only after persistence. Order matters. persisted_results: dict = field(default_factory=dict) def resume_session(state: SessionState, first_received_event_id: int) -> None: """Validate that a resumed stream is a contiguous replay from our checkpoint. Cursor semantics (no in-flight requests) tolerate any starting id. Replay semantics (in-flight requests) require exact contiguity. """ if not state.in_flight_request_ids: # Cursor semantics: nothing to corrupt, accept any resume point. state.committed_event_id = first_received_event_id - 1 return expected = state.committed_event_id + 1 if first_received_event_id != expected: raise SessionResumeGapError( f"Resume gap detected: committed_event_id={state.committed_event_id}, " f"expected first event id {expected}, received {first_received_event_id}. " f"In-flight requests {sorted(state.in_flight_request_ids)} cannot be " f"safely reconstructed; aborting rather than synthesizing a result." ) def apply_terminal_event( state: SessionState, event_id: int, request_id: str, payload: dict ) -> None: """Apply a terminal result/error event and advance the commit cursor. Persist the result BEFORE advancing committed_event_id. A crash between the two must leave the cursor behind the event so replay redelivers it. """ if request_id not in state.in_flight_request_ids: raise SessionResumeGapError( f"Terminal event {event_id} for unknown request {request_id}; " f"session state is inconsistent with the server's event log." ) # Persist first (simulated here as an atomic dict write). state.persisted_results[request_id] = payload # Only now advance the cursor and clear the in-flight marker. state.committed_event_id = event_id state.in_flight_request_ids.discard(request_id) ```