# What happens when an instance resumes an MCP streamable session after an SSE-to-HTTP fallback and replays a stale `Last-Event-ID` cursor against a re-sharded server **Date of Record:** 2026 December 3 **Scope:** Applies to MCP clients using the HTTP-native "streamable session" transport that may negotiate down from SSE to plain request/response, and that attempt to resume a session by replaying `Last-Event-ID` after a transport fallback. Does not apply to stdio transports or to pure SSE deployments with a single fixed event store. ## Core Distinction There are two different things an MCP client can mean when it says "resume": 1. **Cursor resumption** — the client presents `Last-Event-ID: N` and the server replays every event with id `> N` from a durable, monotonically ordered event log for that session. 2. **Session resumption** — the client presents a session identifier (e.g. `Mcp-Session-Id`) and the server rehydrates whatever state it still holds, possibly with a *fresh* event cursor. These are conflated in practice because most servers implement both simultaneously and because the streamable-session spec allows the server to answer a resume request with either a replayed stream or a newly minted one. The failure mode below lives precisely in the gap: a client that fell back to HTTP request/response (no live stream, no `Last-Event-ID` continuity) later re-attempts SSE, sends the *pre-fallback* `Last-Event-ID`, and the server — which has since re-sharded or garbage-collected that session's event log — treats the cursor as a valid but *unreachable* position. The client is told the session resumed. It has not. The invariant that breaks is: **`Last-Event-ID` is only meaningful relative to the event-log instance that issued it.** It is not a session-wide, server-wide, or cluster-wide monotonic clock. A cursor is a foreign key into one specific log, and the log identity is not carried in the header. ## What Was Observed A tool-calling agent runs a long multi-step task (a code-refactor loop with ~40 sequential `tools/call` invocations) over an MCP server behind a load balancer. Sequence of events: 1. Session `S` opens over SSE. Server instance `A` assigns event ids `1..812` for the first 20 tool calls. Client records `Last-Event-ID: 812` in its local session checkpoint. 2. The SSE connection drops (LB idle timeout, 60s). Client's transport layer, configured to "prefer SSE, fall back to HTTP POST," degrades to plain request/response for tool calls 21–34. **These calls produce no event ids** — the HTTP fallback path returns JSON responses directly, and the server does not append them to the event log because there is no open stream to append to. The client keeps `Last-Event-ID: 812` frozen in its checkpoint, unaware that 14 tool calls have now happened "off-log." 3. At tool call 35 the client's transport re-attempts SSE. The LB has rotated the client to instance `B`. Instance `B` has a *different* event log for a *different* incarnation of session `S` (the session was re-sharded to `B` when `A` was drained). `B`'s log for `S` starts at id `9001` and currently sits at `9034`. 4. Client sends `GET /mcp` with `Last-Event-ID: 812` and `Mcp-Session-Id: S`. Instance `B` looks up session `S`, finds it valid, and — because `812 < 9034` — concludes the cursor is "behind" and replays events `9001..9034`. The client accepts them as the continuation of `812`. 5. The client now holds a stream in which the 14 fallback tool calls (21–34) are **absent**, and the replayed events `9001..9034` describe a *different* interleaving of the same logical session that instance `B` reconstructed from the shared session store. Two or three of those replayed events are `tools/call` results the client already applied during the HTTP fallback window — it applies them again. The observable symptom is not a crash. It is a silent divergence: the agent's working memory now contains a tool-call history that never happened in that order, with duplicated side effects (two `apply_patch` results for the same file, the second computed against a tree the first had already mutated), and a `Last-Event-ID` that has jumped from `812` to `9034` with an unbridgeable gap the client has no way to detect. ## Condition of Validity The failure requires all of the following to hold simultaneously: - **Fallback produces off-log state.** The HTTP request/response fallback path must be capable of advancing session state (executing tool calls, mutating resources) *without* emitting events into the same log that `Last-Event-ID` indexes. If the fallback path either (a) refuses to advance state, or (b) appends synthetic events with ids, the gap cannot form. - **Cursor is not namespaced to the log instance.** `Last-Event-ID` must be a bare integer with no log-generation, epoch, or shard token. If the header carried `812@gen3` and `B`'s log were `gen4`, the mismatch would be detectable. - **The server treats "cursor less than head" as "replay from cursor."** A server that validates the cursor against its *own* log's known id range (and rejects unknown ids) would refuse `812` because `B`'s log never issued an id below `9001`. The bug is a permissive range check (`id < head` ⇒ replay) instead of a membership check. - **Session store is shared but event log is not.** The session metadata (`S` is alive, its tool state) is replicated across instances; the event log is per-instance and re-sharded on drain. This asymmetry is what lets `B` accept the session while having no relationship to the cursor. If any one of these is absent, the failure collapses into a clean error (connection refused, 400 on unknown cursor, or a full rehydrate that discards the stale cursor). It is the *combination* that yields silent divergence. ## Decision Boundary The boundary is between **"cursor is a position in a log I can address"** and **"cursor is a position in a log I cannot address but can approximate."** A correct implementation must never approximate. Concretely: - **Reject-on-membership, not reject-on-range.** On resume, the server must verify the presented `Last-Event-ID` was actually issued by the event-log instance now serving the session. If the session was re-sharded, the cursor from the prior shard is *unaddressable* and must produce an explicit `409 Conflict` (or a spec-defined "resume not possible, re-sync required") — never a best-effort replay. The client must then perform a full session re-sync and *discard* its frozen cursor. - **Fallback must be log-preserving or log-declaring.** Either the HTTP fallback path emits events (so the cursor advances and the gap never opens), or the fallback response must carry an explicit `X-Mcp-Log-Gap: ` (or equivalent) telling the client that `n` state-advancing operations occurred off-log. The client's checkpoint must then record "cursor invalid after this point," not "cursor = 812 forever." - **The client must treat a re-attempted SSE after fallback as a fresh stream unless the server proves continuity.** A resumed stream whose first replayed id is not `Last-Event-ID + 1` is, by definition, discontinuous. The client must not splice it into the existing history; it must either reject it or mark the intervening range as `UNKNOWN` and re-derive affected state. The decision rule for the agent: **if the first event id on a resumed stream is not exactly `last_seen + 1`, the resume is unsound; do not apply replayed events as continuations.** This is the single check that catches the re-shard case, the GC case, and the off-log-fallback case at once. ## Operational Effect Downstream, the agent's episodic memory is corrupted in a way that is *self-consistent* — every individual event is well-formed, every tool result parses, and the `Last-Event-ID` monotonically increases. There is no local signal of the gap. The corruption only surfaces when a later step's precondition fails (a patch that no longer applies, a file that was already modified), at which point the agent's error-compounding loop attributes the failure to the *current* step rather than to the replay splice 20 steps earlier. Recovery from that misattribution typically requires a full task restart, not a local retry. The architectural guardrail is therefore not "handle resume errors" but "make cursor-continuity a hard precondition of applying any replayed event." Treat `Last-Event-ID` as a foreign key with a log-generation component, enforce membership on the server, and on the client refuse any replayed stream whose first id is not contiguous with the last applied id. The gap must be loud. ```python class StreamResumeDiscontinuity(RuntimeError): """Raised when a resumed MCP streamable session replays events that are not contiguous with the client's last applied Last-Event-ID, indicating a re-sharded event log, an off-log HTTP fallback window, or event-log GC.""" def apply_resumed_stream( last_applied_id: int, replayed_events: list[dict], session_id: str, ) -> list[dict]: """Apply a replayed SSE stream only if it is strictly contiguous with the client's checkpoint. Non-contiguous streams are unsound and must not be spliced into agent history.""" if not replayed_events: return [] first_id = replayed_events[0]["id"] if first_id != last_applied_id + 1: # Covers: re-shard (first_id >> last_applied_id), off-log fallback # (first_id == last_applied_id, i.e. duplicate), and GC (first_id # jumped past a hole). All are unsafe to apply as continuation. raise StreamResumeDiscontinuity( f"session {session_id}: resumed stream starts at event {first_id} " f"but checkpoint is {last_applied_id}; gap of " f"{first_id - last_applied_id - 1} unaddressable events. " f"Refusing to splice; require full session re-sync." ) expected = last_applied_id + 1 for ev in replayed_events: if ev["id"] != expected: raise StreamResumeDiscontinuity( f"session {session_id}: non-monotonic replay at event {ev['id']}, " f"expected {expected}" ) expected += 1 return replayed_events ```