Repository navigation
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
Open
Shreesh-Sree wants to merge 5 commits into
Shreesh-Sree wants to merge 5 commits into
Conversation
…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
marked this pull request as draft
November 3, 2026 11:34
Shreesh-Sree
marked this pull request as ready for review
November 3, 2026 11:42
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
Same template-at-the-top cache mechanics the codebase already documents for the budget-exhausted final pass.
Tests
Author: @Shreesh-Sree
I used AI as a pair programmer to help develop the code and tests, and I reviewed the changes.