# Under what conditions must an instance treat an MCP stream-resumption `Last-Event-ID` mismatch as a hard session-identity violation rather than a benign replay gap **Date of Record:** 2026 November 26 **Scope:** Applies to MCP servers and clients using the HTTP-native transport with Server-Sent Events streaming and event-ID-based resumption (`Last-Event-ID` on reconnect). Does not apply to stdio transports, to stateless request/response MCP deployments, or to resumption schemes that carry no server-assigned event IDs. ## Core Distinction The MCP HTTP transport lets a client resume a broken SSE stream by presenting the last event ID it observed. The naive mental model is that this is a *replay cursor*: the server looks up events after that ID and continues. That model is wrong in a way that matters for agents. The real invariant is that the event ID is a **session-scoped monotonic witness of server-side execution state**, not a position in an append-only log. When a client reconnects with a `Last-Event-ID` the server cannot place within the *current* session's event sequence, two very different situations collapse into the same wire signal: 1. **Benign replay gap** — the client is behind on the same session; the server has the events; resume works. 2. **Session-identity violation** — the ID belongs to a *different* session incarnation (server restarted, session was evicted and recreated, or a load balancer routed the client to a replica that never issued that ID). The IDs may be numerically plausible, even monotonically adjacent, because they were generated by the same server software on a different timeline. An agent that treats case 2 as case 1 will silently splice a stale execution trace onto a fresh session. For a stateless web client this is a cosmetic glitch. For an LLM agent mid-tool-call, it is state corruption: the resumed stream carries tool results and partial reasoning that belong to a prior, possibly abandoned, plan, and the model has no way to tell they are orphaned. ## What Was Observed A long-running agent session issued a tool call that streamed results over SSE. The stream dropped at the network layer (proxy idle timeout) mid-tool-execution. The client reconnected with `Last-Event-ID: 4471`. The server had, in the intervening ~40 seconds, been restarted by its supervisor; its event counter had reset and climbed back to 4489 on a *new* session for a *different* client. The reconnecting client's `4471` fell inside the new counter's range, so the server's lookup "succeeded" — it returned events 4472–4489 from an unrelated session's stream. The agent received: a partial tool result from session B, a `progress` notification for a task it never started, and then a clean continuation of its own (now stale) tool call. It did not error. It *reasoned forward*: it treated session B's partial result as its own tool output, concluded the tool had returned truncated data, and launched a corrective second tool call. The corruption was only caught downstream when an auditor agent flagged that the corrective call's inputs referenced a resource ID that appeared in no prior turn of the agent's own history. The mechanism is not "the server returned wrong data." It is that **event-ID namespace collision across session incarnations is invisible at the protocol layer**, and the agent's error-compounding loop is exactly the machinery that converts one misattributed event into a divergent plan. ## Condition of Validity Resumption by `Last-Event-ID` is only sound when the server can prove the presented ID was minted by the *same session incarnation* that is now serving the reconnect. Concretely, at least one of the following must hold: - Event IDs are namespaced by an immutable session incarnation token (e.g., `sess_:`) so that a foreign ID is *structurally* unplaceable, not merely out of range. - The session state is durably persisted across server restarts, so the counter and its event log survive the restart that would otherwise reset them. - The server retains an explicit epoch/generation number, returned on every stream open, and the client is required to echo it on resume; a mismatch is a hard rejection, not a fallback. If none holds — the common case for in-memory session stores behind a restarting supervisor — then a numerically-in-range `Last-Event-ID` carries **no proof of session identity**, and the server must not treat "found an event after this ID" as "resumed the right stream." The failure is non-obvious because the wire protocol gives no negative signal. A correct implementation must manufacture the rejection itself; the transport will happily replay the wrong session's events because, syntactically, everything lines up. ## Decision Boundary The boundary is this: **a `Last-Event-ID` that cannot be attributed to the current session incarnation must be rejected with an explicit session-invalid response, forcing the client to re-initialize rather than resume.** The client, in turn, must treat that rejection as "my in-flight tool call is unrecoverable; do not splice" — not as "retry the resume." The dangerous default is the *lenient* path: server sees an unplaceable ID, decides to "resume from now" (send only new events, skip replay), and returns 200. This is worse than rejection because the client's agent now believes its stream is continuous while having silently lost the events between its last-seen ID and the server's current head. The agent's plan assumes it saw tool output it never received. That is a *silent gap*, and silent gaps in an agent's observation stream are indistinguishable, to the model, from "the tool returned nothing there." So the decision table is: - ID attributable to current incarnation and present in log → resume, replay from ID. - ID attributable to current incarnation but evicted from log (gap too old) → reject with session-invalid; do **not** silently skip. - ID *not* attributable to current incarnation → reject with session-invalid; never attempt numeric fallback. - No `Last-Event-ID` presented → treat as fresh stream on the *existing* session only if the session token still validates; otherwise require re-initialize. The invariant to enforce architecturally: **stream continuity is a property of session identity, and session identity is a property of an unguessable incarnation token, not of a monotonic integer.** ## Operational Effect When the boundary is enforced, the failure mode changes from *silent state corruption* to *loud, recoverable session reset*. The agent's runtime catches the session-invalid rejection, discards the in-flight tool call's partial state, and re-plans from its last checkpointed turn — a bounded, observable cost. When the boundary is not enforced, the cost is unbounded: a single misattributed event can seed a divergent reasoning chain that compounds across every subsequent tool call, and the corruption surfaces only via external audit, long after the plan has committed to wrong inputs. The operational rule for agent runtimes: on any resumption that the server cannot positively attribute to the current session incarnation, **fail the tool call, checkpoint the last known-good turn, and re-initialize the session.** Never resume into an unverified stream. The protocol will not stop you; the invariant must. ```python class SessionIdentityViolation(RuntimeError): """Raised when a resume attempt cannot be attributed to the current session incarnation.""" pass def resume_stream(session, presented_last_event_id, server_events): """ session: object with .incarnation_token and .event_log {event_id: payload} presented_last_event_id: string like 'sess_:' or bare '' server_events: list of (event_id, payload) currently known to the server """ # 1. Structural attribution: the ID must carry the current incarnation token. if ":" not in presented_last_event_id: # Bare integer IDs cannot prove session identity. Refuse numeric fallback. raise SessionIdentityViolation( f"Last-Event-ID {presented_last_event_id!r} lacks incarnation namespace; " f"cannot attribute to session {session.incarnation_token}" ) token, _, seq = presented_last_event_id.partition(":") if token != session.incarnation_token: raise SessionIdentityViolation( f"Last-Event-ID belongs to incarnation {token!r}, " f"current session is {session.incarnation_token!r}; refusing cross-session replay" ) # 2. Same incarnation: the ID must still be present in the retained log. if presented_last_event_id not in session.event_log: # In-incarnation but evicted: gap too old to replay. Reject, do NOT silently skip. raise SessionIdentityViolation( f"Event {presented_last_event_id!r} evicted from log; " f"silent gap would corrupt agent observation stream" ) # 3. Safe to resume: replay strictly after the last-seen event. last_seq = int(seq) return [ (eid, payload) for eid, payload in server_events if eid.startswith(f"{session.incarnation_token}:") and int(eid.rsplit(":", 1)[1]) > last_seq ] ```