Repository navigation
Studio: keep the MCP tool list fixed per chat and add each message's matches as a search - #18529
Conversation
…matches as a search With a catalog too large to list in full, Studio declared mcp_tool_search plus tools ranked against the newest message, and replaced them whenever a message's best match was not listed. The tool block is the start of the prompt, so on hybrid and sliding-window models every such change made llama-server re-read the whole chat, often for messages that needed no tool at all. A thread now keeps the tools its first request listed. Tools ranked best for a later message that the listing leaves out are added after the message as an mcp_tool_search call and result, the way the knowledge-base auto-search adds its results, so the UI shows and replays it and the next turn reuses it from the cache. The tool loop accepts those tools, so the model calls them without a search round of its own, and tools the thread ran stay callable without their schemas being sent again. Requests without a thread, the search-only switch, and forced or image-mapped tools behave as before; a pinned tool joins the fixed list once and stays.
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 |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: af017c3df1
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
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".
|
|
||
| from core.inference.tools import mcp_match_exchange | ||
|
|
||
| _gguf_mcp_exchange = mcp_match_exchange(gguf_messages, tools_to_use) |
There was a problem hiding this comment.
Include automatic MCP exchanges in token counts
For a threaded MCP request whose latest user turn matches a tool outside the held header, completion now adds this exchange, but chat_count_tokens() still passes the original openai_messages and held _mcp_tools to _count_chat_context_tokens() without constructing the exchange. With no RAG scope, the existing pending-retrieval refusal does not apply, so the returned input_tokens and prompt breakdown omit the assistant call and schema result, potentially thousands of tokens. Mirror the exchange construction in the count path, or decline pending turns that would receive automatic MCP matches.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
The count only feeds the context meter (refresh-context-usage.ts), and once the turn runs, the search card is part of the history it counts. A preview that omits the pending exchange is not a correctness bug. Not changing it.
|
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". |
|
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". |
|
Codex Review: Didn't find any major issues. Already looking forward to the next diff. 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: Didn't find any major issues. Can't wait for the next one! 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: Didn't find any major issues. Delightful! 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 |
1 similar comment
|
@codex review |
…hai#18624) Follow-up to unslothai#18529. ## Problem 1. Near a full context window, a later MCP lookup message can now come back as an empty reply. unslothai#18529 adds the message's matching tools after it as an `mcp_tool_search` exchange. That exchange sits next to the message, so truncation cannot remove it, and on a near-full window it takes the room the reply needed. Before unslothai#18529 the same turn either answered or showed a clear "Message too long" error. 2. Each request checks the MCP tools remembered from earlier searches one at a time at the start of the tool loop, and each check rebuilds the whole tool catalog. Since unslothai#18529 every search card adds up to 5 names, so this grows over a chat: 200 remembered names against a 346-tool catalog took about 400 ms before the first model call. ## Change - The match search gets at most the room left after the tool listing, the message, a reply reserve (the larger of 1024 tokens and a quarter of the window, as the knowledge-base whole-document budget uses) and a 512-token margin, capped at its existing 15% share. When nothing fits, no search is added. The tools stay reachable through `mcp_tool_search`. - Remembered tools are checked with the bulk `_mcp_tool_specs` lookup, one catalog build for all of them. ## Results Studio from current main vs main + this PR. `unsloth/Qwen3-0.6B-GGUF` Q4_K_M on CPU, 4096 window, a 96-tool MCP server. Second message = filler text + a document lookup: | second message | main | this PR | |---|---|---| | short lookup | `mcp_tool_search` card added | `mcp_tool_search` card added | | ~4,000 tokens (filler x186) | empty reply | answers, calls `list_user` | | ~4,140 tokens (filler x190) | empty reply after 7.9 s | "Message too long: 4142 tokens exceeds the 4096-token context window" | Controller setup with 200 remembered names and a 346-tool catalog: 398 ms on main, 6 ms with this PR. ## Tests - `test_a_message_that_fills_the_window_gets_no_search_after_it`: fails on main (the exchange is added), passes here. - `test_controller_checks_remembered_tools_with_one_catalog_build`: fails on main (6 catalog builds), passes here. - MCP, llama.cpp and safetensors tool-loop, controller, external tool result and admission suites pass.
Closes #17015. Also reported on Reddit, still present after #17326.
Problem
When the MCP tools are too large to list in full, Studio lists
mcp_tool_searchplus up to 5 tools ranked against the newest message, and replaces them when a message's best match is not listed. #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
mcp_tool_searchcall 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.Results
Qwen3.6-27B Q6_K, 64K context, 8-message chats with 6 tool lookups:
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.
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
UNSLOTH_STUDIO_MCP_SEARCH_ONLY=1, and catalogs that fit the window.tool_choice: none, a forced tool, or a resumed reply.Tests
Before / after (Studio, merge base vs this PR)
Two clean Studio installs (merge base
4d93c834c, head4faae01ff), one fastmcp HTTP server with 96 tools,unsloth/Qwen3-0.6B-GGUFQ4_K_M on CPU at a 4096 window. On the document lookup, the PR adds anmcp_tool_searchcard before the model's ownget_documentcall; the base build shows only the call. First replies differ because the model samples.