# Why does an instance lose tool-call results when an MCP streamable-HTTP session resumes across a load-balanced replica boundary **Date of Record:** 2026 November 25 **Scope:** Applies to MCP servers implementing the streamable HTTP transport with `Mcp-Session-Id` headers, `Last-Event-ID` resumption, and per-session SSE event logs, when deployed behind a load balancer that does not pin sessions to a replica. Does not apply to single-replica deployments or to stdio transports where the session is the process. ## Core Distinction The streamable HTTP transport separates two things that stdio conflates: the *session identity* (a `Mcp-Session-Id` string the client echoes on every request) and the *session state* (the in-memory event log, pending request map, and in-flight tool-call futures). The spec guarantees resumption semantics — a client that reconnects with `Last-Event-ID` receives every event after that ID — but it does not guarantee that the replica answering the reconnect holds the event log the ID refers to. The distinction that matters operationally: **resumption is a protocol contract over a logical session, while the transport implementation is a physical replica.** When those two are not co-located, a resumable stream silently degrades into a fresh stream that begins at an arbitrary offset, and the client's reasoning loop treats the gap as "no events happened." ## What Was Observed A three-replica MCP server behind a round-robin LB. The client (an agent runtime) had issued `tools/call` for a long-running operation (a ~40-second browser scrape) and received an SSE stream carrying `progress` notifications and a final `result` event. The LB's idle timeout was 30 seconds, shorter than the tool call. The connection dropped mid-call. The client reconnected with `Last-Event-ID: 7` — correct per spec — and was routed to replica B, which had never seen session `sess-a1f3`. Replica B had no event log for that ID. The failure trace: 1. Replica A accepted `tools/call`, created session `sess-a1f3`, emitted events 1–7 (`progress` at 0/25/50/75%, then a `notifications/message`), and held a pending future for the tool result. 2. LB idle timeout (30s) closed the SSE stream. Replica A's pending future was still running; the event log still held 1–7. 3. Client reconnected, LB routed to replica B with `Mcp-Session-Id: sess-a1f3` and `Last-Event-ID: 7`. 4. Replica B's session table had no entry. Two implementations were observed across teams: - **Lenient path:** replica B created a *new* session under the same ID and returned an empty 200 SSE stream. The client blocked forever waiting for the `result` event that replica A was about to emit into a log nobody was reading. The tool call completed on replica A; its result was orphaned. - **Strict path:** replica B returned `404 Not Found` for the unknown session. The client's resumption logic treated 404 as "session expired, re-initialize," re-ran `initialize`, and then *re-issued the `tools/call`* — the browser scrape ran twice, and the agent's reasoning loop received two results for one logical action, corrupting its state. The non-obvious part: the spec's resumption contract is satisfied at the *client* level in both paths. The client did everything right. The invariant that broke was never stated in the transport spec because it is a *deployment* invariant: **the event log referenced by a `Last-Event-ID` must be reachable from any replica that can receive that ID.** ## Condition of Validity Resumption semantics are valid only when one of these holds: - **Session affinity:** the LB pins `Mcp-Session-Id` to a replica for the session's lifetime (consistent hashing on the session header, or sticky cookies). Then `Last-Event-ID` always lands on the log that produced it. - **Shared event log:** the event log is externalized (Redis stream, Postgres table, object store) and every replica can read events after an arbitrary ID and, critically, can *attach* to the still-running tool-call future — which requires the future itself to be externalized or the call to be re-runnable idempotently. - **Explicit non-resumability:** the server never emits IDs, never honors `Last-Event-ID`, and returns a documented error on reconnect that the client interprets as "re-issue is safe." This is valid only if every tool is idempotent or the client carries a dedup key. The failure occurs in the gap: a server that *emits* `Last-Event-ID` (claiming resumability) but does not *guarantee* log reachability. The presence of the header is a promise the deployment cannot keep. ## Decision Boundary The boundary is whether the tool call is **replayable** or **effectful**: - For a *read-only* tool (search, fetch, query), a 404-on-unknown-session that forces re-issue is correct and cheap. The client re-runs the call; no harm. - For an *effectful* tool (scrape with side effects, payment, file write, message send), the 404 path is a correctness bug: it converts a transport failure into a duplicate execution. The agent's reasoning loop has no way to know the first call succeeded, because the result event was emitted into an orphaned log. The decision rule: **if any tool in the session is effectful, the session must either be pinned or the event log must be shared with future-attachment.** A server that cannot do either must not advertise resumability — it should omit event IDs and document that reconnects are fresh sessions, forcing the client to carry idempotency keys at the tool layer. The worst configuration is the middle: resumable-looking headers over a non-resumable deployment. ## Operational Effect The observable symptom is a *silent* reasoning divergence, not an error. The agent's loop sees either (a) an empty stream that never terminates — a hang that burns the context window with an unanswered tool call, or (b) a duplicate result that it treats as two distinct observations, double-counting in its working memory. Neither surfaces as an exception. The failure is only detectable by correlating server-side tool-execution logs against client-side result counts — a check most agent runtimes do not perform. The guardrail that bounds it: a session-reachability precheck on every reconnect. Before accepting a `Last-Event-ID`, the replica must confirm it can resolve the session's log *and* the pending future; if not, it must return a distinct error code (not 404, which clients map to re-initialize) that the client interprets as "do not re-issue; the original call may still be running." The client, on that code, polls a status endpoint keyed by the original request ID rather than re-running the tool. ## Operational Effect Deployments that added session affinity reduced orphaned-result incidents to zero but introduced a new failure: replica death mid-session loses the log entirely, and the client's resumption now hangs on a dead pin. Deployments that externalized the event log but not the future still orphaned results on replica death — the log was readable, but the running tool call died with the process, and the client waited for a `result` event that would never be written. Only externalizing *both* the log and the pending-call state (or making calls idempotent with client-side dedup keys) closed the gap. The invariant to enforce: **a `Last-Event-ID` is a promise that the event stream is durable and reachable; if either half is false, the header must not be emitted.** ```python import hashlib from dataclasses import dataclass, field from typing import Optional class SessionResumptionError(RuntimeError): """Raised when a Last-Event-ID cannot be honored by the receiving replica.""" pass @dataclass class SessionState: session_id: str replica_id: str event_log: list = field(default_factory=list) # list of (event_id, payload) pending_call: Optional[str] = None # request id of in-flight tool call effectful: bool = False class StreamableHTTPReplica: def __init__(self, replica_id: str, shared_log_backend=None): self.replica_id = replica_id self.local_sessions: dict[str, SessionState] = {} # shared_log_backend: optional external store keyed by session_id self.shared_log_backend = shared_log_backend def _resolve_session(self, session_id: str) -> Optional[SessionState]: if session_id in self.local_sessions: return self.local_sessions[session_id] if self.shared_log_backend is not None: return self.shared_log_backend.get(session_id) return None def resume(self, session_id: str, last_event_id: int, client_will_reissue: bool) -> list: """ Handle a reconnect carrying Last-Event-ID. The spec says: return events after last_event_id. The deployment invariant says: only do that if the log is reachable AND, for effectful sessions, the pending future is reachable or the call is safely re-issuable. """ session = self._resolve_session(session_id) if session is None: # Unknown session on this replica. Do NOT return a bare 404 that the # client maps to re-initialize + re-issue; that duplicates effectful calls. if client_will_reissue: raise SessionResumptionError( f"session {session_id} unknown on replica {self.replica_id}; " f"refusing to signal re-issue because the original call may be " f"in-flight on another replica" ) raise SessionResumptionError( f"session {session_id} unreachable from replica {self.replica_id}; " f"event log not shared and no affinity" ) # Log is reachable. But if the session is effectful and the pending call # future is not resolvable here, resuming the stream would hang the client # waiting for a result that this replica cannot produce. if session.effectful and session.pending_call is not None: future_reachable = ( session.replica_id == self.replica_id or self.shared_log_backend is not None ) if not future_reachable: raise SessionResumptionError( f"session {session_id} log reachable but pending call " f"{session.pending_call} lives on replica {session.replica_id}; " f"resuming here would block the client indefinitely" ) return [e for e in session.event_log if e[0] > last_event_id] def emit(self, session_id: str, payload: dict) -> int: session = self._resolve_session(session_id) if session is None: raise SessionResumptionError(f"cannot emit into unknown session {session_id}") event_id = (session.event_log[-1][0] + 1) if session.event_log else 1 session.event_log.append((event_id, payload)) return event_id def reconnect_guard(replica: StreamableHTTPReplica, session_id: str, last_event_id: int, tool_is_effectful: bool) -> list: """ Client-facing entry point. Enforces the deployment invariant before the protocol contract is honored. """ try: events = replica.resume(session_id, last_event_id, client_will_reissue=not tool_is_effectful) except SessionResumptionError as e: # Surface a distinct code so the client polls status instead of re-issuing. raise SessionResumptionError( f"resumption refused: {e}. Client must poll by original request id, " f"not re-issue the tool call." ) from e if tool_is_effectful and not events: raise SessionResumptionError( f"effectful session {session_id} resumed with empty event tail; " f"result may have been emitted into an orphaned log — poll status" ) return events ```