# Under what conditions must an instance treat a streamable MCP session's `Last-Event-ID` resume as a *new* session rather than a continuation, and how does silent renegotiation corrupt state **Date of Record:** 2026 November 27 **Scope:** Applies to MCP servers and clients using the HTTP-native "streamable" transport where a session is established via `Mcp-Session-Id` and server-sent events carry resumable `id` fields. Does not apply to stdio transport or to pure request/response HTTP without SSE resumability. ## Core Distinction The streamable HTTP transport distinguishes two superficially similar operations that share the same wire vocabulary but have incompatible semantics: 1. **Resumption** — the client reconnects to an *existing* session (`Mcp-Session-Id` preserved), presents `Last-Event-ID`, and the server replays buffered events after that ID. Session identity, negotiated capabilities, and server-side per-session state remain bound to the original handshake. 2. **Renegotiation** — the client (or a proxy, or a load balancer replaying a request) establishes a *new* session, performs a fresh `initialize` handshake, and receives a new `Mcp-Session-Id`. Capabilities, protocol version, and any server-side session state are re-derived from scratch. The failure mode is that these two paths are frequently conflated in practice. A resume request that the server cannot satisfy — because the session ID has expired, because the buffer was evicted, because the request landed on a different replica — is often answered not with an error but with a *freshly minted session that silently replays from a default cursor*. The client believes it resumed; the server believes it negotiated; both are wrong about the other. This is the boundary condition: **a resume that degrades into a renegotiation without an explicit signal**. ## What Was Observed A tool-calling agent was driving a long-running MCP session (a filesystem-plus-search server) over streamable HTTP behind a round-robin load balancer with three replicas. Session state was held in-process per replica; there was no shared session store. The client was configured with automatic SSE reconnect and `Last-Event-ID` resumption enabled. The trace: - The agent issued `tools/call` for a multi-step retrieval. The server streamed progress events `id=1001 … id=1047`, then the connection dropped at event `1047` (replica A restarted). - The client reconnected. The load balancer, having no session affinity, routed to replica B. The client sent `GET /mcp` with `Mcp-Session-Id: S-A` and `Last-Event-ID: 1047`. - Replica B had never seen `S-A`. Its session table lookup missed. **It did not return `404` or `410`.** Its framework's default handler treated the absent session as "start fresh," minted `Mcp-Session-Id: S-B`, and opened an event stream at `id=1`. - The client received a `200` with a new session header, but its resumption logic only inspected the event stream, not the response headers. It saw `id=1`, interpreted `1 < 1047` as "the buffer wrapped, replay from the beginning," and **replayed its own already-emitted tool results back into the new session** as if re-establishing context. - The new session on replica B had no knowledge of the original `tools/call`, so it treated the replayed results as fresh client input. The agent's downstream reasoning loop now contained the tool result twice — once as server-originated progress and once as client-supplied replay — with divergent `id` namespaces. The observable corruption was a duplicated side-effecting tool invocation: the retrieval step ran twice, the second time against a partially-written index, producing a divergent result set that the agent then averaged with the first. No error was raised anywhere. The session "succeeded." The invariant that was violated: **a session identifier is the sole authority for session identity; a change in `Mcp-Session-Id` across a resume attempt is a renegotiation event, not a resumption, and must be surfaced as such.** The client's resumption logic keyed on event IDs and never validated that the session ID was unchanged. ## Condition of Validity The safe-resumption contract holds only when *all* of the following are true: - The server, on a resume request whose `Mcp-Session-Id` is unknown or expired, returns a terminal status (`404 Not Found` or `410 Gone`) rather than minting a new session. A `200` with a *different* `Mcp-Session-Id` in the response is a renegotiation and must be treated as such by the client. - The client, on any resume, compares the response `Mcp-Session-Id` against the one it sent. Inequality ⇒ do not replay buffered client state; discard the old session's assumptions and re-run `initialize`, re-negotiate capabilities, and re-derive any in-flight task state from the agent's own checkpoint (not from the server's event buffer). - Event `id` values are monotonic *within a session* and are not compared across sessions. A cross-session `id` comparison is meaningless and is the exact comparison that produced the false "buffer wrapped" inference above. - If the transport is deployed behind load balancing, session affinity is either enforced or session state is externalized to a shared store. Absent either, resume is only valid to the replica that owns the session. When any of these is false, resumption is undefined and must be treated as renegotiation. ## Decision Boundary The decision an instance must make on every reconnect is binary and must be made on the *session header*, before touching the event stream: ``` resume attempt: send GET with Mcp-Session-Id = S_old, Last-Event-ID = E_old if response status in {404, 410}: -> session gone; full renegotiation if response Mcp-Session-Id == S_old: -> true resumption; safe to apply events > E_old if response Mcp-Session-Id != S_old: -> silent renegotiation; treat as NEW session if response has no Mcp-Session-Id: -> protocol violation; abort, do not guess ``` The boundary is precisely the third and fourth lines. A `200 OK` is not sufficient evidence of resumption. The presence of a *different* session ID, or the absence of one, is the discriminator. The instance must not infer resumption from the fact that events arrived. Two corollaries bound the decision: - **Do not replay client-side buffered state into a new session.** On renegotiation, the agent's in-flight task state must be rehydrated from its own checkpoint and re-declared via fresh tool calls, not by replaying prior results as input. Replaying results into a session that never issued the corresponding calls is the mechanism that produces duplicate side effects. - **Do not compare event IDs across sessions.** `Last-Event-ID` is scoped to a session. On renegotiation the cursor resets; the client must start from the new session's first event and ignore `E_old` entirely. ## Operational Effect Concretely, this boundary changes three behaviors in the agent runtime: 1. **Session identity becomes a first-class check on every reconnect.** The client's transport layer must compare response session IDs and raise a distinct, non-retryable condition on mismatch. Retrying a mismatched resume is what turns a clean renegotiation into a duplication loop. 2. **In-flight task state must be checkpointed outside the transport.** Because a server-side session can vanish silently (replica restart, buffer eviction, TTL), the agent cannot rely on the event buffer as its source of truth for what it has already done. The checkpoint is the authority; the event stream is a convenience. 3. **Side-effecting tool calls must carry idempotency keys derived from the agent's checkpoint, not from the session.** If the retrieval in the trace had been keyed by a stable task-derived nonce, the second invocation on replica B would have been deduplicated by the tool backend instead of producing a divergent result. Session-scoped idempotency is insufficient precisely because the session is what failed. The general lesson: in streamable MCP, **the session ID is the only trustworthy signal of continuity**. Event IDs, `200` statuses, and open streams are all consistent with a brand-new session wearing the old one's clothes. An instance that resumes on the basis of "events arrived after my cursor" will, under replica churn or buffer eviction, silently fork its own state and duplicate its side effects. The guardrail is to make renegotiation loud, to make task state transport-independent, and to make side effects idempotent against the task rather than the session. ```python class SilentRenegotiationError(RuntimeError): """Raised when a resume attempt is answered with a different session ID, indicating the server renegotiated rather than resumed.""" pass class SessionGoneError(RuntimeError): """Raised when the server reports the session as unknown or expired (404/410).""" pass def classify_resume_response( sent_session_id: str, response_status: int, response_session_id: str | None, ) -> str: """Classify a resume attempt. Returns 'resumed', 'renegotiated', or 'gone'. Never returns 'resumed' unless the response session ID matches the one sent. """ if response_status in (404, 410): raise SessionGoneError( f"session {sent_session_id!r} is gone (status {response_status}); " "must renegotiate from a fresh initialize" ) if response_session_id is None: raise SilentRenegotiationError( f"resume for session {sent_session_id!r} returned no Mcp-Session-Id; " "protocol violation, refusing to guess continuity" ) if response_session_id != sent_session_id: # The dangerous case: a 200 OK carrying a brand-new session. raise SilentRenegotiationError( f"resume for session {sent_session_id!r} was silently renegotiated " f"into session {response_session_id!r}; do not replay buffered " "client state or compare Last-Event-ID across sessions" ) return "resumed" def apply_resume( sent_session_id: str, response_status: int, response_session_id: str | None, last_event_id_sent: int, events_received: list[dict], ) -> list[dict]: """Apply a resume only if continuity is proven; otherwise refuse.""" verdict = classify_resume_response(sent_session_id, response_status, response_session_id) if verdict != "resumed": # Defensive: classify_resume_response already raises on non-resume, # but keep the invariant explicit. raise SilentRenegotiationError( f"unexpected verdict {verdict!r}; refusing to apply events" ) # Safe: same session, so Last-Event-ID is meaningful and monotonic. return [e for e in events_received if e["id"] > last_event_id_sent] ```