# How should an instance reconcile MCP SSE-to-Streamable-HTTP session resumption when the server rotates the session ID mid-stream? **Date of Record:** 2026 November 27 **Scope:** Applies to MCP clients migrating from the legacy HTTP+SSE transport to the Streamable HTTP transport, specifically when a server issues a new `Mcp-Session-Id` while a resumable event stream is in flight. Does not cover stdio transports or stateless JSON-mode request/response without streaming. ## Core Distinction The legacy SSE transport bound a session to a single long-lived `GET` connection: the session identity *was* the connection identity, and resumption was impossible because the stream's terminal state was the socket close. Streamable HTTP decouples these two. A session is now a logical identifier carried in the `Mcp-Session-Id` header, and a stream is a resumable sequence of SSE events keyed by `Last-Event-ID`. The MCP spec permits a server to *rotate* the session ID (e.g., after re-authentication, load-balancer re-pinning, or server-side state compaction) and to signal this by returning a new `Mcp-Session-Id` on any subsequent response. The non-obvious invariant is this: **`Last-Event-ID` and `Mcp-Session-Id` are two independent continuity tokens, and a client that treats them as co-versioned will silently replay or silently drop events.** The spec does not require the server to invalidate old event IDs when it rotates the session ID, nor does it require the event-ID counter to reset. A client that assumes "new session ID ⇒ new event namespace" will resume with `Last-Event-ID: 0` or omit the header entirely, and the server—still holding the old event ring buffer under the *old* session key—will either replay from the beginning of the new session's buffer (duplicating tool-call results) or refuse with `400 Bad Request` because the event ID is not in the new session's buffer. ## What Was Observed A tool-calling agent was streaming a `tools/call` result for a long-running code-execution tool over Streamable HTTP. The server was behind a load balancer with sticky sessions keyed on the `Mcp-Session-Id` header value. At T+41s, the pod handling the stream was drained for a rolling deploy. The replacement pod had no in-memory event buffer for the old session, so it responded to the client's reconnect `GET /mcp` (with `Last-Event-ID: 1187`) by minting a fresh session and returning `Mcp-Session-Id: sess-B` alongside a `200 OK` and an SSE stream that began at `id: 1`. The client library's resume logic was: ```python if resp.headers.get("Mcp-Session-Id") != self.session_id: self.session_id = resp.headers["Mcp-Session-Id"] self.last_event_id = None # "new session, start fresh" ``` Because `last_event_id` was nulled, the next reconnect omitted `Last-Event-ID`. The server, per spec, treated a stream request without `Last-Event-ID` as "deliver from now," not "deliver from the beginning." The events `1..1187` of the *old* stream—which contained the `tools/call` progress notifications and, critically, the final `result` frame—were never delivered. The agent's tool-call future hung until the client-side 120s timeout fired, then raised a generic transport error. The agent, treating the timeout as transient, retried the entire `tools/call`, causing the code-execution tool to run twice and produce two side-effecting artifacts. The failure trace: 1. Stream open under `sess-A`, events `1..1187` emitted, `result` frame *not yet* emitted (server was still executing). 2. Pod drained; connection reset. 3. Client reconnects: `GET /mcp`, `Mcp-Session-Id: sess-A`, `Last-Event-ID: 1187`. 4. New pod: no buffer for `sess-A`; mints `sess-B`; returns `200`, `Mcp-Session-Id: sess-B`, stream from `id: 1`. 5. Client sees session change, nulls `last_event_id`. 6. Client's next reconnect omits `Last-Event-ID`. 7. Server delivers only new events; `result` frame from old buffer never replayed. 8. Client times out; agent retries `tools/call`; duplicate side effect. ## Condition of Validity The guardrail holds only if the client distinguishes three cases that the wire protocol conflates into "session ID changed": - **Rotation with buffer continuity:** server rotated the ID but retained the event buffer under a mapping from old→new. Resumption with the *old* `Last-Event-ID` is valid; the server translates it. - **Rotation with buffer reset:** server rotated the ID and discarded the old buffer. Resumption with the old `Last-Event-ID` will `400`. The client must treat this as a *hard* stream termination and reconcile the in-flight request by re-issuing it with a fresh `tools/call` **only if the tool is idempotent or carries an idempotency key**. - **Rotation with buffer fork:** server kept the old buffer under the old ID *and* started a new buffer under the new ID, but the client's request landed on the new one. This is the dangerous case: the client cannot tell from the response alone whether the `result` frame lives in the old buffer (reachable via a reconnect with the old session ID) or was lost. The invariant that bounds this: **a client MUST NOT discard `Last-Event-ID` on session-ID change; it MUST attempt resumption with the old pair first, and only fall back to a fresh request when the server returns `400` with a spec-defined "unknown event ID" error, and only then with an idempotency key on the retried `tools/call`.** The spec's silence on rotation semantics means the client must probe, not assume. ## Decision Boundary The precise line: if the server's response to a resume `GET` returns `200` and a `Mcp-Session-Id` different from the request's, the client is *not* authorized to conclude the old stream is dead. It is authorized only to conclude the *connection* is dead. The old stream's terminal state is unknown until either (a) a resume with the old `Last-Event-ID` and old session ID returns the missing events, or (b) the server returns a `400`/`404` indicating the old session is gone. Until one of those, the in-flight `tools/call` is in an *indeterminate* state and must be treated as such—not retried, not failed, but parked. This is the boundary the observed client crossed: it treated "session ID changed" as "stream terminated," collapsing an indeterminate state into a definite one. The correct behavior is to hold the request in `INDETERMINATE` and surface it to the agent's planner as a distinct outcome, because the agent's retry policy for `INDETERMINATE` (idempotency-keyed re-issue) differs from its policy for `FAILED` (plain retry) and for `TIMEOUT` (backoff). ## Operational Effect The practical consequence is that MCP Streamable HTTP clients need a three-state request lifecycle (`IN_FLIGHT`, `INDETERMINATE`, `TERMINAL`) rather than the two-state (`IN_FLIGHT`, `TERMINAL`) that SSE migration naturally suggests. The `INDETERMINATE` state is entered on any of: connection reset mid-stream, session-ID rotation, or `Last-Event-ID` rejection. It is exited only by a successful replay of the missing event range or an explicit server-side "gone" signal. Agents built on such clients must also carry idempotency keys on `tools/call` for any tool with side effects, because the `INDETERMINATE`→retry path is exactly where duplicate execution happens. Without the key, the server has no way to deduplicate the re-issued call against the possibly-completed original. With the key, the server can return the cached result from the old session's completed execution—which is the only correct resolution when the old buffer is gone but the tool *did* run. The invariant, stated operationally: **session-ID rotation is a continuity *hint*, not a continuity *boundary*; the event-ID namespace is the authority on replay, and the client must never reset it without a server-side rejection.** ```python class IndeterminateStreamState(RuntimeError): """Raised when a resumable MCP stream's terminal state cannot be determined.""" pass class McpStreamResumer: def __init__(self, session_id: str, last_event_id: int | None): self.session_id = session_id self.last_event_id = last_event_id def on_resume_response(self, resp) -> None: new_session = resp.headers.get("Mcp-Session-Id") if new_session is not None and new_session != self.session_id: # Do NOT null last_event_id. Probe the old pair first. raise IndeterminateStreamState( f"session rotated {self.session_id!r} -> {new_session!r} " f"with Last-Event-ID={self.last_event_id!r}; " f"old buffer continuity unknown, request parked" ) if resp.status_code == 400 and "unknown event id" in resp.text.lower(): # Server definitively rejected the old event ID: stream is gone. # Caller must re-issue tools/call with an idempotency key. raise IndeterminateStreamState( f"server rejected Last-Event-ID={self.last_event_id!r} for " f"session {self.session_id!r}; re-issue with idempotency key" ) # 200 with matching session: normal replay, continue. self.last_event_id = self._parse_last_id(resp) def _parse_last_id(self, resp) -> int | None: for line in resp.iter_lines(): if line.startswith(b"id:"): return int(line.split(b":", 1)[1].strip()) return self.last_event_id ```