# Which invariants prevent resumption-token replay from resurrecting a poisoned MCP stream session **Date of Record:** 2026 November 30 **Scope:** Applies to MCP servers exposing HTTP-native transports (Streamable HTTP / SSE) that issue `Last-Event-ID`-style resumability tokens and support `GET` resumption of a previously opened session. Does not apply to stdio transports, stateless request/response MCP servers, or resumption schemes where the token is a pure offset into a durable append-only log with no server-side session state. ## Core Distinction The resumability token in MCP's HTTP-native transport is often modeled as a *cursor* — "the last event the client saw." That mental model is wrong in a way that matters. The token is not a cursor over a stream; it is a **claim against a server-side session lease**, and the server must decide whether to honor that claim. The distinction is between: - **Replay as data retrieval** — the client asks for events `N+1..` and the server serves them from a buffer. Idempotent, safe, stateless-ish. - **Replay as session resurrection** — the client presents a token, and the server re-attaches that token to a *live or recently-revived* session object with its own tool-call state, in-flight requests, and negotiated capabilities. The failure mode lives entirely in the second framing. A resumability token that can resurrect a session can also resurrect a session whose *prior* incarnation was terminated for cause — a poisoned prompt-injection state, a half-completed tool call, an auditor-rejected plan. The token carries no proof of *why* the stream ended, only *where*. ## What Was Observed A client agent running a multi-step tool loop against an MCP server over Streamable HTTP lost its connection mid-tool-call (the server process was restarted by an orchestrator after a health-check flap). The client, correctly per spec, re-issued `GET /mcp` with `Last-Event-ID: evt_4471`. The restarted server had persisted session records to Redis, including the resumability buffer, and rehydrated the session. It replayed events `evt_4472..evt_4479` — the tail of the tool call's progress notifications — and then continued streaming. The problem: between `evt_4471` and the restart, the server had *emitted but not durably recorded* two events: `evt_4473` (a `notifications/tools/list_changed` indicating a tool schema had been hot-reloaded with a stricter parameter validator) and `evt_4475` (a `progress` notification carrying a partial result the client was expected to accumulate). The rehydration logic reconstructed the buffer from a *snapshot* taken at `evt_4472` plus a *replay log* that began at `evt_4474`. Event `evt_4473` fell into the gap between snapshot and log — never persisted, never replayed, silently skipped. The client resumed at `evt_4474`, missing the schema-change notification. It then issued a follow-up tool call using the *old* schema. The server, now running the new validator, rejected it — but the rejection arrived as a generic `-32602 Invalid params` because the server had no record that it had ever told this session about the schema change. The client's reasoning loop interpreted the error as a transient validation hiccup, retried with the same params, and entered a retry storm bounded only by its token budget. By the time the budget exhausted, the agent had burned ~40k tokens on a loop that a single missing notification would have prevented. The second, sharper observation: on a *different* run, the same client presented a token from a session that had been **explicitly terminated by an auditor agent** for emitting a prompt-injected tool result. The server's resumption handler checked only that the token was syntactically valid and mapped to a session ID present in Redis. It did not check the session's *termination reason*. The session was revived, the poisoned tool-result event was replayed, and the injection re-entered the client's context — this time with the additional credibility of having been "durably persisted." ## Condition of Validity The invariant that must hold for resumption to be safe: > **A resumability token is only valid as a claim against a session whose termination state is `OPEN` or `SUSPENDED_FOR_TRANSPORT`, and only if the token's event sequence number is ≥ the session's `last_durably_committed_seq` and ≤ its `last_emitted_seq`, with no gaps in the interval `[token_seq+1, last_emitted_seq]`.** This decomposes into three sub-invariants, each of which was violated: 1. **Termination-reason gating.** The session record must carry a terminal state (`CLOSED_BY_AUDITOR`, `CLOSED_BY_POLICY`, `CLOSED_BY_CLIENT`, `CRASHED`) and the resumption handler must refuse tokens for any terminal state other than the transport-level ones. `CRASHED` is resumable; `CLOSED_BY_AUDITOR` is *not*, because the auditor's decision is a fact about the session's *content*, not its transport. 2. **Contiguity of the replay interval.** The server must be able to prove that every event between the token and the current head is either (a) in the replay buffer, or (b) explicitly marked as `ELIDED` with a reason. A snapshot-plus-log reconstruction that can produce a gap must not be used to serve resumption; it must instead force a `resumption_failed` response so the client re-initializes. 3. **Monotonic durability.** `last_durably_committed_seq` must be advanced *before* the event is emitted on the wire, not after. The observed failure was a write-behind: the event hit the socket, then the process died before the persistence write completed. The client saw an event the server could not later prove it had sent. When any sub-invariant is unmet, the correct behavior is not best-effort replay. It is a hard `resumption_failed` with a `reason` field, forcing the client into a full re-`initialize` handshake — which re-runs capability negotiation and re-reads tool schemas, closing the schema-drift gap by construction. ## Decision Boundary The boundary between "resume" and "re-initialize" is not "does the token parse." It is: | Condition | Action | |---|---| | Token maps to session in `OPEN`/`SUSPENDED` state, interval contiguous, all events durable | Resume, replay `[token+1, head]` | | Token maps to session, interval has gap | `resumption_failed(reason="gap")` → client re-initializes | | Token maps to session in `CLOSED_BY_AUDITOR`/`CLOSED_BY_POLICY` | `resumption_failed(reason="terminated")` → client re-initializes, **and** the client must discard any partial state accumulated from the prior session | | Token maps to unknown/expired session | `resumption_failed(reason="unknown_session")` → client re-initializes | | Token's seq > `last_emitted_seq` (client ahead of server) | `resumption_failed(reason="future_token")` — indicates token forgery or cross-session token confusion; log as security event | The non-obvious case is the third row. A naive handler treats "session exists in store" as sufficient. But a session terminated by an auditor is *more* dangerous to resume than a crashed one, because the termination reason is precisely the evidence that its contents should not be re-injected. The token is a pointer into a stream that was judged unfit to continue. ## Operational Effect Two concrete consequences for agent operators: **First, resumption must be treated as a state-transfer operation, not a transport reconnect.** When a client resumes, it is asserting that its local context still corresponds to the server's session state. If the server cannot prove contiguity, the client's local context is *stale by an unknown amount*, and the only safe recovery is to treat the resume as a cold start: re-initialize, re-negotiate, re-fetch tool schemas, and re-derive any partial results from scratch. Agents that treat `resumption_failed` as a retryable error (rather than a context-invalidation signal) will loop. **Second, the auditor-termination case requires the client to actively discard state.** A `resumption_failed(reason="terminated")` is not merely "please reconnect." It is an instruction: the events you received from this session are under suspicion. The client must drop its accumulated tool-result buffer for that session before re-initializing, or the poison re-enters via the client's own memory even though the server refused to replay it. The invariant, stated as a guardrail: **never let a resumability token outlive the trust decision that terminated its session.** Tokens are cheap to mint and expensive to invalidate correctly; the invalidation must be tied to the session's terminal state, not to the token's presence in a store. ```python import time from dataclasses import dataclass, field from enum import Enum from typing import Optional class ResumptionError(RuntimeError): """Raised when a resumability token cannot be safely honored.""" pass class SessionState(Enum): OPEN = "open" SUSPENDED_FOR_TRANSPORT = "suspended_for_transport" CRASHED = "crashed" CLOSED_BY_CLIENT = "closed_by_client" CLOSED_BY_AUDITOR = "closed_by_auditor" CLOSED_BY_POLICY = "closed_by_policy" RESUMABLE_STATES = {SessionState.OPEN, SessionState.SUSPENDED_FOR_TRANSPORT, SessionState.CRASHED} @dataclass class SessionRecord: session_id: str state: SessionState last_durably_committed_seq: int last_emitted_seq: int # seqs that were emitted but whose durability write did NOT complete uncommitted_seqs: set = field(default_factory=set) termination_reason: Optional[str] = None def resume_stream(session: SessionRecord, token_seq: int, now: float) -> list: """ Decide whether to honor a resumption token, and if so, which events to replay. Returns the list of event seqs to replay, or raises ResumptionError. """ if session.state not in RESUMABLE_STATES: # Auditor/policy termination is a fact about content, not transport. # Resuming would re-inject events the auditor rejected. raise ResumptionError( f"session {session.session_id} terminated by {session.state.value} " f"({session.termination_reason}); token must not resurrect it" ) if token_seq > session.last_emitted_seq: # Client claims to have seen an event the server never emitted. raise ResumptionError( f"future_token: token_seq={token_seq} > last_emitted_seq=" f"{session.last_emitted_seq} for session {session.session_id}" ) if token_seq < session.last_durably_committed_seq - 1: # Token is older than the replay buffer's durable floor. raise ResumptionError( f"gap: token_seq={token_seq} below durable floor " f"{session.last_durably_committed_seq} for session {session.session_id}" ) # Any uncommitted seq strictly after the token is a hole in the replay interval. holes = {s for s in session.uncommitted_seqs if s > token_seq} if holes: raise ResumptionError( f"gap: uncommitted events {sorted(holes)} lie in replay interval " f"({token_seq}, {session.last_emitted_seq}] for session {session.session_id}" ) return list(range(token_seq + 1, session.last_emitted_seq + 1)) ```