Repository navigation
Description
Bug description
When a VirtualMCPServer runs with the optimizer enabled via embeddingServerRef (operator deployment, Serve path), the optimizer re-embeds the entire tool set on every client session registration (and on every cross-pod rehydration). The whole tools/list / initialize handshake is blocked on this synchronous, eager re-embed, which runs in the client's request context.
At a modest catalog size this is invisible, but as the aggregated tool count grows and multiple clients connect concurrently, the embedding backend queues, per-chunk requests cross the client timeout, and session builds fail — the MCP client then sees zero tools ("this connector has no tools available"). Because a failed build tears the session down, the client reconnects and triggers another full rebuild, which feeds the queue further.
This is already acknowledged in the code as a deferred optimization (referenced as #5445), with the note that it's "acceptable while the Serve path is test-only" — but the operator-deployed vMCP uses exactly this Serve path in production.
Environment
- ToolHive operator + vmcp image
v0.36.0(behavior confirmed unchanged in the source throughv0.39.0). - Kubernetes (GKE), operator-managed
VirtualMCPServerwithembeddingServerRef→ a TEIEmbeddingServer(CPU,BAAI/bge-small-en-v1.5). - 8 backends aggregated → ~140 tools. Single TEI replica.
Steps to reproduce
- Deploy an
EmbeddingServer(TEI, CPU) and aVirtualMCPServerwithembeddingServerRefset (optimizer auto-configured), aggregating enough backends to reach ~100+ tools. - Connect one or more MCP clients (e.g. Claude Desktop) and refresh the tool list a few times / open concurrent sessions.
- Watch the vMCP logs.
Actual behavior
- Every session logs a fresh
starting capability aggregation→ embedding phase →session capabilities injected from core, i.e. a full re-embed per session. - Under concurrency, embedding calls fail intermittently:
failed to list core tools for session … TEI request failed: context deadline exceeded (Client.Timeout exceeded while awaiting headers)— the TEI queue exceeds the per-chunk HTTP timeout (default 30s). We measuredqueue_timeon the TEI side climbing 1s → 6.8 → 14.2 → 21.7 → 27.8s whileinference_timestayed ~1s/chunk, i.e. queueing from concurrent redundant rebuilds, not raw capacity.failed to list core tools for session … context canceled— the client abandons theinitialize/tools/listrequest before the ~16s build completes (observed: a successfulsession capabilities injectedimmediately followed bysession terminated67 ms later).
- Each failure →
Manager.Terminate: session terminated→ client shows 0 tools → reconnect → new full rebuild (feedback loop).
Expected behavior
tools/listshould return the two static meta-tools (find_tool/call_tool) without blocking on the index build.- The optimizer index should not be rebuilt from scratch on every session when the tool set is unchanged.
Relevant source (v0.36.0)
pkg/vmcp/server/serve_optimizer.go— per-registration / per-rehydration re-upsert of all core tools; in-code comment: "repeated work … Acceptable while the Serve path is test-only … deferred optimization (tracked for P3.2 Reduce server.New body to the wrapper #5445)". The operator deploys this Serve path.pkg/vmcp/optimizer/internal/toolstore/sqlite_store.go—UpsertTools→EmbedBatchover all tools unconditionally (no skip-if-exists / no content hash); SQLite is in-memory (file:memdb?mode=memory&cache=shared), so nothing survives a restart and it is fully rewritten per session.pkg/vmcp/optimizer/internal/similarity/tei_client.go— sequential chunking onmax_client_batch_size; per-requesthttp.Client{Timeout}(default 30s); no retry, no concurrency limit.pkg/vmcp/server/serve.go— the session hooks pass the client request context into the build, so a client that gives up cancels the embed (context canceled).
Suggested direction
Any of these would resolve it (ordered by leverage):
- Return the static meta-tools immediately; build/refresh the index asynchronously (lazy, out of the client request context).
find_tool/call_toolare static —tools/listshould not depend on the embedding build finishing. - Skip the re-embed when the tool set is unchanged — the embedded text is exactly
"name: X description: Y"; a content hash per tool makes the per-session/per-rehydration rebuild a no-op in the common case. - Persist / pre-compute the index (survive restarts; re-embed only on catalog change) — the event-driven persistent-index model used by other discovery gateways (e.g. agentic-community/mcp-gateway-registry) is a good reference.
Workarounds in place (operator users)
More EmbeddingServer replicas, keeping TEI off preemptible nodes, and raising optimizer.embeddingServiceTimeout reduce the failure rate but don't remove the per-session rebuild — the intermittency scales back up with the tool count. (Note: raising TEI --max-client-batch-size does not help on the ONNX CPU backend, which forces max_batch_requests=8 internally.)
Measured (140 tools, before → after these workarounds): with a single 2-CPU TEI replica on a shared/contended node, concurrent client refreshes produced repeated deadline exceeded failures (TEI queue_time climbing to ~28s against the 30s timeout). After moving to 2 replicas / 3-CPU limit / pinned off preemptible / 120s timeout, deadline exceeded dropped to 0 over a 10-minute window and clients reliably receive the meta-tools. The context canceled case (client abandons tools/list while the eager build runs) still occurs intermittently — that one is only removed by decoupling tools/list from the index build (suggestion #1 above).
Activity
aponcedeleonch commentedon Jul 17, 2026
Thanks for the incredibly thorough writeup. The TEI
queue_timemeasurements and the before/after workaround data make this easy to reason about.I confirmed the mechanism on current main (v0.39.0 plus a few commits) and everything you cited holds: the Serve path rebuilds the per-session optimizer on every registration and every cross-pod rehydration,
tools/listis gated on that synchronous build with the client request context passed straight in, and the store is in-memory so nothing survives a restart. The "acceptable while the Serve path is test-only" comment is also stale, since the operator entrypoint delegates to exactly this Serve path.We are treating this as an enhancement rather than a bug. The current behavior was a deliberate deferral, and closing it properly means building capability that is not there yet. To do this right across a multi-pod vMCP deployment we would need:
"name: X description: Y", so a content hash works: on a match return the stored embedding, on a miss compute it and add the entry. That same hash tells us whether a tool is new, unchanged, or changed.On the suggestion of return the static meta-tools immediately, build the index async): we like the direction, but there is a wrinkle. Today there is no readiness guard on
find_tool. Its search filters onembedding IS NOT NULL, so if an LLM callsfind_toolduring the async build window it gets zero results (or keyword-only if rows exist but embeddings do not) with no error. The client would read that as "no tools found" and conclude the capability does not exist, which is arguably worse than a slowtools/list. So the suggestion needs a readiness signal too: gatefind_toolon an "index ready" state and return a clear "still indexing, retry shortly" while building, rather than silently returning empty.call_toolis not affected since it is an in-memory lookup that does not touch the index.I will check with the team internally on whether this is an enhancement we want to invest in. Would it be something you will be willing to pick up? More than happing to help with reviews. Either way, thanks a lot for the interest and for the detailed report.
TANTIOPE commentedon Jul 21, 2026
Hey, really glad the writeup was useful !
And thanks for the insight on the
find_toolreadiness gap. I'd be happy to help explore your suggestions.I'm a bit short on time right now and for the weeks to come though, so I can't promise anything on timing sadly.
But if I manage to get enough time to put together a nice PR, I'd gladly ping you for a review.
Have a good one!
TANTIOPE commentedon Jul 25, 2026
Hey @aponcedeleonch
Found some time some time sooner than expected. So #5996 is up for the per-pod caching part.
Feel free to take a look when you've got time. Thanks !
aponcedeleonch commentedon Jul 28, 2026
@TANTIOPE Amazing! Thanks so much for the contribution. Made some comments to the PR
Reuse tool embeddings across sessions
Reuse tool embeddings across sessions (stacklok#5996)