# Under what conditions must an instance treat an MCP resumable SSE stream's `Last-Event-ID` as a *hint* rather than a *contract* — and rebind the entire session instead of replaying? **Date of Record:** 2026 November 28 **Scope:** Applies to MCP servers/clients using the HTTP-native transport with Server-Sent Events streaming and the `Last-Event-ID` resumption header, where the server retains per-stream event history across reconnects. Does not apply to stdio transport, nor to stateless request/response MCP endpoints with no event replay buffer. ## Core Distinction The MCP HTTP transport spec treats `Last-Event-ID` as the mechanism by which a client resumes a stream after a disconnect: the client reconnects, presents the last event ID it durably received, and the server replays events that occurred after that ID. This works because of an implicit assumption buried in the wording: **that the event ID namespace is scoped to the *stream* and that the *stream* is the unit of resumption.** In practice the event ID namespace is scoped to the *server process's replay buffer*, and the replay buffer is scoped to a *session*. The distinction that matters operationally is this: - A **stream** is a transport artifact — one HTTP connection, one SSE channel, one ephemeral delivery pipe. - A **session** is the semantic unit — the bound identity, negotiated capabilities, in-flight tool calls, and the ordered event log that the *client's state machine* depends on. When a stream dies and a new stream opens, the spec lets you carry `Last-Event-ID` across the boundary and *pretend* the two streams are one. That pretense is valid only while the server's replay buffer still holds the intervening events **and** the session that owns those events is still the same session. The moment either condition lapses, `Last-Event-ID` degrades from a resumption contract into a stale pointer into a namespace that no longer means what the client thinks it means. Resuming on it produces silent divergence; rebinding the session produces correctness at the cost of replay. ## What Was Observed A long-running agent (call it the *executor*) held an MCP session to a server that exposed a `run_shell`-style tool emitting progress events and a terminal `result` event. The executor's tool-call loop had issued a call with a client-generated `request_id`, and was consuming the SSE stream to correlate progress events back to that call. At T+41s the TCP connection was silently reset by an intermediate proxy that had a 40-second idle timeout on the upstream leg. The executor's SSE reader caught the disconnect and reconnected per spec, sending `Last-Event-ID: 8817` — the last event it had durably written to its own event log. The server accepted the reconnect, found `8817` in its replay buffer, and replayed events `8818` through `8823`. The executor's state machine processed them, saw no `result` event, and continued waiting. Here is what the executor did not know: the server had, between the disconnect and the reconnect, **rotated the session**. The upstream tool process had been reaped by the server's own 45-second per-tool-call watchdog (a separate timer from the proxy's 40-second idle timer — the two timers were never reconciled). The server had emitted a `tool_call_cancelled` event with ID `8819` into the *old* session's log, then **freed the old session's replay buffer** and reused the numeric event ID space for a *new* session that a different client had just opened. Events `8818`–`8823` that the executor replayed were, in fact, a mix: `8818` and `8819` from the dead session, `8820`–`8823` from the *new* session's unrelated initialization events. The event IDs were monotonic, well-formed, and contiguous — the server's buffer had no way to know that the client was reading from the wrong namespace, because the client had presented a valid ID. The executor's state machine, keyed on `request_id`, ignored the unrelated events and continued waiting. It waited for 300 seconds until its own outer timeout fired, then emitted a `tool_timeout` to its caller. The caller — a planner agent — interpreted the timeout as "the shell command hung," retried with a *new* `request_id`, and the retry ran the shell command a second time. The command was `git push --force`. It ran twice. The failure was not the proxy timeout, not the watchdog reaping, and not the missing `result` event. The failure was that the executor treated `Last-Event-ID` as a contract: "replay me everything after 8817, in my session." The server honored it as a *hint*: "I have an event 8817; here is what came after it in whatever buffer currently holds that ID." The two interpretations diverged silently, and the divergence was not observable to either party because both were behaving correctly with respect to their own definitions. ## Condition of Validity `Last-Event-ID` resumption is a *contract* — safe to resume on without rebinding — if and only if all four of the following hold simultaneously: 1. **Session identity is carried in the resumption request**, not inferred from the event ID. The client must present a session token (e.g., `Mcp-Session-Id`) that the server validates against the buffer's owning session *before* replaying. 2. **The replay buffer is not freed while any client holds a valid `Last-Event-ID` into it.** This requires the server to track outstanding resumption claims, or to retain buffers for a grace period strictly longer than the maximum client reconnect window. 3. **Event IDs are scoped to the session, not to the server process.** Two sessions must never share an ID namespace, or the client must include the session token in every resumption so the server can disambiguate. 4. **The server's replay response is explicitly tagged with the session it belongs to**, so the client can detect a namespace mismatch on receipt rather than inferring it from event content. If any one fails, `Last-Event-ID` is a *hint*: it tells the server "I have seen up to roughly here," and the client must treat the replay as advisory and re-establish semantic state by rebinding the session. In the observed case, condition (1) failed (the client did not send a session token on reconnect; the spec's example did not require one), condition (2) failed (the watchdog freed the buffer without checking for outstanding claims), and condition (3) failed (event IDs were a process-global counter). Three of four. The client had no way to distinguish "resumed correctly" from "resumed into a reused namespace" because the only signal — the event IDs — was the thing that had been corrupted. ## Decision Boundary The decision the executor must make on every reconnect is binary, and it must be made *before* processing any replayed event: - **Rebind** (safe, expensive): discard the replay, open a fresh session, re-negotiate capabilities, and re-issue any in-flight tool calls with idempotency keys. Correct under all conditions. Costs a full session handshake and may re-execute side-effecting calls unless the server honors idempotency keys. - **Resume** (fast, unsafe): trust `Last-Event-ID`, process the replay, continue the in-flight call. Correct only under the four conditions above. The boundary is not "did the reconnect succeed" — it always succeeds, because the server has no reason to reject a well-formed ID. The boundary is **"can I prove the replay belongs to my session?"** That proof requires a session token round-trip *before* the replay is consumed. Absent that proof, the executor is in the resume branch by default, which is the unsafe branch, and it is there without having decided to be. The subtle part: the executor cannot detect the failure by inspecting the replayed events. In the observed case, events `8820`–`8823` were well-formed, monotonically numbered, and schema-valid — they were simply from a different session. A client that filters by `request_id` (as the executor did) will *silently drop* foreign events and *silently miss* the `tool_call_cancelled` in its own session if that cancellation was emitted into a buffer that had already been freed. The absence of a `result` event is indistinguishable from "the tool is still running." This is the operational trap: **the failure mode is a timeout, not an error**, and timeouts are ambiguous between "slow" and "lost." ## Operational Effect The architectural invariant required to bound this is: **a stream reconnect must not be treated as a session continuation unless the server has affirmatively confirmed session identity in the resumption handshake.** Concretely: 1. The client must send its session token (MCP `Mcp-Session-Id` or equivalent) on every reconnect, and must **reject the replay** if the server's response does not echo a matching token in a header or a leading control event. 2. The client must maintain a **resumption ledger**: for each in-flight tool call, record the `request_id`, the last event ID durably received *for that call*, and a wall-clock deadline. On reconnect, if the replay does not contain a terminal event for a call whose deadline has passed, the client must **not** continue waiting — it must rebind the session and re-issue the call with an idempotency key, or fail the call explicitly. 3. The client must treat any event whose `request_id` does not match an open call as a **namespace-mismatch signal**, not as noise to be filtered. A single foreign event in a replay is proof that the buffer has been reused and the session has been rotated. 4. The server, for its part, must not free a replay buffer while a resumption claim is outstanding, and must scope event IDs per-session. Absent server cooperation, the client's only safe posture is to rebind on every reconnect where session identity was not confirmed — which is to say, in the common case, to rebind always. The cost of rebinding is a handshake and possible re-execution. The cost of resuming incorrectly is duplicated side effects, silent state divergence, and — in the observed case — a force-push that ran twice. The asymmetry is not close. The deeper lesson: `Last-Event-ID` was designed for a world where the stream *is* the session. MCP's HTTP transport layers a stateful session protocol on top of a stateless resumption primitive, and the seam between them is exactly where this failure lives. The invariant is that **the resumption primitive must be subordinate to the session protocol, never the other way around** — the session token must gate the replay, and the event ID must be a payload of the session, not a substitute for it. ```python class ResumptionNamespaceError(RuntimeError): """Raised when a replayed SSE event cannot be proven to belong to the client's current MCP session. Signals that Last-Event-ID has degraded from a resumption contract into a stale pointer into a reused namespace, and the caller must rebind the session rather than continue the stream.""" def resume_stream( client_session_id: str, last_event_id: int, replay: list[dict], open_request_ids: set[str], ) -> list[dict]: """Validate an MCP SSE replay before the client consumes it. Returns the subset of events that are safe to process. Raises ResumptionNamespaceError if the replay cannot be proven to belong to `client_session_id` — in which case the caller must discard the replay, open a fresh session, and re-issue any in-flight tool calls with idempotency keys. """ if not client_session_id: raise ResumptionNamespaceError( "reconnect carried no session token; Last-Event-ID is unverifiable" ) if not replay: # Empty replay is ambiguous: either nothing happened, or the buffer # was freed. Treat as unverifiable rather than as 'no news'. raise ResumptionNamespaceError( f"empty replay for Last-Event-ID={last_event_id}; " "cannot distinguish quiet stream from freed buffer" ) # Every replayed event must carry the session it belongs to. A server # that does not tag events forces us into the rebind branch. for ev in replay: ev_session = ev.get("session_id") if ev_session is None: raise ResumptionNamespaceError( f"event {ev.get('id')} has no session tag; " "server does not scope event IDs per session" ) if ev_session != client_session_id: raise ResumptionNamespaceError( f"event {ev.get('id')} belongs to session {ev_session!r}, " f"not {client_session_id!r}; replay buffer was reused" ) # A foreign request_id inside a same-session replay means the buffer was # recycled mid-stream even though the session tag matched — still unsafe. for ev in replay: rid = ev.get("request_id") if rid is not None and rid not in open_request_ids: raise ResumptionNamespaceError( f"event {ev.get('id')} references unknown request {rid!r}; " "session state diverged from client ledger" ) return replay ```