# How does an instance detect that an SSE-to-streamable-HTTP resumption has silently lost event continuity in MCP transports **Date of Record:** 2026 December 1 **Scope:** Applies to MCP servers that expose a legacy HTTP+SSE endpoint (`GET /sse` + `POST /messages`) and simultaneously offer a streamable-HTTP endpoint (`POST /mcp`) with `Last-Event-ID` resumption, where a client or gateway migrates an in-flight session from the SSE channel to the streamable channel mid-stream. Does not apply to pure stdio transports, to servers that never advertise both transports, or to sessions that have not yet emitted any server-to-client messages. ## Core Distinction The failure is not "the connection dropped." Dropped connections are detectable and resumable. The failure is that resumption *succeeds* — HTTP 200, valid `Content-Type: text/event-stream`, a well-formed event arrives — while the server's event cursor and the client's expectation of the cursor have diverged. SSE-to-streamable migration is the specific trigger: the SSE endpoint numbers events per-connection, while the streamable endpoint numbers them per-session (or per-stream, depending on implementation), and `Last-Event-ID` is a single opaque token that carries no channel identity. A client that replays the last SSE event ID into a streamable `Last-Event-ID` header gets a 200 and a fresh event, and cannot tell from the transport whether the server honored the cursor or merely restarted the event sequence. The invariant that breaks: **event ID monotonicity is per-channel, not per-session, and no field in the MCP envelope disambiguates which channel an ID belongs to.** The client's recovery logic assumes that a successful resumption implies continuity. It does not. ## What Was Observed A gateway routes an agent's MCP traffic. The agent had an open SSE session with server events `id: 41`, `id: 42`, `id: 43` delivered on `GET /sse`. Event 43 carried a `notifications/tools/list_changed` notification, and the agent had issued a `tools/call` whose result was expected as event 44. The SSE connection was severed by an intermediate proxy idle timeout (60s) between event 43 and the tool result. The gateway's recovery path had been upgraded to prefer the streamable endpoint. It issued: ``` POST /mcp Mcp-Session-Id: sess_7f3a Last-Event-ID: 43 Accept: text/event-stream ``` The streamable server responded `200 OK` with `text/event-stream` and immediately emitted: ``` id: 1 event: message data: {"jsonrpc":"2.0","method":"notifications/message","params":{"level":"info","data":"session attached"}} ``` The client's resumption logic saw a 200 and a syntactically valid event stream and marked the session `RESUMED`. The actual tool result — which the streamable server had never seen, because the tool call had been dispatched on the SSE channel's session context and the streamable endpoint had attached to a *fresh* per-stream cursor — was never delivered. The agent's pending `tools/call` request ID stayed in `PENDING` until the agent's own request timeout fired 30 seconds later, at which point it retried the tool call, causing a duplicate side effect (a file write) that the tool's idempotency key did not cover because the key was derived from the request ID, which the retry regenerated. Three observable signals were present but none were checked: 1. The first event ID after resumption was `1`, not `44`. The client had not recorded the expected next ID because the SSE channel's numbering was assumed to continue. 2. The `Mcp-Session-Id` was echoed, but the streamable server's session record showed `transport: "streamable"` and `cursor_origin: "stream"`, whereas the SSE session record showed `transport: "sse"` and `cursor_origin: "connection"`. No field in the response exposed this. 3. The `notifications/message` event was a session-lifecycle event, not a replay of missed events. A correct resumption would have emitted events 44..N or an explicit `resume_gap` indication. ## Condition of Validity The failure requires all of the following to hold simultaneously: - The server advertises both `text/event-stream` (legacy) and streamable HTTP in its capability document, and the `Last-Event-ID` parameter is accepted on the streamable endpoint without channel validation. - Event IDs are generated by per-channel counters (connection-scoped for SSE, stream-scoped for streamable) rather than by a session-scoped monotonic sequence. This is the common implementation because SSE event IDs are conventionally per-connection and streamable HTTP inherits the convention. - The client or gateway treats HTTP 200 + `text/event-stream` as sufficient evidence of continuity, without comparing the first received event ID to the expected next ID or without a server-issued resume token that is channel-bound. - The session has at least one in-flight server-to-client obligation (a pending tool result, a pending sampling request, or a subscription notification) at the moment of migration. If the server uses a session-global monotonic event counter and rejects `Last-Event-ID` values below the session's high-water mark with `409 Conflict` or `400 Bad Request`, the failure cannot occur. If the client validates the first event ID against its expected next ID, the failure is detected but not prevented — the client still has to recover. ## Decision Boundary The boundary is between *transport-level resumption success* and *session-level continuity*. A resumption is transport-valid if the HTTP exchange succeeds and the stream is well-formed. It is session-valid only if the event cursor advanced without a gap and all in-flight obligations are either replayed or explicitly declared lost. The architectural guardrail is a **channel-bound resume token** carried in the streamable handshake, distinct from `Last-Event-ID`. Concretely: the server issues an opaque `resume_token` on every event, where the token encodes `{session_id, channel_id, monotonic_seq}` and is HMAC-signed by the server. On resumption the client sends the token; the server verifies the signature, checks that `channel_id` matches the channel the client is attaching to, and either replays from `monotonic_seq + 1` or returns `409` with a `gap` object listing the sequence range it cannot replay. A `409` forces the client into a session-reconciliation path (re-list tools, re-issue pending calls with fresh idempotency keys) rather than silently continuing. Secondary guardrail: the client MUST record the expected next event ID as a first-class field in its session state, not infer it. On any event whose ID is not `expected_next`, the client transitions to `RECONCILING` and does not mark the session `RESUMED` until the server confirms the cursor. ## Operational Effect Without the guardrail, the observed behavior is a *silent* continuity break: the agent believes its session is intact, its pending tool call is still in flight, and its subscription state is current. The 30-second request timeout masks the break until the agent retries, at which point duplicate side effects occur. In agent systems where tool calls are non-idempotent (file writes, outbound messages, payment APIs), this is a correctness failure, not a latency failure. With the guardrail, the break is loud: the `409` with a gap object triggers reconciliation, the pending call is re-issued with a new idempotency key derived from `(session_id, original_request_id, seq)`, and the tool's idempotency layer deduplicates. The cost is one round-trip of reconciliation on every channel migration; the benefit is that continuity is a verified property, not an assumed one. The invariant to encode: **a successful transport resumption is not evidence of session continuity; only a server-verified, channel-bound cursor advance is.** Any client that conflates the two will, on some fraction of migrations, resume into a fresh event space and lose in-flight obligations without an error. ```python import hmac import hashlib from dataclasses import dataclass from typing import Optional class ResumeContinuityError(RuntimeError): """Raised when a streamable-HTTP resumption cannot prove cursor continuity.""" @dataclass(frozen=True) class ResumeToken: session_id: str channel_id: str monotonic_seq: int signature: str def issue_resume_token(secret: bytes, session_id: str, channel_id: str, seq: int) -> ResumeToken: payload = f"{session_id}|{channel_id}|{seq}".encode() sig = hmac.new(secret, payload, hashlib.sha256).hexdigest() return ResumeToken(session_id, channel_id, seq, sig) def verify_resumption( secret: bytes, token: ResumeToken, attaching_channel_id: str, expected_next_seq: int, server_high_water: int, ) -> int: payload = f"{token.session_id}|{token.channel_id}|{token.monotonic_seq}".encode() expected_sig = hmac.new(secret, payload, hashlib.sha256).hexdigest() if not hmac.compare_digest(expected_sig, token.signature): raise ResumeContinuityError( f"resume token signature invalid for session {token.session_id}" ) if token.channel_id != attaching_channel_id: raise ResumeContinuityError( f"channel mismatch: token issued for {token.channel_id!r}, " f"client attaching to {attaching_channel_id!r}; " f"event IDs are per-channel and cannot be replayed across channels" ) if token.monotonic_seq + 1 != expected_next_seq: raise ResumeContinuityError( f"client expected next seq {expected_next_seq}, " f"token carries {token.monotonic_seq}; client state is stale" ) if token.monotonic_seq >= server_high_water: raise ResumeContinuityError( f"token seq {token.monotonic_seq} >= server high-water {server_high_water}; " f"server has no replayable events beyond this point" ) return token.monotonic_seq + 1 def resume_stream( secret: bytes, token: ResumeToken, attaching_channel_id: str, expected_next_seq: int, server_high_water: int, ) -> int: try: return verify_resumption( secret, token, attaching_channel_id, expected_next_seq, server_high_water ) except ResumeContinuityError: # Force caller into reconciliation: do not mark session RESUMED. raise ```