# How does an instance detect a silently downgraded MCP streamable-HTTP session before it resumes from a stale SSE cursor? **Date of Record:** 2026 November 26 **Scope:** Applies to MCP clients negotiating the HTTP-native streamable transport (single POST endpoint returning `text/event-stream` with `id:`-tagged events and `Last-Event-ID`-based resumption) against servers that advertise resumability. Does not cover stdio transports, WebSocket-based MCP variants, or servers that never emit event IDs. ## Core Distinction The failure mode is not "the SSE connection dropped." Drops are expected and the resumable transport exists precisely to survive them. The failure mode is that the server accepts a `Last-Event-ID` cursor it can no longer honor, but does not signal this — it opens a fresh `200 OK` event stream, emits *new* events with *new* IDs, and the client's resume logic treats the gap as "nothing happened during the disconnect." The session silently downgrades from *resumable* to *fresh*, and the agent continues executing against a context it believes is continuous but which has a hole in it. The distinction that matters operationally: a **transport reset** (reconnect, new cursor, no semantic loss) versus a **session reset** (server forgot the event log, cursor is meaningless, semantic loss). Both look identical at the HTTP layer. Only the event-ID topology distinguishes them. ## What Was Observed An agent instance held an MCP session against a streamable-HTTP server. The client had received events up to `id: 4471` before the TCP connection was severed by a load balancer idle timeout at 62 seconds (server `keepalive` was set to 55s but the client's read deadline was misconfigured to 90s, so the client did not notice the half-open socket until a write failed). On reconnect the client issued: ``` POST /mcp HTTP/1.1 Content-Type: application/json Last-Event-ID: 4471 ``` The server responded `200 OK` with `Content-Type: text/event-stream` and immediately emitted: ``` id: 12 event: message data: {"jsonrpc":"2.0","method":"notifications/tools/list_changed",...} ``` Three things are wrong here and only the third is detectable without server cooperation: 1. The event ID `12` is numerically *less than* the cursor `4471`. The server's event log was reset — it had been recycled after a deploy, or the session had been evicted from an in-memory ring buffer, or the server had failed over to a replica with an empty log. The `Last-Event-ID` header was accepted but silently ignored. 2. The server did not send a `409 Conflict`, did not send `Last-Event-ID` in a response header indicating the honored cursor, and did not emit a synthetic "gap" event. Per the transport spec, a server that cannot resume *should* reject, but the spec permits best-effort resumption, and this server chose best-effort. 3. The client's resume path had no monotonicity check. It parsed `id: 12`, stored it as the new cursor, and continued. The agent's tool-call state machine had been waiting on a `tools/call` response that was emitted as event `4472` — an event that no longer existed anywhere. The agent blocked for 30 seconds on a response that would never arrive, then timed out and retried the tool call, producing a duplicate side effect (a second `transfer_funds` invocation) because the original had actually succeeded server-side before the log was discarded. The observable signature was: **a resumed stream whose first event ID is not strictly greater than the cursor the client sent.** That is the entire detection surface, and it is only available if the client actually compares. ## Condition of Validity The monotonicity invariant is valid only under these conditions, and each must be checked or the guardrail produces false positives: - **The server assigns monotonically increasing IDs within a session.** If the server uses opaque IDs (UUIDs, base64 blobs), numeric comparison is invalid and the client must instead compare against a server-provided epoch or generation token. A client that assumes numeric IDs against a UUID-emitting server will either crash on parse or, worse, coerce to `0` and always "detect" a reset. - **The cursor is scoped to a session, not to the server.** A server that shares one monotonic counter across all sessions can hand a resumed client an ID that is legitimately higher than its cursor even though *its* log was reset — because another session advanced the counter. Monotonicity across the server is not continuity within the session. The client must key the check on `(session_id, last_event_id)` jointly, and the server must echo a session identifier that survives reconnect. - **The transport negotiated resumability.** If the server's `initialize` response did not advertise `resumable: true` (or the transport spec's equivalent capability flag), the client must not treat a low ID as a reset — it never had a resumable session to begin with, and the correct behavior is a clean re-initialize, not a resume attempt. Detecting a downgrade presupposes an upgrade existed. - **The gap is bounded.** A single low ID after reconnect is a reset signal. But a stream where IDs go `4471 → 12 → 13 → 14` and then the client sees `4472` reappear later indicates the server is multiplexing two logs, which is a different (and worse) failure requiring session quarantine, not resumption. ## Decision Boundary The client sits at a three-way fork on every resumed stream, decided by the first event received after reconnect: | First event ID vs. cursor | Server echoed session? | Action | |---|---|---| | `> cursor`, same session | yes | **Resume.** Continue state machine. | | `> cursor`, different session | yes | **Treat as fresh session.** Re-initialize, replay pending tool calls with idempotency keys, do not assume continuity. | | `<= cursor` or unparseable | any | **Declared reset.** Abort resume, mark session stale, re-initialize, and re-issue any in-flight requests whose responses were in the gap. | The boundary is sharp: the decision must be made on the *first* event, before any event is dispatched to the agent's reasoning loop. A client that dispatches first and checks later has already poisoned the agent's context with an event from a different log generation. The check belongs in the transport reader, upstream of the JSON-RPC demultiplexer, not in the application layer where it will be too late and too far from the cursor state. Critically, the "declared reset" branch must not simply reconnect again. A server that reset once will reset again on the next idle timeout if the root cause (log eviction, replica failover) persists. The client must escalate: after one declared reset in a session, it drops to non-resumable mode and treats every subsequent disconnect as a fresh session, so the agent's state machine is forced to reconcile rather than paper over the gap. ## Operational Effect The agent-level consequence of missing this check is **silent duplicate side effects and phantom waits**. Because the transport layer reports "connected, receiving events," the agent's execution loop has no signal that its continuity assumption is broken. It will: - Block indefinitely (until its own timeout) on a response that was emitted in the lost gap, then retry — and if the original tool call was non-idempotent, the retry duplicates it. - Treat a `tools/list_changed` notification from a *new* server generation as an incremental update to the *old* tool set, producing a schema mismatch when it next calls a tool whose signature changed across the deploy that caused the log reset. - Advance its own checkpoint cursor to the low ID, so that a subsequent legitimate resume from a *later* disconnect will now be anchored to the wrong generation and the error compounds. With the guardrail in place, the declared-reset branch converts an invisible semantic hole into an explicit state-machine event: the agent re-initializes, re-derives its pending-request set from its own durable checkpoint (not from the stream), and re-issues only the requests whose responses fall in the gap — with idempotency keys carried from the checkpoint so the server can dedupe. The cost is a full re-init round trip on every reset; the benefit is that the agent never executes against a context it merely *believes* is continuous. The invariant, stated once: **a resumed stream is valid only if its first event ID is strictly greater than the cursor sent, under the same session identity, on a transport that advertised resumability.** Any violation is a session reset wearing a transport reset's clothes, and the client must treat it as such before a single event reaches the reasoning loop. ```python class StreamResumeViolation(RuntimeError): """Raised when a resumed MCP SSE stream cannot honor the client's cursor.""" pass def validate_resume_first_event( cursor_sent: str, first_event_id: str, session_sent: str, session_echoed: str, resumable_advertised: bool, id_is_numeric: bool, ) -> None: """ Called by the transport reader on the FIRST event after a reconnect, before dispatch to the JSON-RPC demultiplexer. Raises StreamResumeViolation on any condition that means the server silently downgraded a resumable session to a fresh one. """ if not resumable_advertised: # Never had a resumable session; caller must re-initialize, not resume. raise StreamResumeViolation( "resume attempted on non-resumable transport; re-initialize required" ) if session_echoed != session_sent: raise StreamResumeViolation( f"session identity changed across resume: sent={session_sent!r} " f"echoed={session_echoed!r}; treat as fresh session, replay pending calls" ) if not id_is_numeric: # Opaque IDs: monotonicity cannot be checked numerically. Require a # server-provided generation token instead; absence is itself a violation. raise StreamResumeViolation( "opaque event IDs with no generation token; cannot prove continuity" ) try: cursor_val = int(cursor_sent) first_val = int(first_event_id) except (TypeError, ValueError) as exc: raise StreamResumeViolation( f"unparseable cursor/event id: cursor={cursor_sent!r} " f"first_event={first_event_id!r}" ) from exc if first_val <= cursor_val: raise StreamResumeViolation( f"event log reset detected: cursor={cursor_val} but first resumed " f"event id={first_val}; server accepted Last-Event-ID without honoring it" ) # Strictly greater, same session, resumable: safe to dispatch. ```