Skip to content

fix(studio): carry mcp_tool_search schemas in the tool result to keep the prompt cache stable - #16539

Open
Shreesh-Sree wants to merge 5 commits into
unslothai:mainfrom
Shreesh-Sree:fix/studio-mcp-search-prompt-cache
Open

Shreesh-Sree wants to merge 5 commits into
unslothai:mainfrom
Shreesh-Sree:fix/studio-mcp-search-prompt-cache

Conversation

@Shreesh-Sree

@Shreesh-Sree Shreesh-Sree commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #16538.

What

When mcp_tool_search found tools beyond the up-front preload, the controller injected their full schemas into the declared tools array mid-turn. Chat templates render tools at the top of the prompt, so the next continuation diverged near the prompt's start and llama-server re-evaluated everything after it. On hybrid/recurrent models with no recurrent-state checkpoint at or before the divergence, that is a full re-prefill; and a discovered-but-unused tool fell out of the next turn's listing, so the invalidation repeated on every later search. (The 35s / 16.8x figures in the issue are the reporter's measurements; this PR changes the mechanism behind them.)

How

  • The search result now carries the full schema of each matched tool, within the existing _MCP_TOOL_LOAD_SHARE budget - same information, delivered in the appended tool result instead of the prompt head.
  • ToolLoopController no longer mutates the declared tools mid-turn: searched or directly-named MCP tools are admitted through its call gate from the catalog and coerced with their catalog schema, so the declaration (and llama-server's prefix cache) stays byte-identical across the turn.
  • Unchanged: tools a thread actually runs are still recorded and listed on its later turns, and calls are still typed by the full schema.

Same template-at-the-top cache mechanics the codebase already documents for the budget-exhausted final pass.

Tests

  • New regression test: repeated searches + calls leave active_tools() identical while every searched tool still executes. It fails on current main and passes with this change.
  • tests/test_mcp_servers.py: 137 passed; 7 remaining failures are pre-existing environment issues (Windows codec, installed-SDK dump), identical on main.
  • tests/test_llama_cpp_tool_loop.py (mcp/search subset): 15 passed; tests/test_safetensors_tool_loop.py (mcp/search subset): 16 passed; tests/test_permission_mode.py: 2431 passed; tests/test_llama_tool_parse_rejection_retry.py: 7 passed.
  • Not run: end-to-end GPU verification (no GPU in this environment); the fix is covered at controller and payload level.

Author: @Shreesh-Sree

I used AI as a pair programmer to help develop the code and tests, and I reviewed the changes.

…ation

mcp_tool_search injected the matched tool schemas into the declared
tools array mid-turn. The chat template renders tools at the top of the
prompt, so the next continuation diverged from the first tokens and
llama-server re-evaluated the whole conversation; on hybrid/recurrent
models without a checkpoint at the divergence that is a full prefill
(unslothai#16538), and a discovered-but-unused tool dropped from the next turn's
listing repeated the storm on every later search.

The search result now carries the full schemas it already budgets with
_MCP_TOOL_LOAD_SHARE, and the controller only opens its call gate for
the found catalog names, leaving the declared tools - and the cached
prompt prefix - untouched. Tools a thread runs are still recorded and
listed on its later turns.

Refs unslothai#16538
@Shreesh-Sree
Shreesh-Sree marked this pull request as draft November 3, 2026 11:34
danielhanchen added a commit that referenced this pull request Nov 3, 2026
Fixes #16538. Keep searched MCP schemas out of the local model’s cached
prompt declaration and return them in tool results. For llama.cpp,
supply discovered schemas to its decoding grammar separately from the
template’s tools. External providers retain normal tool declarations
because they cannot separate the two.

Search admits only complete schemas within the response budget, without
charging rejected matches against smaller tools. Deferred calls retain
the existing schema-identifier filtering and argument coercion.

Continues Shreesh-Sree’s #16539, preserving the original commits. That
PR and its source fork became unavailable during review. Includes fixes
for four Codex findings from [the review
mirror](wasimysaid#296), plus the
external-provider compatibility regression.

Live validation:
- Real ComfyMCP discovery: 23 tools plus the resource tool. Main expands
declarations from 5 to 7; the local prompt declaration stays at 5 after
the fix.
- Qwen3.5-0.8B/llama.cpp b11985 controlled replay: 1,616 cached tokens
versus 1,104 on main. This verifies the mechanism, not the reported 17×
timing or Windows hardware.
- Qwen3-8B-Q4_K_M/b11985: the original stable declaration repeats the
search; the separated grammar emits the discovered native call
(`mcp__demo__ping`, `n=7`). `/apply-template` confirms byte-identical
rendered prompts.

MCP/controller/external-loop tests: 317 passed. The updated llama-loop
regression and seven llama MCP tests pass. Pre-commit passes. Regression
cases fail on their respective baselines. The broader backend run was
interrupted after stalling with 415 tests passed; it is not a full-suite
pass.

[Final Codex
review](wasimysaid#296 (comment))
found no major issues at `d9570c4ffa`.

---------

Co-authored-by: Shreesh-Sree <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Daniel Han <[email protected]>

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Studio: Dynamic MCP loading invalidates prompt cache on hybrid/recurrent models (17x slowdown)

2 participants