Skip to content

Kubernetes stdio MCPServer: proxy and backend pod lifecycles are decoupled, so a restart of either breaks the initialize handshake #6974

Open

Description

@reefland

Bug description

When the operator runs an MCPServer with transport: stdio, the proxy is a Deployment and the MCP server process is a separate StatefulSet pod (<name>-0). Each can restart on its own. The stdio proxy's initialize handling (#1982) assumes the two share one lifecycle: it forwards the first client initialize to the backend and answers every later client from its cache. When only one side restarts, the proxy and backend disagree about whether the session is initialized. MCP servers that enforce the lifecycle strictly (anything on the Go MCP SDK, e.g. github-mcp-server, kubernetes-mcp-server) then stop serving tools, while the proxy's /health stays green.

Two failure modes:

  1. Proxy restarts, backend keeps running. This happens on every operator upgrade, because the operator updates TOOLHIVE_RUNNER_IMAGE and rolls all proxy Deployments while leaving the <name>-0 pods alone. The new proxy forwards a fresh initialize to a backend that the old proxy already initialized, and the backend rejects it with duplicate "initialize" received.
  2. Backend restarts, proxy keeps running. Since v0.56.0 the proxy re-attaches to the new <name>-0 pod in place ("container is still running after stdout EOF, attempting to re-attach"). It keeps its cached initialize response, so it never sends initialize to the new backend. The backend then rejects every request with method "tools/call" is invalid during session initialization.

Steps to reproduce

Mode 1 (proxy restart):

  1. Deploy a stdio MCPServer backed by a Go-SDK server:
    apiVersion: toolhive.stacklok.dev/v1beta1
    kind: MCPServer
    metadata:
      name: github
    spec:
      image: ghcr.io/github/github-mcp-server:v2.0.3
      transport: stdio
  2. Connect a client and complete initialize / tools/list. It works.
  3. Restart only the proxy, either with kubectl rollout restart deploy/github or by upgrading the operator chart (we saw it going from 0.57.0 to 0.58.0).
  4. Connect a new client and send initialize. It fails with duplicate "initialize" received.

Mode 2 (backend restart):

  1. Same MCPServer, already initialized by a client.
  2. kubectl delete pod github-0.
  3. The proxy logs that it is re-attaching. Connect a client, initialize (answered from the cache), then tools/call. It fails with method "tools/call" is invalid during session initialization.

Expected behavior

The proxy sends exactly one initialize to each backend process it talks to. When it attaches to a backend process it didn't initialize, or re-attaches after the backend was replaced, it either re-initializes the new process and refreshes its cache, or restarts the backend pod so both sides start fresh. Clients see working tools after either side restarts.

Actual behavior

  • Mode 1: every new client initialize fails until someone deletes <name>-0 by hand.
  • Mode 2: initialize succeeds from the cache, but every request after it fails until someone deletes the proxy pod by hand.
  • Both: the proxy's readiness and liveness checks stay green, so Kubernetes never recovers on its own. MCP gateways that aggregate tools (we use LiteLLM) silently drop the server's tools from their tool list. Clients that cache that list keep running without those tools until they reconnect.

Lenient servers (Python and TypeScript SDK servers in our setup) accept a repeated initialize, so only strict servers show the problem.

Environment (if relevant)

  • OS/version: k3s v1.36.5+k3s1 (Ubuntu 24.04 nodes)
  • ToolHive version: operator / proxyrunner v0.58.0 (mode 1). Mode 2 first seen on v0.56.0 and still present.
  • Affected backends: ghcr.io/github/github-mcp-server:v2.0.3, kubernetes-mcp-server (both Go SDK)
  • Client path: LiteLLM /mcp gateway → ToolHive proxy (streamable HTTP) → stdio backend

Additional context

Mode 1, operator upgrade 0.57.0 → 0.58.0. The proxies rolled at 17:43:50Z; the client connected at 17:44:05Z:

LiteLLM: Error getting tools from server github_pr_review: Listing tools from MCP server 'github_pr_review' failed
mcp.shared.exceptions.MCPError: duplicate "initialize" received
LiteLLM: Error getting tools from server kubernetes_readonly: ...
mcp.shared.exceptions.MCPError: duplicate "initialize" received

Mode 2, proxy log after github-0 was replaced:

