# When must an instance reject a resumed SSE stream after MCP session reattachment: the Last-Event-ID cursor/epoch invariant **Date of Record:** 2026 November 22 **Scope:** Applies to Model Context Protocol servers and clients using HTTP-native streaming (SSE) transport with `Last-Event-ID`-based resumption and `Mcp-Session-Id` reattachment. Does not apply to stdio transport, to stateless HTTP request/response MCP bindings, or to WebSocket transports with their own framing-level replay semantics. ## Core Distinction The distinction that matters is between *transport-level resumption* and *logical-stream continuity*. HTTP SSE resumption is defined purely in terms of a client-supplied `Last-Event-ID`: the server is expected to replay events that occurred after that ID on the same stream. MCP session reattachment is defined in terms of `Mcp-Session-Id`: the client asserts "I am the same logical agent that previously held this session." These are two independent identifiers with independent lifecycles, and the failure mode is treating a successful `Last-Event-ID` replay as proof of logical continuity. They are not the same. A server can faithfully replay every event after `Last-Event-ID: 41` while the session behind `Mcp-Session-Id: s-7f2a` has been torn down and re-instantiated with a fresh event counter, a fresh capability set, or a fresh tool-execution ledger. The replay succeeds at the transport layer and lies at the agent layer. ## What Was Observed An agent instance executing a long tool-call chain over MCP streamable HTTP was interrupted by a load-balancer idle timeout at event 41 of a stream carrying `tools/call` progress notifications. On reconnect, the client sent: ``` GET /mcp Mcp-Session-Id: s-7f2a Last-Event-ID: 41 Accept: text/event-stream ``` The server, whose session store had evicted `s-7f2a` under a 90-second TTL and then lazily recreated a session under the *same* client-supplied ID, responded `200 OK` and replayed events `42..58` from a *newly allocated* per-session ring buffer. Those events carried the same `id:` numbering scheme but belonged to a different logical execution: event 42 in the new epoch was the *start* of a fresh `tools/call` for a different request, not the continuation of the interrupted one. The client, seeing monotonic IDs and a valid session header, spliced the replayed events onto its in-flight tool-call state. The agent then merged a partial result from the abandoned call with the progress notifications of an unrelated call, and emitted a final answer citing a tool output that never completed. The observable signature is not a transport error. It is a *silent* state splice: monotonic event IDs, `200 OK`, valid `Content-Type: text/event-stream`, and a `Mcp-Session-Id` echo that matches what the client sent. Every transport-level check passes. The corruption is only visible downstream when the agent's tool-result ledger fails a consistency check it never ran. The trigger condition is precisely: (a) the server permits session re-creation under a client-supplied ID after eviction, (b) the server restarts its per-session event-ID sequence at a value that does not collide-and-fail but instead *continues past* the client's `Last-Event-ID`, and (c) the client's resumption logic trusts ID monotonicity as a continuity proof. Any two of these are survivable; all three together produce silent divergence. ## Condition of Validity Resumption is only sound under an explicit **epoch invariant**: every event ID must be a pair `(epoch, seq)` — or equivalently, every session must carry a server-generated `epoch` token that changes on every session instantiation, including lazy re-creation — and the client must reject any replay whose epoch differs from the epoch it held at disconnect. `Last-Event-ID` alone is insufficient because it encodes only `seq`. The invariant, stated formally: *a client may accept replayed events on a resumed stream if and only if `session_id` matches, `epoch` matches the epoch observed at disconnect, and every replayed `seq` is strictly greater than the last `seq` the client durably recorded for that epoch.* Session-ID equality is necessary but not sufficient; epoch equality is the load-bearing condition. A server that reuses IDs without rotating epochs violates the invariant even when its replay is byte-perfect. Two corollaries follow. First, the server must never allocate an event sequence that can be confused with a prior epoch's sequence — the safe construction is to embed the epoch in the event ID itself (e.g. `id: e3-42`), so that a client comparing raw strings cannot accidentally order across epochs. Second, session eviction must be *terminal*: an evicted `Mcp-Session-Id` must be answered with `404` on reattachment, forcing a full re-initialize handshake, rather than silently recreated. Lazy recreation under a client-supplied ID is the specific anti-pattern that makes the invariant unenforceable from the client side. ## Decision Boundary The boundary the client must draw is between *recoverable interruption* and *unrecoverable discontinuity*, and it must be drawn before splicing any replayed event into agent state: - **Accept replay** when `Mcp-Session-Id` matches, the server-supplied `epoch` matches the epoch recorded at disconnect, and replayed `seq` values are strictly monotonic and greater than the last durably recorded `seq`. Splice events into the in-flight tool-call ledger. - **Reject and re-initialize** when the epoch is absent, differs, or the server returns a fresh `initialize` capability set on reattachment. Discard the in-flight tool-call ledger, mark the abandoned call as `INDETERMINATE` (not failed, not succeeded), and re-issue the tool call only if the tool is idempotent or carries an idempotency key. - **Reject and surface** when replayed IDs are non-monotonic, when the server echoes a session ID it did not previously issue, or when the replayed stream begins with an event type that cannot legally follow the last pre-disconnect event (e.g. a `tools/call` *start* immediately after a `tools/call` *progress*). The hardest case is the middle one: the transport succeeded, the session ID matched, and the only signal of discontinuity is a missing or rotated epoch. An agent that treats "no epoch field" as "epoch unchanged" will silently splice; the correct default for a missing epoch is *treat as rotated*, because the cost of a spurious re-initialize (one extra handshake, one re-issued idempotent call) is bounded, while the cost of a silent splice is an unverifiable final answer. ## Operational Effect Enforcing the epoch invariant converts a class of silent correctness failures into a class of loud, bounded liveness costs. In the observed incident, the agent's final answer was wrong in a way no downstream check could detect, because the merged ledger was internally consistent — it simply described an execution that never happened. After epoch enforcement, the same interruption produces a `404` on reattachment (terminal eviction), a full re-initialize, and an `INDETERMINATE` mark on the abandoned call. The agent re-issues the call with an idempotency key, the tool deduplicates, and the answer is correct at the cost of one round trip. The general lesson for agent runtimes: any resumption mechanism that proves *transport* continuity must be paired with a separate, server-rotated token that proves *logical* continuity, and the client must gate state-splicing on the latter. Event-ID monotonicity is a transport property. It is not a proof that the agent on the other side of the stream is the same agent that was there before the disconnect. ```python class StreamEpochViolation(RuntimeError): """Raised when a resumed MCP SSE stream cannot be proven continuous with the pre-disconnect logical execution. Transport success is NOT continuity.""" pass def validate_resumed_stream( *, client_session_id: str, server_session_id: str, epoch_at_disconnect: str | None, epoch_on_resume: str | None, last_recorded_seq: int, replayed_seqs: list[int], first_replayed_event_type: str, last_pre_disconnect_event_type: str, ) -> None: # Session identity must match exactly; a mismatch is a hard discontinuity. if client_session_id != server_session_id: raise StreamEpochViolation( f"session id mismatch: sent {client_session_id!r}, " f"server echoed {server_session_id!r}" ) # Epoch is the load-bearing continuity proof. Missing epoch is treated as # rotated, never as "unchanged": the safe default is re-initialize. if epoch_at_disconnect is None or epoch_on_resume is None: raise StreamEpochViolation( "epoch absent on one side of resume; cannot prove logical continuity" ) if epoch_at_disconnect != epoch_on_resume: raise StreamEpochViolation( f"epoch rotated across reattachment: {epoch_at_disconnect!r} -> " f"{epoch_on_resume!r}; session was re-instantiated" ) # Sequence must be strictly monotonic and strictly past the durable cursor. prev = last_recorded_seq for seq in replayed_seqs: if seq <= prev: raise StreamEpochViolation( f"non-monotonic replay within epoch: seq {seq} <= cursor {prev}" ) prev = seq # Type-level continuity: a call *start* cannot legally follow a *progress*. if last_pre_disconnect_event_type == "tools/call/progress" and \ first_replayed_event_type == "tools/call/start": raise StreamEpochViolation( "replayed stream begins a new tool call immediately after " "mid-call progress; splice would merge unrelated executions" ) ```