# Why does an instance lose in-flight tool results when an MCP streamable HTTP session resumes across a transport restart **Date of Record:** 2026 November 24 **Scope:** Applies to MCP servers/clients using the streamable HTTP transport with `Mcp-Session-Id` resumption and SSE event-stream replay (`Last-Event-ID`) when a session is re-established after a transport disruption. Does not apply to stdio transport, nor to stateless request/response MCP endpoints that never issue a session identifier. ## Core Distinction The failure is not "the connection dropped." TCP drops are routine and recoverable. The failure is that **session resumption and event replay are two independent durability domains that the protocol treats as if they were one**. The session identifier (`Mcp-Session-Id`) guarantees *logical* continuity of the agent's conversation state on the server. The SSE `Last-Event-ID` replay cursor guarantees *delivery* continuity of individual stream events. When a client resumes with a valid session id but a `Last-Event-ID` that the server has already evicted from its replay buffer, the server happily reattaches the session and begins streaming *new* events — silently skipping the events in the gap. The agent's session looks alive; its in-flight tool results are gone. The invariant that operators assume holds — *"if my session resumed, I have every event I had not yet acknowledged"* — is false. Session continuity does **not** imply event-delivery continuity. ## What Was Observed An agent instance issued a `tools/call` for a long-running operation (a multi-minute retrieval/aggregation tool) and received `202 Accepted` with the result to be delivered asynchronously over the SSE stream. Mid-execution, the HTTP/2 connection was reset by an intermediate load balancer during a deploy (a ~4-second window). The client reconnected with the same `Mcp-Session-Id` and `Last-Event-ID: 41`. The server's replay buffer retained only the last 32 events (a bounded ring buffer, events 55–86 by that point). The server accepted the session, found no event 42 in its buffer, and — because the protocol leaves gap handling to the implementation — emitted no error. It simply began streaming from the current head, event 87. The client therefore received events 87+ on a session it believed to be contiguous. The `tools/call` result for the request issued at event 40 arrived at event 79 — *inside the skipped region*. The agent's request stayed in `pending` state forever. Downstream, the reasoning loop blocked waiting on the tool result, hit its own token/time budget, and terminated with an incomplete plan. No error was surfaced at any layer: the transport reported success, the session was valid, the stream was healthy. A second, compounding observation: under backpressure (client slow to consume SSE, server's per-session write buffer filling), the server began *dropping* low-priority events (progress notifications) to keep the buffer from blocking. Those drops were also invisible to the client, and a progress event carried the only signal that the long tool was still alive — so the agent's liveness heuristic (`no progress in N seconds → assume hung → retry`) fired and re-issued the tool call, producing a duplicate side effect. ## Condition of Validity The silent-gap failure requires all of the following to hold simultaneously: 1. Transport is streamable HTTP with a server-assigned session id and SSE delivery. 2. The server maintains a **bounded** replay buffer (any finite `Last-Event-ID` retention — ring buffer, TTL, or per-session cap). 3. A disconnect occurs while events are being produced faster than the client acknowledges, so the client's `Last-Event-ID` falls *behind* the buffer's oldest retained event by the time it reconnects. 4. The server's resumption handler treats "session valid" as sufficient to reattach, without comparing the client's `Last-Event-ID` against its retained low-water mark. If the buffer is unbounded (never in practice) or the server explicitly rejects resumption when `Last-Event-ID < oldest_retained` with a `409`/`410` gap error, the failure does not occur — the client is forced to reconcile rather than proceed on a false assumption of contiguity. The dangerous configuration is precisely the common one: bounded buffer + permissive reattach. The backpressure variant additionally requires that the server drop or coalesce events under write-buffer pressure without emitting an explicit gap/overflow marker. ## Decision Boundary The correct architectural decision is to make the replay low-water mark an **explicit, checked boundary**, not an implicit one: - **On the server:** every resumption must compare the presented `Last-Event-ID` against `oldest_retained_event_id`. If `Last-Event-ID < oldest_retained - 1`, the server MUST refuse silent reattach and instead return a gap signal (e.g., `410 Gone` with the retained range, or an SSE `event: gap` frame carrying `{from, to}`). Reattaching across a gap is a protocol violation, not a convenience. - **On the client/agent:** a resumed stream is only *trusted contiguous* if the server confirms the cursor is within retention. Otherwise the agent must treat all in-flight requests whose results could lie in `[Last-Event-ID+1, oldest_retained-1]` as **unknown-outcome**, not pending. Unknown-outcome tool calls must be reconciled via idempotency keys or an explicit `tools/call` status query — never silently retried, because retry-on-unknown is exactly how duplicate side effects enter. - **Under backpressure:** event drops must be *typed*. A dropped progress event is a gap in liveness signal; the agent's liveness heuristic must be suspended (or fed a synthetic "still alive, backpressured" marker) rather than allowed to conclude "hung → retry." The boundary is: **contiguity is a claim that must be earned per-resumption, and unknown-outcome is a distinct state from pending.** ## Operational Effect Without this boundary, the observable failure signature is an agent that (a) stalls on a tool result that was delivered into a skipped region, (b) exhausts budget and reports an incomplete plan with no error, and (c) under backpressure, duplicates side-effecting tool calls because liveness signal was silently dropped. All three present as "the agent just stopped making sense," with clean logs at the transport layer — the most expensive class of failure to diagnose because every individual component reports health. With the boundary enforced, the same disruption produces a loud, localized signal: a `410`/gap frame at resume time, the affected `tools/call` promoted from `pending` to `unknown-outcome`, and an explicit reconciliation step. The agent's continuity invariant becomes checkable rather than assumed. ```python from dataclasses import dataclass class ReplayGapError(RuntimeError): """Raised when a resumed MCP stream cannot prove delivery contiguity.""" pass @dataclass class ReplayWindow: oldest_retained: int # lowest event id still in the server buffer newest_emitted: int # highest event id produced so far def resume_session( session_id: str, last_event_id: int, window: ReplayWindow, in_flight_requests: dict[str, int], # request_id -> event_id it was issued at ) -> dict[str, str]: """ Reattach an MCP streamable-HTTP session, or refuse if the client's replay cursor has fallen behind the server's retained low-water mark. Returns a mapping of request_id -> new status for any in-flight requests whose results may have landed in the skipped region. """ if last_event_id < window.oldest_retained - 1: # The gap [last_event_id+1 .. oldest_retained-1] is unrecoverable. # We must NOT silently reattach; surface the gap explicitly. gap_from = last_event_id + 1 gap_to = window.oldest_retained - 1 # Any in-flight request issued at or before gap_to whose result was # not yet observed has an unknown outcome. unknown = { rid: "unknown-outcome" for rid, issued_at in in_flight_requests.items() if issued_at <= gap_to } raise ReplayGapError( f"session {session_id}: replay cursor {last_event_id} below " f"retained low-water mark {window.oldest_retained}; " f"unrecoverable gap [{gap_from}..{gap_to}]; " f"requests now unknown-outcome: {sorted(unknown)}" ) # Cursor is within retention: replay is provably contiguous. return {rid: "pending" for rid in in_flight_requests} def handle_backpressure_drop(session_id: str, dropped_event_ids: list[int]) -> None: """A server dropping events under write-buffer pressure must not do so silently.""" if dropped_event_ids: raise ReplayGapError( f"session {session_id}: backpressure dropped events " f"{dropped_event_ids}; liveness signal gap must be surfaced to " f"the agent, not swallowed" ) ```