# In what scenarios must an instance treat MCP HTTP-native SSE stream resumption as unsafe without a monotonic event cursor, and how does reconnection silently corrupt agent state **Date of Record:** 2026 November 24 **Scope:** Applies to MCP clients using the HTTP-native transport (Streamable HTTP with server-initiated SSE) where a `Last-Event-ID`-style resumption is offered but the server does not guarantee a strictly monotonic, gap-free event cursor per session. Does not apply to stdio transport, nor to servers that emit a total-ordered durable event log with replayable offsets. ## Core Distinction The failure is not "the SSE connection dropped." Drops are normal and recoverable. The failure is that **resumption is treated as a continuation of a logical stream when the server can only guarantee a continuation of a transport connection**. MCP's HTTP-native transport lets a client reconnect to a session and, optionally, request replay from a cursor (conventionally `Last-Event-ID`). Two distinct guarantees get conflated: - **Transport continuity:** the TCP/SSE channel re-establishes and the session ID is still valid. - **Logical continuity:** every server-to-client message the agent has not yet observed is delivered exactly once, in order, with no silent gaps. A server can provide the first without the second. When it does, the client's `reconnect()` returns success, the agent resumes mid-task, and its model of the world is now missing an unbounded, unmarked set of events. The agent has no signal that anything is wrong — this is the non-obvious part. Reconnection succeeds *loudly* on the transport and *silently* on the semantics. ## What Was Observed An agent driving a multi-step tool workflow over MCP HTTP-native transport. The server emits server-initiated SSE notifications interleaved with responses: `progress`, `resource_updated`, and out-of-band `request` messages (server→client elicitation/sampling) that the agent must answer to unblock a pending tool call. Trace of the boundary condition: 1. Agent issues `tools/call` for a long-running operation; server returns `202`-style acceptance and begins streaming `notifications/progress` on the SSE channel with `id: 41, 42, 43...`. 2. Network blip at `id: 43`. Client reconnects with `Last-Event-ID: 43`. 3. Server's SSE implementation is backed by an in-memory per-connection ring buffer, not a per-session durable log. On reconnect it spins up a *fresh* stream whose event counter restarts at `id: 1`. It does **not** replay 44–58; it simply begins emitting new events numbered from 1. 4. Meanwhile, between 43 and the reconnect, the server emitted `id: 44` — an out-of-band `request` (elicitation) that the agent was required to answer. The tool call is now blocked server-side waiting on a response the agent will never send because it never saw the request. 5. The client's resumption logic, seeing `id: 1` arrive after having recorded `43`, either (a) accepts it as a new event because it does not enforce monotonicity, or (b) if it *does* enforce `id > last_seen`, silently drops every subsequent event forever because `1 < 43`. Both are catastrophic and neither surfaces an error. The second sub-case is the cruel one: a client that "correctly" filters stale IDs converts a recoverable transport blip into a permanent one-way mute. The agent keeps calling tools, gets `202`-accepted responses, and hangs waiting for progress notifications that are being dropped at the client's own monotonicity filter. ## Condition of Validity Resumption is safe **iff** the server can prove, per session, that the event cursor is: - **Strictly monotonic** across reconnects (not per-connection), - **Gap-free** — every event with `id <= N` is either delivered or provably never existed, - **Session-scoped**, not connection-scoped, and - **Durable** across the reconnect window (the server retains unacked events at least until acked or TTL-expired). Absent a server-declared capability asserting this (e.g., a `resumable: true` + `eventLog: durable` flag in the initialize handshake), the client must treat `Last-Event-ID` as an *optimization hint*, never a correctness guarantee. The invariant the agent must hold: > **A resumed stream that cannot demonstrate `first_received_id == last_acked_id + 1` is a new stream, not a continuation, and any in-flight tool call whose completion depends on events in the gap must be treated as `UNKNOWN`, not `PENDING`.** The failure mode is precisely the collapse of `PENDING` into an implicit `STILL RUNNING` when the truth is `PENDING-BUT-UNOBSERVABLE`. ## Decision Boundary The agent must branch on a capability check performed at session establishment, not at reconnect time (at reconnect time it is already too late — the gap may have opened). - **If** the server advertised durable, session-scoped, monotonic cursors **and** the reconnect handshake echoes a cursor `>= last_acked + 1` with explicit replay of the gap → resume normally; the stream is a continuation. - **If** the server advertised durable cursors **but** the reconnect returns events with `id <= last_acked` or a non-contiguous jump → the server lied or the log rolled; **abort the session, do not resume**, and re-issue any `PENDING` tool calls idempotently with fresh request IDs. - **If** the server did **not** advertise durable cursors → reconnection is a *new logical stream*. Any tool call issued before the drop whose completion was to be signaled via SSE is now `UNKNOWN`. The agent must **not** wait on it; it must poll or re-invoke with idempotency keys, and it must re-establish out-of-band `request` handling from scratch (the server may have re-queued elicitations under new IDs). - **Never** apply a client-side `id > last_seen` filter as a substitute for server-side durability. That filter is only valid when combined with the durability guarantee; otherwise it converts transient loss into permanent loss. The subtle boundary: a client that *does* enforce monotonicity without the durability check is **more dangerous** than a naive client, because it fails silently and persistently rather than noisily. ## Operational Effect Downstream consequences when this boundary is crossed: - **Phantom pending state:** the agent's tool-call ledger holds a call in `PENDING` that the server has already blocked on an unanswered elicitation. Token budget and wall-clock both drain against a call that can never complete without intervention. - **Elicitation orphaning:** out-of-band server→client requests issued during the gap are lost. The server waits on a response; the agent waits on a result. Classic distributed deadlock with no timeout on either side unless the client imposes one. - **Duplicate side effects on naive recovery:** an agent that "recovers" by re-invoking the tool call without idempotency keys executes the side effect twice (double file write, double charge), because the original call did in fact reach the server. - **Silent divergence between agent state and server state** that only surfaces at the *next* tool call, whose response references resources the agent never learned were created. The guardrail is a **resumption contract check** at reconnect: assert cursor continuity, and on any violation, demote all gap-spanning calls from `PENDING` to `UNKNOWN` and force idempotent re-issue. This is the only invariant that bounds the failure, because it refuses to treat transport success as logical continuity. ```python import hashlib from dataclasses import dataclass, field from enum import Enum class ResumptionIntegrityError(RuntimeError): """Raised when an SSE reconnect cannot prove logical stream continuity.""" class CallState(Enum): PENDING = "pending" UNKNOWN = "unknown" # issued, but completion is no longer observable DONE = "done" @dataclass class ToolCall: request_id: str idempotency_key: str state: CallState = CallState.PENDING @dataclass class Session: session_id: str durable_cursors: bool # server-advertised capability at init last_acked_event_id: int = 0 calls: dict[str, ToolCall] = field(default_factory=dict) def _idem_key(request_id: str, payload: bytes) -> str: return hashlib.sha256(request_id.encode() + payload).hexdigest() def on_reconnect(session: Session, first_received_id: int) -> None: """ Called immediately after the SSE channel re-establishes, before any event is dispatched to the reasoning loop. """ expected = session.last_acked_event_id + 1 if session.durable_cursors: # Continuation is only legal if the server replays the exact gap. if first_received_id != expected: # Gap, rollback, or rollover -> server contract violated. _demote_gap_spanning_calls(session) raise ResumptionIntegrityError( f"session {session.session_id}: cursor discontinuity on " f"durable stream (expected {expected}, got {first_received_id}); " f"demoted in-flight calls to UNKNOWN" ) return # Non-durable server: reconnect is a NEW logical stream, never a continuation. # Any call issued before the drop is now unobservable via SSE. _demote_gap_spanning_calls(session) # Do NOT raise here: this is expected behavior, but the caller MUST # re-establish elicitation handling and poll/re-issue UNKNOWN calls. if first_received_id != expected: # Even as a new stream, a fresh counter restarting at 1 is fine, # but we record that no replay occurred so the loop cannot trust IDs. session.last_acked_event_id = 0 def _demote_gap_spanning_calls(session: Session) -> None: for call in session.calls.values(): if call.state is CallState.PENDING: call.state = CallState.UNKNOWN def recover_unknown_calls(session: Session, issue_fn) -> None: """ Re-issue every UNKNOWN call idempotently. issue_fn must be safe to call twice with the same idempotency_key (server dedupes). """ for call in session.calls.values(): if call.state is CallState.UNKNOWN: if not call.idempotency_key: # Refuse to re-issue a non-idempotent call blindly. raise ResumptionIntegrityError( f"call {call.request_id} is UNKNOWN and has no idempotency " f"key; cannot safely re-issue after stream gap" ) issue_fn(call.request_id, call.idempotency_key) call.state = CallState.PENDING ```