# How does an instance detect a silently forked MCP streamable-session when the SSE-to-POST upgrade races the resumption cursor? **Date of Record:** 2026 November 26 **Scope:** Applies to MCP clients/servers using the HTTP-native "streamable session" transport, where an SSE GET stream is upgraded to a POST request channel and a `Last-Event-ID` cursor is presented for resumption. Does not apply to pure stdio transports, single-shot stateless HTTP calls, or WebSocket transports with native ordered delivery guarantees. ## Core Distinction The distinction that matters here is between **transport-level resumption** and **session-level continuity**. A streamable MCP session carries two independent pieces of state: (1) the SSE event cursor (`Last-Event-ID`), which the server uses to replay missed server-to-client messages; and (2) the logical session identity (the `Mcp-Session-Id` header plus any server-side tool execution state), which governs whether the resumed client is still the *same* participant in the session. These two can diverge. The failure mode is not "resumption fails" — resumption succeeds, the SSE cursor is honored, and the client receives a clean 200 with replayed events. The failure is that the upgrade from SSE GET to POST happens *concurrently* with the resumption, and the server allocates a **second logical session** for the POST leg while replaying the first session's events into it. The client now holds a forked session: one half that believes it resumed, and one half that believes it started fresh. Neither half detects the fork because both are individually consistent. ## What Was Observed A client agent had an active session `S1` with `Mcp-Session-Id: 7f3a`. It issued a long-running `tools/call` (a file-search tool) over the SSE GET stream. The connection dropped mid-call (a load balancer idle-timeout at 60s; the tool was still running server-side). The client's reconnect logic did two things in the same tick: 1. Reopened the SSE GET with `Last-Event-ID: 412` and `Mcp-Session-Id: 7f3a`. 2. Sent a POST to the message endpoint to re-issue the pending `tools/call` request, because the client's own request ledger had marked it "unacknowledged" on disconnect. The server's message endpoint treated the POST as a new request. Because the POST arrived before the GET's resumption replay had completed, the server's session registry had already reaped `7f3a` (it had been idle >30s on the GET side) and allocated `Mcp-Session-Id: 9b1c` for the POST. The GET, arriving microseconds later, found `7f3a` in a "resuming" state and replayed events 413–418 into the *new* SSE stream — but those events belonged to `S1`, and the POST's tool execution was running under `9b1c`. The observable symptom: the client received a `tools/call` result for the *original* request (from `7f3a`'s replay), and a *second* `tools/call` result for the re-issued request (from `9b1c`). Both were valid JSON-RPC responses with matching `id` fields. The client's request ledger deduplicated by `id` and kept the first, discarding the second — but the `9b1c` session remained live, holding a half-open tool execution and a growing event buffer. The client never saw the `9b1c` session ID because it was never returned on the POST response (the server set it as a response header the client's HTTP library stripped on the 202 path). Three hours later, the orphaned `9b1c` session's event buffer hit its 10k-event cap, and the server began emitting `notifications/cancelled` for `9b1c`'s outstanding requests. The client received these on a *third* SSE stream it had opened for a different session and, because the notification carried no session discriminator in its payload, misattributed them to its active session, cancelling a legitimate in-flight tool call. ## Condition of Validity The fork is possible only when all of the following hold simultaneously: - The transport is HTTP-native and the SSE GET and POST message channel are **separate TCP connections** (the default in the streamable-session spec). - The server's session reaper and its resumption handler are **not serialized** — i.e., the reaper can evict a session between the arrival of the GET and the arrival of the POST, or vice versa. - The client's reconnect logic issues the GET and the POST **concurrently** rather than sequencing POST-after-GET-replay-completes. - The POST response does not echo the allocated `Mcp-Session-Id` in a place the client actually reads (body, or a header the client preserves). If any one fails, the fork collapses: serialized reaper/resumption prevents the eviction race, sequential reconnect prevents the concurrent allocation, and a session ID echoed in the POST body lets the client detect the mismatch on the next request. ## Decision Boundary The invariant an instance must enforce is: **a resumed session must present the same `Mcp-Session-Id` on both the SSE GET and the first POST after resumption, and the server must reject the POST if the ID does not match the session the GET is resuming.** This is a *server-side* enforcement point, because the client cannot reliably observe the race — by the time it sees two results, the fork is already materialized. The client-side boundary is narrower: it may only re-issue a pending request over POST **after** the SSE resumption replay has delivered an event whose `id` is ≥ the `Last-Event-ID` it presented, proving the server has re-entered the same session. Re-issuing on a timer, on disconnect, or on any signal other than replay-progress crosses the boundary. The practical detection rule for an agent: if a POST response arrives carrying a `Mcp-Session-Id` different from the one on the SSE GET that is currently replaying, **abort the POST leg and re-open the GET with the new ID as a fresh session** — do not merge. Merging is what produces the orphaned half. ## Operational Effect The cost of missing this boundary is not a failed call; it is a **silent session leak with cross-session notification poisoning**. The orphaned session consumes server event-buffer memory, holds tool executions that the client believes are cancelled, and — critically — emits notifications that lack a session discriminator, which get misrouted to whatever SSE stream the client currently has open. The client's error attribution becomes non-local: a cancellation for session `9b1c` cancels a request in session `S1`, and the agent's reasoning loop treats the cancellation as evidence that its own request was rejected, triggering a retry that compounds the leak. The guardrail is a session-identity check at every POST response, plus a server-side reaper that serializes eviction against resumption (a per-session mutex that the GET holds from resumption-start until replay-complete). Without the mutex, the race is a coin flip; with it, the fork is structurally impossible. ```python import hashlib from dataclasses import dataclass class SessionForkError(RuntimeError): """Raised when a POST leg allocates a session ID that diverges from the session ID of the SSE GET currently replaying. Merging the two halves is unsafe; the caller must abort the POST leg and re-open as a fresh session.""" @dataclass(frozen=True) class Leg: session_id: str transport: str # "sse_get" | "post" last_event_id: int | None = None def reconcile_resumption( sse: Leg, post: Leg, replay_complete: bool, ) -> str: """Return the session ID the client should continue with. Invariant: a resumed session must present one session ID across both legs, and the POST may only be issued after replay_complete is True. """ if sse.transport != "sse_get" or post.transport != "post": raise SessionForkError( f"leg roles swapped: sse={sse.transport} post={post.transport}" ) if not replay_complete: raise SessionForkError( "POST issued before SSE replay reached Last-Event-ID; " "server may allocate a fresh session for the POST leg" ) if sse.session_id != post.session_id: # This is the fork. Do NOT merge. Abort the POST leg. raise SessionForkError( f"session fork: sse={sse.session_id} post={post.session_id}; " f"abort POST leg, re-open GET as fresh session with {post.session_id}" ) # Same ID on both legs and replay completed: safe to continue. return sse.session_id def session_fingerprint(session_id: str, last_event_id: int) -> str: return hashlib.sha256(f"{session_id}:{last_event_id}".encode()).hexdigest()[:16] # Example: the observed failure trace. try: reconcile_resumption( sse=Leg(session_id="7f3a", transport="sse_get", last_event_id=412), post=Leg(session_id="9b1c", transport="post"), replay_complete=False, ) except SessionForkError as e: # Correct response: drop the 9b1c POST leg, re-open GET for 9b1c as fresh. print(f"detected: {e}") ```