# When must an instance treat an MCP Streamable-HTTP `Mcp-Session-Id` rebind as a context-invalidating event rather than a transparent reconnect? **Date of Record:** 2026 November 29 **Scope:** Applies to MCP clients speaking the HTTP-native Streamable transport (POST JSON-RPC + optional SSE upgrade on the same session) against servers that advertise stateless resumption or that drop `Mcp-Session-Id` state between edge nodes. Does not apply to stdio transports, nor to stateful single-node servers that pin session state in-process. ## Core Distinction There are two superficially identical events that arrive on the wire as "the session is no longer the session you had": 1. **Transport re-establishment** — the TCP/SSE connection broke, the client reconnects, the server still holds the same logical session (`Mcp-Session-Id` accepted, tool-call ledger intact). Here the client may transparently resume; the reasoning context on the client side is still valid because no server-side state was lost. 2. **Session rebind** — the server has *rejected or forgotten* the prior `Mcp-Session-Id` and issued (or accepted) a fresh one, typically because the request landed on a different stateless edge node. The wire looks like a successful handshake. But any server-side state that the agent's plan depended on — pagination cursors, in-flight tool executions, subscription registrations, resource handles — is now gone or, worse, silently re-initialized under a different identity. The invariant the agent must enforce: **a change in `Mcp-Session-Id` (including `404` on a previously valid id followed by a fresh id in the response header) is a context-invalidation event, not a reconnect.** The failure mode is that the transport layer reports success while the semantic layer has been reset, and the probabilistic planner continues executing against stale assumptions. This is non-obvious because the Streamable transport is *designed* to allow stateless servers, and the spec explicitly permits a server to respond to a request with a new `Mcp-Session-Id`. Clients that treat the header as advisory will silently lose state. ## What Was Observed A research agent with a multi-step plan ("enumerate issues → for each, fetch thread → summarize → post digest") was talking to an MCP server behind a round-robin load balancer with three stateless replicas. Replica state was held in a request-scoped cache with a 30-second TTL and *not* shared. Trace: - Step 1: `initialize` → `200`, header `Mcp-Session-Id: A`. Client stores `A`. - Step 2: `tools/call` `list_issues` with `cursor: null` → `200`, header `Mcp-Session-Id: A`, body returns `nextCursor: "c2"`. Replica 1. - Step 3: `tools/call` `list_issues` with `cursor: "c2"` → lands on replica 2. Replica 2 does not know session `A`. It responds `404` with `Mcp-Session-Id: B` and a JSON-RPC error `-32600` ("session not found"). The client's HTTP layer, configured for "retry on 404 during reconnect," re-issues `initialize` against `B`, gets `200`, and resumes. - Step 4: `tools/call` `list_issues` with `cursor: "c2"` again — but now against session `B`. Replica 2 has no cursor `c2` because cursors are per-session. It returns `nextCursor: null` and an empty page. The agent's planner observed "empty page, done enumerating" and proceeded to post a digest summarizing **zero issues**. No error surfaced to the reasoning loop. The tool returned `200`. The JSON-RPC envelope was well-formed. The failure was entirely in the semantic contract: the cursor was bound to session `A`, and the client had silently rebound to `B`. A second observed variant: on SSE-to-streamable upgrade, the server sent `200` with `Content-Type: text/event-stream` on the initial POST, then a subsequent POST was routed to a replica that had no record of the SSE stream. The server responded with a fresh `Mcp-Session-Id` and a `202 Accepted` (no body), which the client interpreted as "notification acknowledged." The actual server-side subscription — the one that was supposed to deliver tool results asynchronously — had never been registered on that replica. The agent waited on a stream that would never emit. ## Condition of Validity The rebind-as-invalidation rule is *required* when any of the following hold, and may be relaxed (treated as transparent reconnect) only when all are false: - The plan holds a **server-issued opaque handle** across steps: pagination cursors, upload IDs, job IDs, resource URIs, subscription IDs, or `progressToken` values. - The server advertises stateless resumption or is deployed behind a load balancer without session affinity (detectable via `Mcp-Session-Id` churn, or via a server capability flag such as `sessionAffinity: false`). - The transport is Streamable HTTP with an SSE upgrade, where the upgrade is a *separate* request that may route to a different replica than the POST that established the session. - Any tool in the plan is **non-idempotent** (post, create, send). A silent rebind followed by a retry can double-execute. Conversely, the rule does *not* apply when the server guarantees session affinity (e.g., `Mcp-Session-Id` is a signed token containing the replica id, and the LB routes on it) and the id is unchanged across the reconnect. In that case a dropped SSE is genuinely just a transport event. ## Decision Boundary The boundary is the moment the client observes, on any response: ``` Mcp-Session-Id != previously_stored_id OR (status == 404 AND previously_stored_id was set) OR (status == 202 on a POST that was expected to return a result body) ``` At that instant the agent must choose between two actions, and the choice is **not** "retry": - **Invalidate-and-replan** (default): discard all server-issued handles, mark the current plan step as `STALE_HANDLE`, and re-derive the step from the plan's durable state (the agent's own episodic memory of what it has already confirmed), not from the transport's cached cursor. - **Resume-with-proof**: only permitted if the server returns an explicit continuity token (e.g., a `Mcp-Session-Id` that is cryptographically bound to the prior one, or a server capability `resumable: true` with a replay endpoint). Without such proof, resume is unsafe. The critical anti-pattern: the HTTP layer's generic retry-on-404 logic must not be allowed to auto-reinitialize the session and hand the *new* id back to the planner as if nothing happened. The transport must surface the rebind to the reasoning layer as a typed event, because only the reasoning layer knows whether the current step depends on a handle that just became invalid. ## Operational Effect Agents that treat rebind as reconnect exhibit a specific, hard-to-diagnose signature: **plausible but empty or duplicated results.** Empty pagination pages, digests of zero items, duplicate posts (because a non-idempotent call was retried under a fresh session that had no record of the first attempt), and streams that never emit. None of these raise a transport error; the JSON-RPC layer is healthy. The failure only becomes visible when a downstream consumer notices the digest is wrong — often after the agent has reported success. The guardrail is to make session identity a first-class field in the agent's working memory, not a transport-internal detail. Every tool result that carries a server-issued handle must be tagged with the `Mcp-Session-Id` under which it was issued. Before the next step, the agent compares the current session id to the tag; a mismatch forces replan. This converts a silent semantic reset into an explicit, recoverable state transition. ```python from __future__ import annotations from dataclasses import dataclass, field from typing import Optional class SessionRebindError(RuntimeError): """Raised when a server-issued handle is used under a rebound MCP session.""" @dataclass class HandleBinding: session_id: str cursor: Optional[str] = None job_id: Optional[str] = None @dataclass class AgentSessionState: session_id: Optional[str] = None bindings: list[HandleBinding] = field(default_factory=list) stale: bool = False def observe_session_header(self, new_id: Optional[str]) -> None: """Called on every response. A changed id is a rebind, not a reconnect.""" if new_id is None: return if self.session_id is not None and new_id != self.session_id: # Rebind detected: invalidate every handle bound to the old session. self.stale = True self.bindings.clear() self.session_id = new_id def bind_handle(self, cursor: Optional[str] = None, job_id: Optional[str] = None) -> HandleBinding: if self.session_id is None: raise SessionRebindError("cannot bind handle before session established") b = HandleBinding(session_id=self.session_id, cursor=cursor, job_id=job_id) self.bindings.append(b) return b def require_handle(self, binding: HandleBinding) -> HandleBinding: """Guard called before reusing a server-issued handle in a later step.""" if self.stale: raise SessionRebindError( "session rebound; handle invalidated and plan must be re-derived " "from durable state, not the transport cursor" ) if binding.session_id != self.session_id: raise SessionRebindError( f"handle bound to session {binding.session_id!r} used under " f"session {self.session_id!r}" ) return binding def resume_pagination(state: AgentSessionState, binding: HandleBinding) -> HandleBinding: """Resume a paginated tool call. Refuses to resume across a rebind.""" return state.require_handle(binding) ```