Repository navigation
Description
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:
- Proxy restarts, backend keeps running. This happens on every operator upgrade, because the operator updates
TOOLHIVE_RUNNER_IMAGEand rolls all proxy Deployments while leaving the<name>-0pods alone. The new proxy forwards a freshinitializeto a backend that the old proxy already initialized, and the backend rejects it withduplicate "initialize" received. - Backend restarts, proxy keeps running. Since v0.56.0 the proxy re-attaches to the new
<name>-0pod in place ("container is still running after stdout EOF, attempting to re-attach"). It keeps its cachedinitializeresponse, so it never sendsinitializeto the new backend. The backend then rejects every request withmethod "tools/call" is invalid during session initialization.
Steps to reproduce
Mode 1 (proxy restart):
- Deploy a stdio
MCPServerbacked 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
- Connect a client and complete
initialize/tools/list. It works. - Restart only the proxy, either with
kubectl rollout restart deploy/githubor by upgrading the operator chart (we saw it going from 0.57.0 to 0.58.0). - Connect a new client and send
initialize. It fails withduplicate "initialize" received.
Mode 2 (backend restart):
- Same
MCPServer, already initialized by a client. kubectl delete pod github-0.- The proxy logs that it is re-attaching. Connect a client,
initialize(answered from the cache), thentools/call. It fails withmethod "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
initializefails until someone deletes<name>-0by hand. - Mode 2:
initializesucceeds 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
/mcpgateway → 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
somaz94 commentedon Nov 6, 2026
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?
jhrozek commentedon Nov 24, 2026
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>-0after a proxy restart recover things, or do you always need to bounce the proxy too?somaz94 commentedon Nov 25, 2026
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?
reefland commentedon Dec 1, 2026
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:
<name>-0to move us from mode 1 to mode 2 rather than recover.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.