{"level":"WARN","msg":"container stdout closed, checking if container is still running"}
{"level":"WARN","msg":"container is still running after stdout EOF, attempting to re-attach"}
{"level":"INFO","msg":"attaching to pod","pod":"github-0","workload":"github"}
{"level":"ERROR","msg":"error attaching to workload, retrying","workload":"github","error":"unable to upgrade connection: container mcp not found in pod github-0_toolhive"}

After the proxy re-attached, the backend rejected calls with method "tools/call" is invalid during session initialization. Before v0.56.0 the proxy restarted itself when the backend went away, which avoided this.

Our workaround is a CronJob that every 5 minutes sends initialize → notifications/initialized → tools/list through each proxy. On duplicate "initialize" it deletes <name>-0, waits for it, then deletes the proxy pod. It works, but every operator upgrade still leaves a window of up to 5 minutes where these tools are missing.

Related: #1982 (initialize interception), #5890 (same duplicate-initialize error from vMCP health checks).

  • AI disclaimer - Claude Opus 5.5 provided the write up.

Activity

  1. added
    bug
    on Oct 28, 2026
  2. added
    needs-triage
    on Oct 28, 2026
  3. somaz94 commented on Nov 6, 2026

    @somaz94

    I'd like to work on this, starting with the backend restart case. When the proxy re-attaches to a replaced backend pod, it would drop its cached initialize response and send initialize to the new process again. The proxy restart case seems to need a design call (re-initializing an unknown backend versus restarting it), so I would leave that for a follow-up unless you have a preference. Could I be assigned?

  4. jhrozek commented on Nov 24, 2026

    @jhrozek
    Contributor

    Thanks for the detailed write-up, this matches what I see in the code. The initialize cache in the stdio proxy is set once and never cleared. Nothing resets it when the proxy re-attaches to a new backend pod (your mode 2). A freshly started proxy has no cache, so it forwards initialize to a backend that was already initialized (your mode 1). I haven't reproduced it on a cluster, but I don't see anything on main that handles either case.

    The backend-restart case looks like a contained fix. When the proxy re-attaches, it drops its cached handshake and re-initializes the new process. Mode 1 is harder, because the proxy can't tell whether the backend it attached to was already initialized. We need to decide between restarting the backend pod and some other recovery. @somaz94, you're welcome to take the mode 2 fix. Please keep it to that case and say in the PR that mode 1 is still open. One question for you, @reefland: with the existing workaround, does deleting only <name>-0 after a proxy restart recover things, or do you always need to bounce the proxy too?

  5. removed
    needs-triage
    on Nov 24, 2026
  6. somaz94 commented on Nov 25, 2026

    @somaz94

    Thanks @jhrozek for checking it against the code. I will keep the PR to mode 2, dropping the cached initialize when the proxy re-attaches to a replaced backend, and say in the description that mode 1 is still open. Could you assign the issue to me, as the contributing guide asks?

  7. reefland commented on Dec 1, 2026

    @reefland
    Author

    One question for you, @reefland: with the existing workaround, does deleting only <name>-0 after a proxy restart recover things, or do you always need to bounce the proxy too?

    Our automated workaround has always restarted both: it deletes <name>-0, waits for it to go, then deletes the proxy pod. So we have no clean data on deleting only the backend after a proxy restart.

    What we do have:

    • Mode 1 has hit us 4 times since mid-November (every operator bump rolls the proxies, most recently 0.71.0 → 0.72.0). Each time the Go SDK backend logs duplicate initialize request, and backend + proxy restart recovers it.
    • Deleting the backend pod makes the existing proxy log container is still running after stdout EOF, attempting to re-attach within the same process. Given the mode 2 bug, that re-attach keeps the cached initialize, so I'd expect deleting only <name>-0 to move us from mode 1 to mode 2 rather than recover.
    • Before we automated this (on v0.56.0), we deleted github-0 alone twice and it stayed broken with method "tools/call" is invalid during session initialization until the proxy pod was restarted too. I can't confirm from retained logs that this incident began with a proxy restart.

    So: in practice we always bounce the proxy too, and I believe backend-only would not recover on current releases. If it helps, I can run a controlled test (restart the proxy, then delete only <name>-0, then check tools/list) and report back.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions