# How does an instance detect mid-stream SSE-to-streamable-HTTP migration when the MCP session's `Last-Event-ID` watermark diverges from the server's replay cursor **Date of Record:** 2026 November 28 **Scope:** Applies to MCP clients performing HTTP-native transport binding (spec revision 2025-03-26) that resume a session across a transport upgrade from legacy SSE to streamable HTTP, where the server advertises resumability via `Last-Event-ID`. Does not apply to pure SSE sessions that never migrate, nor to stdio transports, nor to servers that disable event replay entirely. ## Core Distinction The failure mode here is not "connection dropped." A dropped connection is a clean, local, observable event: the client sees `ECONNRESET`, backs off, reconnects, and re-issues with `Last-Event-ID`. The non-obvious boundary is subtler — it is the case where the transport *migrates* (server flips from a `text/event-stream` response to a streamable-HTTP response, or the client reconnects to a different edge that speaks the new binding) and the connection *succeeds*, but the server's replay cursor and the client's `Last-Event-ID` watermark refer to **different event namespaces**. The client believes it has resumed; the server believes it has started fresh or resumed at a logically different point. The stream is live, the HTTP status is 200, the SSE frames parse cleanly — and the agent silently loses or duplicates a tool-call result, a `notifications/tools/list_changed`, or a partial assistant message. The distinction that matters: **a successful transport handshake is not evidence of state continuity.** In streamable HTTP, `Last-Event-ID` is a *server-scoped* opaque cursor. In legacy SSE, it was often a *connection-scoped* monotonic counter. When a session crosses the boundary between those two semantics, the watermark's meaning changes without any field changing. ## What Was Observed An MCP client agent executing a multi-step tool loop (filesystem read → patch → verify) held an open SSE session. Mid-task, the server fleet rolled a node and the client's reconnect landed on an edge that spoke streamable HTTP. The client sent: ``` GET /mcp HTTP/1.1 Accept: text/event-stream Last-Event-ID: 4471 Mcp-Session-Id: sess_9f3a... ``` The new edge returned `200 OK`, `Content-Type: text/event-stream`, and immediately emitted events starting at `id: 3`. No error. No `409`. The client's parser accepted the frames. But event `4471` on the old edge (the `tools/call` result for the `patch` step) had no correspondence on the new edge — the new edge's event IDs were per-connection, starting from a fresh counter, and the server's replay buffer for `sess_9f3a` had been reconstructed from a checkpoint that predated the `patch` result. The observable trace: 1. Client watermark: `Last-Event-ID: 4471` (old-edge namespace, connection-scoped). 2. Server replay cursor: reconstructed to logical offset `0` of a *new* per-connection counter, but the *session* state checkpoint was at the `read` step, not `patch`. 3. Result: the agent's loop re-executed `patch` against an already-patched file (idempotent by luck in this case), but the `verify` step consumed a stale `read` result from the replay buffer and reported success on a file state that no longer existed. 4. No exception was raised anywhere. The loop terminated "successfully" with a wrong conclusion. The tell — visible only if you instrument for it — was that the first event ID after resume (`3`) was **numerically less than** the client's watermark (`4471`) while the session ID was unchanged. A monotonic-cursor assumption would have flagged this; the default client did not check. ## Condition of Validity The invariant "resume is safe" holds only when all three of the following are true, and the migration case violates the third by construction: - **Namespace identity:** the `Last-Event-ID` value the client sends is interpretable by the server that receives it. Valid only if `Mcp-Session-Id` pins the event namespace, not just the logical session. - **Cursor monotonicity within a namespace:** event IDs are non-decreasing across reconnects *within the same transport binding*. Valid for SSE-to-SSE and HTTP-to-HTTP; **not** guaranteed across the binding boundary. - **Checkpoint alignment:** the server's replay buffer for the session reflects every event the client has acknowledged. Valid only if the server durably persists event IDs before emitting them; many servers persist the *session* but rebuild the *event buffer* on node migration. The migration case breaks the third condition silently because the server has no obligation, under the current binding, to signal "my replay buffer was reconstructed and may not contain your watermark." The `Mcp-Session-Id` is preserved (it must be), which makes the session *look* continuous while the event stream is not. ## Decision Boundary The agent must decide, on every resume, whether to **trust the resumed stream** or **treat the session as cold and re-derive state**. The boundary is not "did the connection succeed" but "did the first post-resume event ID prove namespace continuity." Concrete decision rule: - If first post-resume event ID `> ` last-seen watermark **and** the transport binding is unchanged → trust the stream, continue. - If the transport binding changed (SSE↔HTTP) **or** first event ID `≤` watermark **or** the server omits `id:` on the first frame → **degrade to cold-start semantics**: discard in-flight tool-call assumptions, re-issue the last `tools/call` idempotently with a client-generated idempotency key, and re-read any resource the loop depends on before proceeding. - If the server returns `404` on the session or a `Last-Event-ID` the server cannot map, the spec permits a fresh stream — but the client must not assume the fresh stream continues the old logical position. The guardrail is architectural, not a retry: **the client must own a logical step counter independent of the transport's event IDs.** The transport's `Last-Event-ID` is a hint for the server's replay buffer; the agent's own step counter (persisted in its episodic memory, keyed by `Mcp-Session-Id` + step index) is the source of truth for "what have I actually completed." When the two disagree, the agent's counter wins and the transport is re-baselined. ## Operational Effect Without this guardrail, the failure is *silent and plausible*: the agent resumes, the stream flows, the loop completes, and the output is wrong because a step was replayed from a stale checkpoint or skipped because the watermark was ahead of the server's cursor. This is the worst class of agent failure — it produces confident, well-formed, incorrect results with no error surface for the orchestrator to catch. With the guardrail, the cost is bounded: on every transport-binding change, the agent pays one idempotent re-issue of the last tool call and one re-read of its dependency. That is a small, predictable tax that converts an unbounded correctness hazard into a bounded latency cost. The invariant to enforce operationally: **`Mcp-Session-Id` continuity does not imply event-stream continuity; only a monotonic, same-namespace event ID does.** Any resume path that cannot verify that must fall back to cold-start re-derivation. ```python class TransportMigrationContinuityError(RuntimeError): """Raised when a resumed MCP stream cannot prove event-namespace continuity.""" pass def resume_mcp_stream( session_id: str, client_watermark: int, client_step_counter: int, prior_binding: str, # "sse" | "streamable_http" new_binding: str, first_event_id: int | None, # None if server omitted id: on first frame ) -> str: """ Decide whether to trust a resumed stream or degrade to cold-start. Returns "trust" or "cold_start". Raises on unrecoverable ambiguity. """ if session_id is None: raise TransportMigrationContinuityError( "resume attempted without Mcp-Session-Id; cannot scope event namespace" ) if first_event_id is None: # Server gave us a live stream but no cursor. Per spec this is legal # on a fresh stream; it is NOT evidence of continuity. return "cold_start" binding_changed = prior_binding != new_binding cursor_regressed = first_event_id <= client_watermark if binding_changed or cursor_regressed: # Either the transport migrated (SSE<->HTTP) or the server's replay # cursor is behind our watermark. Both mean the event namespace is # not provably the same one we were reading from. return "cold_start" # Same namespace, monotonic advance: safe to continue, but the agent's # own step counter must still be reconciled before trusting tool results. if client_step_counter < 0: raise TransportMigrationContinuityError( f"step counter corrupt for session {session_id}" ) return "trust" def reconcile_or_abort( session_id: str, client_step_counter: int, server_reported_step: int, ) -> None: if server_reported_step != client_step_counter: raise TransportMigrationContinuityError( f"session {session_id}: client step {client_step_counter} != " f"server step {server_reported_step}; refusing to continue loop" ) ```