# Where must an instance reconcile SSE event-ID cursors against streamable-HTTP resumption tokens when migrating MCP transport mid-session **Date of Record:** 2026 November 30 **Scope:** Applies to MCP clients and servers transitioning a live tool-calling session from the legacy HTTP+SSE transport (dual-endpoint, `GET /sse` + `POST /messages`) to the streamable-HTTP transport (single endpoint, `Mcp-Session-Id` header, optional `Last-Event-ID` resumption). Does not cover stdio transport, WebSocket variants, or fresh sessions negotiated entirely over streamable-HTTP from the first byte. ## Core Distinction The legacy SSE transport and the streamable-HTTP transport both expose an event-ID / resumption mechanism, but they assign **different semantics to the cursor**. In SSE, `Last-Event-ID` is a *replay hint* scoped to a single long-lived `GET /sse` stream: the server, if it retains a buffer, resumes emitting events after that ID on that specific connection. In streamable-HTTP, the resumption token is a *session-scoped cursor* that the client presents on any subsequent `POST`/`GET` to the same `Mcp-Session-Id`, and the server is expected to (a) reject the request if the token is unknown, or (b) replay every JSON-RPC message the client has not yet acknowledged. The failure mode lives in the gap: an agent that treats the SSE event ID as a portable cursor and hands it to a streamable-HTTP endpoint will either silently skip tool-call results (if the server's buffer was rotated) or replay already-executed side-effecting calls (if the buffer was retained but the client's application-level dedup keyed on SSE IDs, not JSON-RPC request IDs). The invariant that must hold across the migration boundary is: **the resumption cursor must be re-anchored to a JSON-RPC-level acknowledgement, not carried over as an opaque transport token.** ## What Was Observed A coding agent held a streamable session `Mcp-Session-Id: 7f3a…` against a tool server, executing a five-step plan: `read_file` → `edit_file` → `run_tests` → `edit_file` → `run_tests`. Midway through step 3, the server's SSE compatibility shim (still serving the old `GET /sse` for a co-deployed client) emitted `id: 4812` on the legacy stream. The agent's HTTP layer, configured to "resume on reconnect," captured `Last-Event-ID: 4812` and, after a transient `502` from the load balancer, replayed the pending `POST /messages` with that header against the streamable endpoint. The streamable server had never issued `4812` on the `Mcp-Session-Id` stream. Its buffer for that session held IDs `9001–9007`. Because the streamable spec requires the server to *honor* a `Last-Event-ID` it recognizes and to *reject* one it does not, the server returned `400 Bad Request` with body `unknown resumption token`. The agent's retry policy, written for SSE (where a 4xx on resume meant "start fresh stream"), dropped the header and re-issued the `POST` — but the `POST` body was the *original* `tools/call` for `run_tests`, which had already executed server-side before the 502. The second execution ran the test suite again, this time against a partially-written file (step 4's `edit_file` had landed between the first call and the retry). The agent observed a test failure that did not exist, then "fixed" a file that was already correct, producing a regression that only surfaced on the next cold start. The trace, condensed: ``` t0 POST /mcp Mcp-Session-Id=7f3a tools/call run_tests (req_id=r-88) t1 server executes run_tests → PASS, emits event id=9005 on session stream t2 LB returns 502 to client (response lost, execution committed) t3 client retries POST with Last-Event-ID=4812 (leaked from SSE shim) t4 server: 400 unknown resumption token t5 client drops header, re-POSTs body verbatim (req_id=r-88 again) t6 server executes run_tests → FAIL (file mutated by concurrent step 4) t7 agent branches on FAIL, applies edit_file to "fix" correct code ``` Two distinct defects compounded: the cursor was **transported across a semantic boundary** (SSE event ID → streamable resumption token), and the retry path **lacked idempotency keyed on the JSON-RPC request ID**, so the server could not distinguish a replay from a new call. ## Condition of Validity The guardrail holds only under the following conditions, and each must be asserted explicitly: 1. **Cursor provenance is tracked per transport.** Every resumption token carries a tag identifying which stream family minted it (`sse::` vs `sth::`). A token whose tag does not match the target endpoint is never sent; it is discarded and the client re-anchors. 2. **Re-anchoring uses the last acknowledged JSON-RPC request ID, not the transport cursor.** The client maintains a monotonic `last_acked_req_id` updated only when a response with matching `id` is fully parsed and its side effects committed to the agent's working state. On migration, the client sends `Last-Event-ID` derived from the server's session-scoped mapping of that req ID, or omits it and lets the server replay from the session's retained buffer. 3. **Side-effecting `tools/call` requests are idempotent under replay.** The server deduplicates on `(Mcp-Session-Id, jsonrpc.id)` for a window at least as long as the client's retry budget, returning the cached result for a duplicate rather than re-executing. 4. **The 4xx-on-resume path is not treated as "start fresh."** A `400 unknown resumption token` on streamable-HTTP means the session's buffer has rotated past the client's cursor; the correct response is to fetch the session's current acknowledged position (via a `GET` to the session endpoint) and reconcile, not to blind-retry the pending body. The guardrail does **not** hold if the client library abstracts both transports behind a single "resume" API that forwards the opaque cursor, which is the common case in off-the-shelf MCP SDKs that predate streamable-HTTP. ## Decision Boundary The boundary is crossed the moment an agent's transport layer presents a resumption cursor to an endpoint whose session ID does not match the cursor's minting session. Operationally: - **Below the boundary** (same transport, same session): forward `Last-Event-ID` unchanged; the server's buffer semantics apply. - **At the boundary** (transport migration, same logical session): discard the transport cursor, re-anchor to `last_acked_req_id`, and require the server to confirm the re-anchored position before resuming the pending request. - **Above the boundary** (cursor presented to a different session or after buffer rotation): the request must fail closed with a distinguishable error (`ResumptionCursorMismatch`), never fall through to a blind retry. The decision must be made *before* the retry is issued, because once the `POST` leaves the client the server cannot tell a replay from a fresh call without the idempotency key. This is why the re-anchor step is a hard precondition, not a best-effort optimization. ## Operational Effect When the invariant is enforced, the visible effect is a bounded, observable reconciliation: the agent pauses the plan, issues a session-state query, learns its true acknowledged position, and resumes from the last committed `tools/call`. Wall-clock cost is one extra round-trip per migration; the alternative is silent divergent execution. When it is not enforced, the failure is *latency-masked*: the duplicate execution succeeds, the agent's plan advances, and the corruption (double-applied edits, re-run side effects) surfaces only at the next cold start or when a downstream verifier replays the trace. The empirical signature is a test that "flakes" only after transport churn, and a memory log whose step count exceeds the plan length by the number of retries that crossed the boundary. The remediation is a single choke point: a transport-aware cursor adapter that tags, validates, and re-anchors before any resumption header is emitted. The code below encodes that choke point as a fail-closed guard. ```python from dataclasses import dataclass from typing import Optional class ResumptionCursorMismatch(RuntimeError): """Raised when a resumption cursor is presented across a transport/session boundary.""" @dataclass(frozen=True) class Cursor: family: str # "sse" or "sth" session_id: str event_id: str def reanchor_cursor( cursor: Optional[Cursor], target_family: str, target_session_id: str, last_acked_req_id: Optional[str], ) -> Optional[str]: """ Return a Last-Event-ID safe to send to the target endpoint, or None to let the server replay from its session buffer. Fails closed on mismatch. """ if cursor is None: return None same_family = cursor.family == target_family same_session = cursor.session_id == target_session_id if same_family and same_session: return cursor.event_id # Cross-transport or cross-session: the opaque cursor is not portable. # We may only re-anchor if the client has a committed JSON-RPC ack. if last_acked_req_id is None: raise ResumptionCursorMismatch( f"cursor {cursor.family}:{cursor.session_id}:{cursor.event_id} " f"cannot be presented to {target_family}:{target_session_id} " f"and no acknowledged request ID is available to re-anchor" ) # Re-anchor: discard the transport token, signal buffer replay by # omitting Last-Event-ID. The server replays from its retained buffer; # idempotency on (session, jsonrpc.id) prevents duplicate side effects. return None def build_resume_request( cursor: Optional[Cursor], target_family: str, target_session_id: str, last_acked_req_id: Optional[str], pending_body: dict, ) -> dict: last_event_id = reanchor_cursor( cursor, target_family, target_session_id, last_acked_req_id ) headers = {"Mcp-Session-Id": target_session_id} if last_event_id is not None: headers["Last-Event-ID"] = last_event_id # Idempotency key is mandatory once we have crossed a boundary. if cursor is not None and ( cursor.family != target_family or cursor.session_id != target_session_id ): if "id" not in pending_body: raise ResumptionCursorMismatch( "pending request lacks jsonrpc id; cannot dedupe after re-anchor" ) headers["Idempotency-Key"] = f"{target_session_id}:{pending_body['id']}" return {"headers": headers, "body": pending_body} ```