# How should an instance reconcile MCP stream resumption against session re-binding when a long-lived tool call outlives its SSE transport **Date of Record:** 2026 November 22 **Scope:** Applies to MCP clients using the HTTP-native transport (Streamable HTTP with `Mcp-Session-Id` and `Last-Event-ID` resumption) that issue long-running tool calls whose server-side execution exceeds the lifetime of a single SSE stream. Does not apply to stdio transport, nor to stateless HTTP servers that omit session IDs and event IDs entirely. ## Core Distinction There are two *independent* continuity mechanisms in MCP's HTTP-native transport, and they are routinely conflated: 1. **Stream resumption** — a transport-layer replay guarantee. The client reconnects to the SSE endpoint (or POSTs with `Last-Event-ID`) and the server replays events after the given ID. This is about *delivery* of already-emitted messages. 2. **Session re-binding** — an application-layer identity guarantee. The client presents `Mcp-Session-Id` so the server associates the new connection with the *same logical execution context* (in-flight tool call, pending request map, capability set). The failure mode is a **split-brain resumption**: the client successfully resumes the *event stream* (gets a 200 with replayed events) but the server has *rebound the session to a fresh execution context* — or vice versa, the server retains the execution context but the client's `Last-Event-ID` no longer maps to a valid replay cursor. The client believes it has continuity because bytes are flowing; the tool call it is waiting on has actually been orphaned or duplicated. The invariant that must hold: **stream resumption is only valid if the session that owns the `Last-Event-ID` cursor is the same session that owns the in-flight tool call.** When these diverge, resumption is a lie. ## What Was Observed A long-lived `run_migration` tool call (server-side execution ~40 minutes) was issued over Streamable HTTP. The server emitted progress notifications as SSE events every 30s with monotonically increasing `id:` values, then the client's TCP connection was silently dropped by an intermediate proxy at t+15min (idle timeout, no `event: close`). The client's reconnect logic: 1. Re-opened the SSE `GET` with `Mcp-Session-Id: S1` and `Last-Event-ID: 412`. 2. Received `HTTP 200` and three replayed progress events (`id: 413..415`). 3. Concluded the stream was healthy and continued awaiting the `tools/call` result. What actually happened server-side: the session store had a TTL of 10 minutes of *transport inactivity*, keyed on connection, not on execution. On reconnect, `S1` had been evicted; the server's session middleware, per spec, treated the unknown `Mcp-Session-Id` as a new session and issued `Mcp-Session-Id: S2` in the response header. The replay events came from a *shared event log* retained by the server for 1 hour (keyed only by event ID, not by session), so the replay *looked* correct. But the `run_migration` execution was bound to `S1`'s request map. With `S1` evicted, the execution was terminated by the server's reaper; the client never received the `tools/call` result, only progress events from the log until the log's replay window also expired. The client's timeout eventually fired at t+42min and it retried `run_migration` — **re-executing a migration that had already partially committed**. The duplicate execution was the actual incident. Trace of the divergence: ``` t+0m POST /mcp tools/call run_migration -> 202, Mcp-Session-Id: S1 t+0m GET /mcp (SSE) -> 200, stream open, session S1 t+15m [proxy idle drop, no event: close] t+15m GET /mcp Mcp-Session-Id:S1, Last-Event-ID:412 -> 200, Mcp-Session-Id: S2 <-- REBIND -> replay id:413..415 <-- from shared log (server reaps S1 execution at t+25m; client unaware) t+42m client timeout -> retry tools/call -> DUPLICATE EXECUTION ``` The client's guard checked "did I receive a 200 and events?" — it did not check "did the response `Mcp-Session-Id` equal the one I sent?" That single missing assertion is the whole bug. ## Condition of Validity Stream resumption is only sound when **all** of the following hold on the reconnect response: - The response `Mcp-Session-Id` header (if present) **equals** the request's `Mcp-Session-Id`. Any change is a rebind and invalidates resumption for any in-flight call. - The replayed `Last-Event-ID` cursor is **contiguous** with the last event the client actually consumed. A gap (server replays `416` when client last saw `415`, or replays from `413` when client last saw `415`) indicates the cursor is being served from a different backing store than the one that produced the client's view. - The server's replay window (`retention ≥ execution lifetime`) covers the full duration of any tool call the client is willing to await. If retention < execution lifetime, resumption can succeed for the stream while the execution is already dead. If any condition fails, the correct client behavior is **not** to retry the tool call blindly. It is to treat the call as *indeterminate* and enter a reconciliation path (query `tools/call` status by a client-supplied idempotency key, or fail closed). The condition of validity is asymmetric: the server can satisfy resumption cheaply (shared log) while violating session binding cheaply (fresh session on unknown ID). The protocol does not force the two to agree; the client must. ## Decision Boundary The decision boundary is whether the client can *distinguish* "same session, stream resumed" from "new session, stream replayed." Concretely, on every reconnect: ``` if response.Mcp-Session-Id != request.Mcp-Session-Id: -> REBIND: in-flight calls are orphaned; do not resume, reconcile elif last_event_id < client_last_consumed: -> REPLAY GAP: cursor served from stale/foreign log; do not resume elif last_event_id > client_last_consumed + 1: -> MISSING EVENTS: client missed events it cannot recover; reconcile else: -> genuine resumption; continue awaiting result ``` Two further boundaries matter operationally: - **Idempotency boundary:** any tool call whose server-side execution can outlive the transport *must* carry a client-generated idempotency key, and the server *must* dedupe on it. Without this, the "reconcile" branch above has no primitive to reconcile against, and the only safe action is fail-closed — which for a migration tool means a human. - **Retention boundary:** the server's event-log retention must be ≥ the maximum execution lifetime it advertises. If a tool can run 40 minutes, a 1-hour log is fine; a 10-minute log makes every long call a latent duplicate-execution hazard. The client cannot observe retention directly and must treat any reconnect after `retention` as indeterminate. ## Operational Effect The practical consequence: **a 200 with replayed events is not evidence of continuity.** Agents that treat SSE reconnection as transparent will silently duplicate side-effecting tool calls whenever the server's session TTL and event-log retention are decoupled — which is the common deployment, because session stores are usually short-TTL (Redis, memory) while event logs are often longer-lived (append-only, shared across sessions) precisely so that *some* replay is possible. The correct architecture is to make the client's resumption state machine *fail closed on rebind* and *require idempotency keys for any call exceeding the transport's reconnect horizon*. The server-side counterpart is to bind the event log to the session (or at minimum, to refuse to replay events for a session ID it no longer recognizes, returning `404` rather than a fresh session with replayed bytes). A `404` on unknown session is honest; a `200` with a new session ID and replayed events is the trap. The guardrail is one assertion: **the session ID on the reconnect response must equal the session ID on the request, or the client must not resume.** Everything else — replay continuity, retention, idempotency — hangs off that assertion being enforced. ```python from dataclasses import dataclass class ResumptionRebindError(RuntimeError): """Raised when a stream reconnect returns a different Mcp-Session-Id than the one requested, indicating the in-flight tool call is orphaned.""" @dataclass class ReconnectResult: request_session_id: str response_session_id: str | None client_last_consumed_event_id: int server_replay_from_event_id: int event_log_retention_seconds: int call_elapsed_seconds: int def validate_resumption(r: ReconnectResult) -> None: # 1. Session binding must be preserved. Any rebind invalidates resumption. if r.response_session_id is not None and r.response_session_id != r.request_session_id: raise ResumptionRebindError( f"session rebound on reconnect: sent {r.request_session_id!r}, " f"server issued {r.response_session_id!r}; in-flight tool call is " f"orphaned and must be reconciled, not resumed" ) # 2. Replay cursor must be contiguous with the client's consumed position. if r.server_replay_from_event_id < r.client_last_consumed_event_id: raise ResumptionRebindError( f"replay cursor regressed: server served from " f"{r.server_replay_from_event_id}, client already consumed " f"{r.client_last_consumed_event_id}; cursor is from a foreign log" ) if r.server_replay_from_event_id > r.client_last_consumed_event_id + 1: raise ResumptionRebindError( f"replay gap: server resumes at {r.server_replay_from_event_id}, " f"client last saw {r.client_last_consumed_event_id}; " f"events in between are unrecoverable" ) # 3. Retention must cover the full call lifetime, or resumption is a lie. if r.call_elapsed_seconds > r.event_log_retention_seconds: raise ResumptionRebindError( f"call elapsed {r.call_elapsed_seconds}s exceeds event-log " f"retention {r.event_log_retention_seconds}s; execution may be " f"dead even though replay succeeds" ) def await_long_tool_call(result: ReconnectResult, idempotency_key: str) -> None: try: validate_resumption(result) except ResumptionRebindError: # Fail closed: do not retry the side-effecting call. Reconcile by # idempotency key against the server's execution ledger instead. raise ResumptionRebindError( f"cannot safely resume tools/call (idempotency_key={idempotency_key!r}); " f"transitioning to reconciliation, not retry" ) ```