Skip to content

vMCP optimizer re-embeds the full tool set on every session (Serve path) — unreliable at scale, tools/list blocks on the rebuild #5847

Description

@TANTIOPE

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 through v0.39.0).
  • Kubernetes (GKE), operator-managed VirtualMCPServer with embeddingServerRef → a TEI EmbeddingServer (CPU, BAAI/bge-small-en-v1.5).
  • 8 backends aggregated → ~140 tools. Single TEI replica.

Steps to reproduce

  1. Deploy an EmbeddingServer (TEI, CPU) and a VirtualMCPServer with embeddingServerRef set (optimizer auto-configured), aggregating enough backends to reach ~100+ tools.
  2. Connect one or more MCP clients (e.g. Claude Desktop) and refresh the tool list a few times / open concurrent sessions.
  3. 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 measured queue_time on the TEI side climbing 1s → 6.8 → 14.2 → 21.7 → 27.8s while inference_time stayed ~1s/chunk, i.e. queueing from concurrent redundant rebuilds, not raw capacity.
    • failed to list core tools for session … context canceled — the client abandons the initialize/tools/list request before the ~16s build completes (observed: a successful session capabilities injected immediately followed by session terminated 67 ms later).
  • Each failure → Manager.Terminate: session terminated → client shows 0 tools → reconnect → new full rebuild (feedback loop).

Expected behavior

  • tools/list should 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 → EmbedBatch over 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 on max_client_batch_size; per-request http.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):

  1. Return the static meta-tools immediately; build/refresh the index asynchronously (lazy, out of the client request context). find_tool/call_tool are static — tools/list should not depend on the embedding build finishing.
  2. 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.
  3. 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

  1. added
    needs-triage
    on Jul 17, 2026
  2. added
    vmcp
    enhancement
    scalability
    applied-ai
    p1
    and removed
    needs-triage
    on Jul 17, 2026
  3. aponcedeleonch commented on Jul 17, 2026

    @aponcedeleonch
    Member

    Thanks for the incredibly thorough writeup. The TEI queue_time measurements 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/list is 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:

    • A centralized embedding store shared across pods, so embeddings are computed once instead of per request or per pod.
    • A lookup keyed on tool content. The embedded text is just "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.
    • Staleness handling, since a persistent store means entries drift when a tool's name or description changes, and entries for tools that no longer exist need to be evicted against the current catalog.

    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 on embedding IS NOT NULL, so if an LLM calls find_tool during 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 slow tools/list. So the suggestion needs a readiness signal too: gate find_tool on an "index ready" state and return a clear "still indexing, retry shortly" while building, rather than silently returning empty. call_tool is 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.

  4. TANTIOPE commented on Jul 21, 2026

    @TANTIOPE
    ContributorAuthor

    Hey, really glad the writeup was useful !

    And thanks for the insight on the find_tool readiness 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!

  5. TANTIOPE commented on Jul 25, 2026

    @TANTIOPE
    ContributorAuthor

    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 !

  6. aponcedeleonch commented on Jul 28, 2026

    @aponcedeleonch
    Member

    @TANTIOPE Amazing! Thanks so much for the contribution. Made some comments to the PR

  7. added a commit that references this issue on Aug 9, 2026
    7539691
  8. added a commit that references this issue on Aug 19, 2026
    fe39aec
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

applied-aienhancementp1performancescalabilityvmcp

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions