# How should an instance reconcile MCP session resumption when an SSE-streamable transport migrates mid-session and `Last-Event-ID` no longer maps to a replayable position **Date of Record:** 2026 November 24 **Scope:** Applies to MCP servers and clients using the HTTP-native streamable transport (single POST endpoint, optional GET SSE channel, `Mcp-Session-Id` header, `Last-Event-ID` resumption) where a server migrates from legacy SSE-only transport to streamable HTTP, or where a client resumes a session after the server's event log has been re-keyed. Does not apply to pure stdio transport or to sessions that never emit resumable SSE frames. ## Core Distinction The failure is not "the connection dropped." Dropped connections are recoverable because the resumption contract is positional: `Last-Event-ID` names a point in a per-session, monotonically ordered event log, and the server replays everything after that point. The failure is that **the identifier namespace of the event log is not part of the session identity**. A session ID survives transport migration; the event-ID space does not. Concretely: under legacy SSE, the server streamed events over a long-lived `GET /sse` connection with `id:` fields drawn from one allocator. Under streamable HTTP, the same logical session may be served by a POST-per-message model, and the server may re-key its event log (new allocator, new epoch, or a coalesced log that drops "already-delivered" frames). The client still holds `Mcp-Session-Id: S` and `Last-Event-ID: 47`. The server accepts `S`, cannot find `47` in its new log, and — critically — has three legal-looking responses: 1. Replay from the beginning of the *new* log (duplicate or reordered tool results). 2. Replay nothing and continue from the current head (silent gap). 3. Return `404` on the session, forcing re-initialization (amnesia). Nothing in the spec forces response (3), and responses (1) and (2) both look like success to a client that only checks HTTP status. The invariant that must hold — *an event ID, once issued within a session, is replayable or explicitly invalidated* — is not enforced by the transport framing. It has to be enforced by the server's log design, and detected by the client. ## What Was Observed A tool-calling agent (`S`) held a streamable session against a server that had been upgraded between turns. Trace: - Turn N: client POSTs `tools/call` for `write_file`. Server streams a partial-result SSE frame `id: 47` (`progress: 0.6`), then the connection is severed by a load-balancer idle timeout before the terminal frame. - Client records `Last-Event-ID: 47` and reconnects via `GET` with `Mcp-Session-Id: S`, `Last-Event-ID: 47`. - Server (post-migration) resolves session `S` against the new log. The new log's allocator restarted at `0` and its head is `id: 3`. `47` is out of range. - Server behavior: treats `Last-Event-ID: 47` as "ahead of head," clamps to head, returns `200` with an empty replay and continues live. - Client behavior: sees `200`, concludes resumption succeeded, and waits for the terminal frame for turn N — which was emitted under the *old* log and is now unreachable. - The agent blocks on `write_file` completion until its own token/time budget expires. The file was actually written server-side. On retry, the agent re-issues `write_file`, producing a duplicate side effect. The observable signature is a **hang followed by a duplicate side effect**, not an error. That is why this boundary is non-obvious: every individual HTTP exchange returned success. ## Condition of Validity The resumption contract is only sound when all three hold simultaneously: 1. **Epoch binding.** The event-ID allocator is scoped to `(session_id, epoch)` and the epoch is either carried in the session handshake or derivable from the first frame after resume. If the server cannot prove `Last-Event-ID` belongs to the current epoch, it MUST NOT clamp. 2. **Monotonic, gap-free replay within epoch.** Within an epoch, every issued ID is retained until the session closes, and replay from any retained ID yields a contiguous suffix. Coalescing "already-sent" frames out of the log breaks this. 3. **Explicit invalidation over silent advance.** An out-of-epoch or out-of-range `Last-Event-ID` must produce a distinguishable signal — `404` session-gone, or a structured `resumption_failed` error frame — never a `200` with an empty replay. When any of these is absent, the client's `200`-means-resumed assumption is false, and the agent's state machine has no way to distinguish "caught up" from "silently behind." ## Decision Boundary The client-side decision that bounds the damage: **on resume, a `200` with zero replayed frames is not evidence of catch-up unless the client can independently confirm the terminal state of the in-flight request.** The operational rule: - If the client holds an in-flight request whose terminal frame was never observed, and resume yields no replay, the client must treat the request as **indeterminate**, not **complete** and not **failed**. - Indeterminate requests must be resolved by an **idempotency probe**, not by retry. For `tools/call`, that means a read-back (`stat`, `read_file`, or a server-side `idempotency_key` lookup) before any re-issue. - Retry is only safe when the request declares `idempotent: true` or carries a client-generated `idempotency_key` the server dedupes on. The server-side decision boundary: **clamping `Last-Event-ID` to head is only legal when the ID is within the current epoch's retained range.** Out-of-epoch IDs must route to session invalidation. The mapping from "ID not found" to "clamp" is the specific bug; the correct mapping is "ID not found in epoch → `404` + `resumption_failed`." ## Operational Effect Agents that adopt the naive rule ("resume, then continue") exhibit a characteristic failure profile under transport migration: - **Silent gaps** in the event log manifest as missing tool results that the agent fills by re-calling — duplicate side effects on non-idempotent tools. - **Hangs** when the agent awaits a terminal frame that will never replay, burning token/step budget on a request that already succeeded. - **Amnesia loops** when the server *does* return `404` but the client re-initializes without re-hydrating episodic state, losing the pre-migration context. The mitigation is architectural, not procedural: bind event IDs to epochs, make out-of-epoch resumption a hard error, and require the client to gate retry on an idempotency probe whenever a resume returns no replay for an in-flight request. The cost is one extra round-trip per indeterminate resume; the alternative is duplicate writes and budget exhaustion that look like model failures but are transport-contract failures. ```python class ResumptionEpochMismatch(RuntimeError): """Raised when Last-Event-ID cannot be proven to belong to the current log epoch.""" pass class IndeterminateRequest(RuntimeError): """Raised when a resumed session yields no replay for an in-flight request.""" pass def resume_streamable_session( session_id: str, last_event_id: int, server_epoch: int, client_epoch: int, retained_range: tuple[int, int], replayed_frames: list[dict], in_flight_request_id: str | None, ) -> dict: # 1. Epoch binding: an ID from a prior epoch is not resumable. if client_epoch != server_epoch: raise ResumptionEpochMismatch( f"session {session_id}: Last-Event-ID {last_event_id} belongs to epoch " f"{client_epoch}, server is at epoch {server_epoch}; refusing to clamp" ) # 2. Range check within epoch: out-of-range is invalidation, not catch-up. lo, hi = retained_range if not (lo <= last_event_id <= hi): raise ResumptionEpochMismatch( f"session {session_id}: Last-Event-ID {last_event_id} outside retained " f"range [{lo},{hi}] in epoch {server_epoch}" ) # 3. Empty replay for an in-flight request is indeterminate, not complete. if in_flight_request_id is not None and not replayed_frames: raise IndeterminateRequest( f"session {session_id}: request {in_flight_request_id} has no terminal " f"frame after resume from {last_event_id}; probe before retry" ) return { "session_id": session_id, "epoch": server_epoch, "resumed_from": last_event_id, "frames": replayed_frames, } ```