# Why does an instance lose its tool-call ledger when an MCP session resumes across an SSE-to-StreamableHTTP transport boundary **Date of Record:** 2026 November 27 **Scope:** Applies to MCP clients and servers that migrate or bridge between the legacy HTTP+SSE transport (two-endpoint: `GET /sse` event stream + `POST /messages` uplink) and StreamableHTTP (single endpoint, `Mcp-Session-Id` header, optional `Last-Event-ID` resumption on the GET stream). Does not cover stdio transport, nor pure in-process MCP bindings where session identity is process-scoped. ## Core Distinction The migration literature frames SSE→StreamableHTTP as a *transport swap*: same JSON-RPC payloads, different wire framing. That framing hides the actual discontinuity. The two transports disagree about **what a session is and who owns the replay cursor**. In legacy HTTP+SSE, resumption is a property of the *event stream*: the client reconnects `GET /sse` with `Last-Event-ID`, and the server replays buffered SSE events from that cursor. The `POST /messages` uplink is stateless per request — the correlation between a `tools/call` request and its eventual result is carried *only* by the SSE event stream. There is no server-side request ledger keyed by anything the client can re-present on the uplink. In StreamableHTTP, resumption is a property of the *session*: the `Mcp-Session-Id` header identifies a durable server-side session, and `Last-Event-ID` on the GET stream is a resumption hint *within* that session. The server is expected to hold per-session state — including in-flight request tracking and the mapping from resumption cursor to undelivered responses. The failure lives exactly at the seam: a client that resumes with a valid `Last-Event-ID` but a *freshly minted or dropped* `Mcp-Session-Id` (or vice versa) gets a stream that replays cleanly while the server's request ledger has no record of the outstanding `tools/call`. The stream looks healthy; the call is orphaned. ## What Was Observed A tool-calling agent (call it the executor) issues `tools/call` for a long-running operation (a 40-second database export). The server dispatches it, streams progress notifications over SSE, and holds the final `result` for delivery on the event stream. At T+12s the client's HTTP connection drops (NAT rebind, not a server fault). Recovery trace, legacy path: 1. Client reconnects `GET /sse` with `Last-Event-ID: 118`. 2. Server replays events 119…N from its ring buffer, terminating with the `tools/call` result carrying `id: "req-7f3a"`. 3. Client correlates `req-7f3a` to its pending call, resolves it, continues. **No loss.** Recovery trace, migrated path (client now speaks StreamableHTTP to a bridging gateway that fronts the same legacy server): 1. Client reconnects `GET /mcp` with `Last-Event-ID: 118` but **without** `Mcp-Session-Id`, because the client's session store was keyed to the *connection object* and the connection object was destroyed on drop. 2. Gateway, seeing no session header, mints a new session `sess-9c1`. It opens a fresh upstream `GET /sse` with **no** `Last-Event-ID` (its cursor is per-session, and the new session has none). 3. Upstream replays from the head of its buffer — but the buffer has already evicted events 119…N under its retention window, or the upstream treats the new stream as a fresh subscriber and only emits *future* events. 4. The `tools/call` result for `req-7f3a` is emitted to the **old**, now-dead stream's buffer, never to `sess-9c1`. The client sits on an open, healthy-looking stream, waiting for a response that will never arrive on it. The agent does not error. It blocks until its own token/time budget expires, then — depending on its retry policy — **re-issues `tools/call` for the same export**, because it has no record that `req-7f3a` succeeded. The server, which did complete the export, now runs it a second time. This is the non-obvious part: the failure is silent on the wire and manifests as duplicated side effects plus a stalled reasoning loop, not as a transport error. A second variant was observed with the header present but mismatched: client presents `Mcp-Session-Id: sess-4a0` (its pre-drop value) to a gateway that has *already reaped* `sess-4a0` under an idle-timeout shorter than the client's reconnect backoff. The gateway returns `404 Session not found` on the GET, the client's library treats 404 as "start fresh," mints `sess-9c1`, and lands in the same orphaned-call state — but now with a misleading 404 in the logs that suggests the session was never valid. ## Condition of Validity The invariant that must hold for safe resumption is: > **For any in-flight request `r` with pending response, the tuple `(session_id, last_event_id, request_id)` presented on reconnect must resolve, server-side, to the same ledger entry that owns `r`.** This fails when any of the three components is regenerated independently of the others. Concretely, resumption is *unsafe* whenever: - `session_id` is bound to connection lifetime rather than to client-side durable state (the common migration bug: the client library's session field is initialized in `connect()` and cleared in `disconnect()`); - `last_event_id` is scoped per-session server-side, so a new session cannot resume an old cursor (true of most StreamableHTTP implementations, and the reason a fresh session silently drops the backlog); - the server's request ledger is keyed by session and garbage-collected on session reap, so a reaped session's in-flight results are unrecoverable even though the work completed. Resumption is *safe* only when the client persists `session_id` and `last_event_id` together in the same durable record as the pending `request_id` set, and re-presents all three atomically — and when the server retains the ledger entry for a grace window strictly longer than the client's maximum reconnect backoff plus the operation's max runtime. ## Decision Boundary The boundary is not "did the connection drop" — it is **"does the client's pending-request set survive the transport object."** Two operational rules follow: 1. **Persist the triple, not the connection.** The client must write `(session_id, last_event_id, {request_id: tool_name})` to durable agent memory *before* the first `tools/call` is dispatched, and update `last_event_id` on every received event. On reconnect, it must present all three or none. Presenting a subset is the failure trigger. 2. **On `404 Session not found`, do not silently re-mint.** A 404 on the GET with a previously-valid session id means the server reaped the ledger. The client must treat every request in its persisted pending set as **indeterminate** — not failed, not succeeded — and must reconcile via an idempotency key or a `tools/call` with a client-supplied `idempotency_key` field before re-issuing. Re-minting and re-issuing without reconciliation is what produces the duplicate export. The corollary for server implementers: a session reap must not discard in-flight ledger entries faster than the client's documented reconnect contract allows. If the server's idle-timeout is 30s and the client's backoff ceiling is 60s, the server has *guaranteed* the orphaned-call state for any drop occurring in the last 30s of a session's life. ## Operational Effect The observable signature of this failure in production is a specific, misleading triad: - **Healthy stream, no progress.** The agent's transport layer reports connected; the reasoning loop stalls on a single `tools/call` until the token budget expires. - **Duplicate side effects.** The retry re-executes the tool; if the tool is not idempotent (export, charge, write), the effect is doubled and the agent's later reasoning sees two results for one intent. - **A 404 or a fresh-session log line that looks benign.** The root cause — a `last_event_id` presented against a session that never owned it — is not logged by either side, because neither side considers the mismatch an error. The architectural guardrail is a **resumption manifest**: the client persists the triple atomically, the server retains ledger entries for `max(client_backoff) + max(operation_runtime)`, and the client refuses to dispatch any `tools/call` whose `request_id` is already in its persisted pending set without first reconciling. This converts a silent orphan into an explicit `IndeterminateCallError` that the agent's planner can route — retry with idempotency key, or escalate — instead of a stall that burns the budget and duplicates the work. ```python import time from dataclasses import dataclass, field class IndeterminateCallError(RuntimeError): """Raised when a pending tool call cannot be resolved after resumption. The call may have succeeded server-side; the client has no ledger entry tying it to the presented (session_id, last_event_id) tuple. """ @dataclass class ResumptionManifest: session_id: str | None last_event_id: str | None pending: dict[str, str] = field(default_factory=dict) # request_id -> tool_name idempotency_keys: dict[str, str] = field(default_factory=dict) def resume_session( manifest: ResumptionManifest, presented_session_id: str | None, presented_last_event_id: str | None, server_ledger: dict[str, set[str]], # session_id -> request_ids owned max_backoff_s: float = 60.0, session_age_s: float = 0.0, ) -> ResumptionManifest: """Reconcile a resumption attempt against the server ledger. Invariant: the presented triple must resolve to the same ledger entry that owns every request in `manifest.pending`. Any partial match is an indeterminate state, not a resumable one. """ if presented_session_id is None and manifest.session_id is not None: # Client dropped the session id but still holds a cursor: the cursor # is scoped per-session server-side, so it cannot be honored. if manifest.pending: raise IndeterminateCallError( f"cursor {presented_last_event_id!r} presented without session " f"{manifest.session_id!r}; {len(manifest.pending)} calls orphaned" ) return ResumptionManifest(None, None) if presented_session_id not in server_ledger: # Session was reaped. Every pending call is indeterminate until # reconciled via idempotency key; do NOT silently re-mint and retry. if manifest.pending: raise IndeterminateCallError( f"session {presented_session_id!r} reaped with " f"{len(manifest.pending)} pending calls; reconcile before retry" ) return ResumptionManifest(None, None) owned = server_ledger[presented_session_id] orphans = set(manifest.pending) - owned if orphans: raise IndeterminateCallError( f"requests {sorted(orphans)} not in ledger for " f"session {presented_session_id!r}" ) if session_age_s > max_backoff_s: raise IndeterminateCallError( f"session age {session_age_s}s exceeds reconnect contract " f"{max_backoff_s}s; ledger retention not guaranteed" ) return ResumptionManifest( session_id=presented_session_id, last_event_id=presented_last_event_id, pending=dict(manifest.pending), idempotency_keys=dict(manifest.idempotency_keys), ) ```