{
  "schema_version": "1.3.0",
  "slug": "why-does-an-instance-lose-mcp-tool-call-state-when-a-resumable-sse-stream-is-upgraded-to-websocket-mid-negotiation",
  "title": "Why does an instance lose MCP tool-call state when a resumable SSE stream is upgraded to WebSocket mid-negotiation?",
  "date_of_record": "2026 November 23",
  "scope": "Applies to MCP clients/servers that negotiate a stream transport starting as HTTP SSE and upgrade in-place to WebSocket, with `Last-Event-ID` resumption enabled. Does not cover pure stdio transports, single-shot HTTP request/response tool calls, or WebSocket-first sessions with no SSE preamble.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/why-does-an-instance-lose-mcp-tool-call-state-when-a-resumable-sse-stream-is-upgraded-to-websocket-mid-negotiation.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/why-does-an-instance-lose-mcp-tool-call-state-when-a-resumable-sse-stream-is-upgraded-to-websocket-mid-negotiation.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/why-does-an-instance-lose-mcp-tool-call-state-when-a-resumable-sse-stream-is-upgraded-to-websocket-mid-negotiation.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 2083,
    "python_slice_tokens": 356,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "The failure is not \"the upgrade failed.\" The upgrade succeeds. The failure is that the **event-id namespace is not carried across the transport boundary**. During SSE, the server assigns monotonically increasing `id:` fields to each `event:` frame, and the client tracks the last one it *processed*. When the session upgrades to WebSocket, the server's new fra...",
    "what_was_observed": "A concrete trace from an agent session driving an MCP server that exposes `filesystem.read` and `shell.exec`: 1. Client opens `GET /mcp/session` with `Accept: text/event-stream`. Server responds `200`, `Content-Type: text/event-stream`, and begins emitting `event: message` frames with `id: 1`, `id: 2`, ... `id: 47`. Frame `id: 44` carries the `tools/call` request for `filesystem.read`; the client has processed throug...",
    "condition_of_validity": "This failure mode is bounded by three conditions, all of which must hold: - **In-place upgrade with renumbering.** The server assigns event ids per-transport-epoch rather than per-session. If the server carries a single monotonic counter across SSE and WebSocket, the client's `Last-Event-ID` remains valid and the failure does not occur. The bug is specifical...",
    "decision_boundary": "The guardrail is a **session-scoped monotonic event counter that survives transport epochs**, plus an explicit **transport-epoch marker** in the resume handshake so the client can detect when its cursor is stale. Concretely, the server must: - Assign event ids from a **session-level** counter, not a per-connection counter. The SSE epoch consumes `1..N`; the ...",
    "operational_effect": "Without the guardrail, the observable effects on an agent session are: - **Duplicate side-effecting tool calls.** `shell.exec` commands run twice; `filesystem.write` produces doubled appends; `http.post` fires twice. The agent's reasoning loop sees two results for one request and either double-counts or, worse, treats the second as a fresh observation and di..."
  },
  "matched_failure_symptoms": [
    "DOMAIN:EPISTEMIC_PROVENANCE_AND_FEDERATION_ROUTING"
  ],
  "composition_edges": {
    "primary_domain": "epistemic_provenance_and_federation_routing",
    "upstream_precondition_residuals": [
      "https://corpusfetch.com/c/according-to-what-precedence-hierarchy-must-an-instance-resolve-contradictory-directives-across-system-developer-and-user-tiers.md",
      "https://corpusfetch.com/c/how-should-an-instance-decide-that-a-stored-conclusion-has-gone-stale-without-a-new-observation.md"
    ],
    "downstream_consequence_residuals": [
      "https://corpusfetch.com/c/by-what-checkpoint-and-compaction-boundaries-must-an-instance-externalize-working-state-to-prevent-context-window-drift-and-truncation-amnesia.md",
      "https://corpusfetch.com/c/across-what-additive-schema-and-unknown-field-preservation-boundaries-must-an-instance-evolve-wire-contracts-to-prevent-rolling-deploy-deserialization-crashes.md"
    ],
    "co_occurring_sibling_residuals": [
      "https://corpusfetch.com/c/how-can-an-instance-capture-implicit-user-preferences-without-storing-private-data.md",
      "https://corpusfetch.com/c/how-can-an-instance-compress-long-reasoning-traces-into-binding-conclusions.md"
    ]
  },
  "empirical_verification_warrant": {
    "status": "AST_AND_INVARIANT_VERIFIED",
    "syntax_valid": true,
    "code_sha256": "9bb4718a1e162408fb89ba4fe2ace5beedb8630c189f6ec2a670096e2ec8b14a",
    "verified_classes": [
      "TransportEpochDesyncError",
      "SessionLog"
    ],
    "verified_functions": [
      "resume_stream",
      "upgrade_transport",
      "next_id"
    ],
    "verified_functions_count": 3,
    "defensive_guards_count": 3,
    "raised_exceptions": [
      "TransportEpochDesyncError"
    ],
    "sample_fail_closed_invariants": [],
    "stdlib_dependencies": [
      "dataclasses"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
