Repository navigation
Studio: keep deferred MCP tools in place when a listed tool runs again - #17326
Conversation
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Codex Review: Didn't find any major issues. 🎉 Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. Keep them coming! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
…matches as a search (unslothai#18529) Closes unslothai#17015. Also reported on [Reddit](https://www.reddit.com/r/unsloth/comments/1xr5e7o/prompt_processing_is_triggered_almost_every_prompt/), still present after unslothai#17326. ## Problem When the MCP tools are too large to list in full, Studio lists `mcp_tool_search` plus up to 5 tools ranked against the newest message, and replaces them when a message's best match is not listed. unslothai#17326 fixed their order, but which tools are listed still changes with almost every topic, even for messages that need no tool ("What is 3+4?"). The tool list is at the start of the prompt, so any change there makes llama-server process the whole conversation again. Hybrid models (Qwen3.5 and later) and sliding-window models (Gemma) also lose the system prompt before it, so they process the entire prompt. ## Change - A chat keeps the MCP tools its first request listed. - On later messages, the tools that match the message and are not listed are added after it as an `mcp_tool_search` call and result, the same way the knowledge-base auto-search works. The UI shows it as a search card and keeps it in history. The model can call those tools directly. - Tools the chat ran, and tools an earlier search in the chat returned, stay callable. - The tool list keeps the existing preload share of the context (33%), and the added search has its own 15%, so a list that fills its share still leaves room for a later topic's tools. The list is fitted again only when it no longer fits, for example after a reload with a smaller context or a schema update. ## Results Qwen3.6-27B Q6_K, 64K context, 8-message chats with 6 tool lookups: | MCP tools | main: full re-processes | main: tokens processed | this PR: full re-processes | this PR: tokens processed | |---|---:|---:|---:|---:| | 4 servers x 24 tools | 6 of 7 | 28,846 | 0 | 4,787 | | One 346-tool server | 6 of 7 | 38,804 | 0 | 4,898 | Prompt processing time for the first chat went from 17.1 s to 7.0 s. With the 4 servers, the model called the right tool first on all 6 lookups (main: 5 of 6), for both Qwen3.6-27B and Qwen3-4B. Ranked tools are still sent with each message because small models rarely search: with a fixed list alone, Qwen3-4B found 0 of 6 tools. <img width="1620" height="474" alt="The document lookup turn. Before: one get_document_title card. After: an mcp_tool_search card added by Studio, then the same get_document_title call." src="https://raw.githubusercontent.com/oobabooga/unsloth/pr-media/pr/mcp-fixed-listing/mcp_lookup_turn-3d7b53823795.png" /> A document lookup on main and on this PR. Studio adds the matching tools as a search card, and the model calls the same tool directly on both sides. ## Notes - Unchanged: requests without a thread id (API clients), `UNSLOTH_STUDIO_MCP_SEARCH_ONLY=1`, and catalogs that fit the window. - The search is not added for `tool_choice: none`, a forced tool, or a resumed reply. - The search cards stay in the chat, so the prompt grows by up to 5 schemas per message. ## Tests - New tests, all failing on main: incidental wording keeps the tool block byte-identical; a later lookup adds the search after the message; the tool loop accepts the matched tools; the GGUF and safetensors loops add and skip the search correctly. - Listing tests that described the old replacement now describe the fixed list. - Further tests cover a smaller context, a schema that grows, large schemas that fill the list's share, a resumed reply, a "yes" after a clarifying question, and a tool the chat ran staying callable after several topic changes. - MCP, tool-loop, permission, passthrough, compaction, skills and token-count suites pass. --------- Co-authored-by: Daniel Han <[email protected]>
When many MCP tools are enabled, GGUF chats reprocess the whole prompt on almost every message, which makes each reply much slower. This was reported on Reddit and in #17015.
Why it happens
When the MCP tools would take more than 10% of the context, Studio puts only a few of them at the top of the prompt. It rebuilds that short list on every message, and the tool that ran last always goes first. So the order keeps changing even when the tools are the same:
The tool list comes before the chat in the prompt. When it changes, llama-server can't reuse its cache and processes the whole chat again.
Fix
Tools that are already in the list now keep their place, and new tools are added at the end. Which tools are listed does not change, only their order.
Token counting used to update the saved list too, so counting a different branch of a chat could still change the order. Counting now only reads it.
The list can still change when a message needs a tool that isn't listed yet, and that still costs one full reprocess. Avoiding it would mean making the model search for every new tool, which works worse with small models (#10997), so it is not part of this PR.
Testing
The 2 remaining reprocesses happened when a message needed a tool that wasn't listed yet.