# Where must an instance reconcile SSE `Last-Event-ID` resumption against MCP session state when the streamable HTTP transport reconnects mid-tool-call **Date of Record:** 2026 November 29 **Scope:** Applies to MCP clients/servers using the Streamable HTTP transport where a server-initiated SSE stream carries JSON-RPC responses/notifications and the client resumes via `Last-Event-ID` after a disconnect. Does not apply to the deprecated HTTP+SSE dual-endpoint transport, stdio transports, or streams that carry no event IDs. ## Core Distinction There are two independent notions of "resume" in MCP-over-Streamable-HTTP, and they are routinely conflated: 1. **Transport-level resumption** — the SSE layer's `Last-Event-ID` replay buffer. This guarantees that *bytes the server already emitted* are re-delivered in order after a reconnect. It is a replay of a log, not a re-execution of logic. 2. **Session-level continuity** — the MCP `Mcp-Session-Id` binding, which owns the JSON-RPC request/response correlation table, in-flight tool-call state on the server, and the client's pending-request map. The boundary failure occurs when a client treats a successful transport-level replay (HTTP 200, events flowing, `Last-Event-ID` honored) as proof that session-level state is intact. It is not. The event log and the session state can diverge, and the divergence is silent because the transport reports success. ## What Was Observed A tool call (`tools/call` with a long-running server-side operation, e.g. a code-execution or retrieval tool taking 40–90s) was issued over the Streamable HTTP transport. The server streamed progress notifications as SSE events with monotonically increasing `id:` fields, then held the connection open awaiting the final `result`. At T+35s the client's HTTP connection dropped (LB idle timeout, not a server event). The client reconnected with `Last-Event-ID: `. The server's SSE replay buffer still held the buffered progress events and replayed them — HTTP 200, events delivered, no error. The client's transport layer marked the stream "healthy." But the server-side session had, on the dropped connection, treated the disconnect as a *stream termination* and — depending on implementation — either (a) cancelled the in-flight tool invocation and discarded its state, or (b) kept it running but bound the result to the *old* stream's response slot. In case (a), the client waited forever for a `result` event that would never be emitted; the replay buffer contained only the pre-disconnect progress events, so the client saw a healthy stream with no terminal message. In case (b), the result was emitted but the client's pending-request map had been rebuilt on reconnect with a *new* internal request id, so the arriving `result` matched no pending entry and was silently dropped as an orphan. The observable signature: **replay succeeds, final response never arrives, no error is raised at any layer.** Liveness checks pass because the SSE connection is open and heartbeats flow. A sharper variant: the server reused event IDs across sessions (a global counter reset on process restart). A client resuming with `Last-Event-ID: 412` from a prior session hit a *new* session whose buffer had also reached event 412, and the server replayed an unrelated session's events — including a `result` for a different request id. The correlation table on the client rejected it, but the events polluted the stream and advanced the client's `Last-Event-ID` past the point where its real replay would have resumed. ## Condition of Validity The `Last-Event-ID` resume contract is only sound under a conjunction that the spec does not enforce: - The server MUST scope event IDs to the session (and ideally to the stream), not to a process-global counter. IDs are only meaningful relative to the buffer that produced them. - The server MUST retain in-flight tool-call state across a transport disconnect for at least the client's reconnect window, OR MUST emit an explicit terminal error event for the cancelled request before closing. - The client MUST NOT advance its `Last-Event-ID` watermark on events whose `request id` does not match a pending entry in its correlation table. - The client MUST treat "stream open + no terminal event within the request's deadline" as a distinct failure state from "stream closed." When any conjunct is false — and in practice the first and third are the common failures — transport success and session continuity decouple, and the failure is an unbounded wait, not an exception. ## Decision Boundary The operative rule: > A replayed SSE stream is evidence about the *log*, not about the *session*. Resumption is only valid if the replayed event's correlation id maps to a live pending request AND the server's session state for that request survived the disconnect. Absent an explicit terminal event, the client must escalate to session-level recovery (re-issue or query status), not wait on the stream. Concretely, the client must distinguish three outcomes on reconnect: - **Replay with matching pending request + subsequent terminal event** → resume normally. - **Replay with no matching pending request** → discard replayed events, do not advance watermark, treat request as orphaned → re-issue or fail. - **Replay exhausted, stream open, no terminal event** → the request's server-side state is unknown; do not block. This is the boundary case the naive client misses. ## Operational Effect Without this boundary, agents exhibit a *silent hang*: the reasoning loop blocks on a tool result that will never arrive, the token budget drains on heartbeats and retries, and the orchestrator's liveness probe (which checks SSE connectivity) reports green. The failure surfaces only at the outer timeout, misattributed to the tool or the model rather than to the resume semantics. The guardrail is to make the correlation table authoritative over the event watermark, and to bound every pending request with a deadline that is independent of stream health. The code below encodes that invariant: it refuses to advance the watermark on unmatched events, and raises a typed error when replay completes without a terminal event for a live request. ```python import time from dataclasses import dataclass, field from typing import Dict, Optional class SessionResumeDivergence(RuntimeError): """Raised when transport-level SSE replay succeeds but session-level continuity for a pending request cannot be established.""" pass @dataclass class PendingRequest: request_id: str deadline: float terminal_seen: bool = False @dataclass class ResumeState: session_id: str last_event_id: int = 0 pending: Dict[str, PendingRequest] = field(default_factory=dict) def handle_replayed_event(state: ResumeState, event: dict) -> Optional[str]: """Process one replayed SSE event. Returns request_id if it terminated a request. Refuses to advance the watermark on unmatched events.""" ev_id = event["id"] req_id = event.get("request_id") if req_id is None or req_id not in state.pending: # Orphaned event: do NOT advance last_event_id. Polluting the # watermark here is what causes real replay to be skipped. return None if ev_id <= state.last_event_id: # Non-monotonic replay within a session: reject, do not advance. return None state.last_event_id = ev_id if event.get("type") in ("result", "error"): state.pending[req_id].terminal_seen = True del state.pending[req_id] return req_id return None def reconcile_after_reconnect( state: ResumeState, replayed_events: list, stream_open: bool, ) -> list: """After replay is exhausted, decide per-request whether continuity holds. The invariant: an open stream with no terminal event for a live request is a divergence, not a healthy wait. We raise rather than block. """ resolved = [] for ev in replayed_events: rid = handle_replayed_event(state, ev) if rid is not None: resolved.append(rid) now = time.monotonic() for req_id, req in list(state.pending.items()): if req.terminal_seen: continue if now >= req.deadline: raise SessionResumeDivergence( f"session {state.session_id}: request {req_id} replayed " f"without terminal event and past deadline; server-side " f"state unknown, re-issue required" ) if stream_open and state.last_event_id == 0: # Replay produced nothing usable for this request; stream is # open but carries no continuity evidence. raise SessionResumeDivergence( f"session {state.session_id}: open stream but no replay " f"coverage for request {req_id}; treating as orphan" ) return resolved ```