Skip to content

Studio: keep deferred MCP tools in place when a listed tool runs again - #17326

Merged
danielhanchen merged 5 commits into
unslothai:mainfrom
oobabooga:studio-mcp-listing-stable-order
Nov 10, 2026
Merged

danielhanchen merged 5 commits into
unslothai:mainfrom
oobabooga:studio-mcp-listing-stable-order

Conversation

@oobabooga

@oobabooga oobabooga commented Nov 9, 2026 •

Copy link
Copy Markdown
Member

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:

main
Turn 6: mcp_tool_search, git_diff, read_file, run_tests, write_file
Turn 7: mcp_tool_search, run_tests, git_diff, read_file, write_file   <- run_tests moved

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.

this PR
Turn 6: mcp_tool_search, read_file, write_file, run_tests, git_diff
Turn 7: mcp_tool_search, read_file, write_file, run_tests, git_diff

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

  • Two new tests, one for the order and one for token counting. The existing MCP, tool-loop and token-count tests pass.
  • A live test in Studio with Qwen3.8-27B, 350 MCP tools, and the same 12-message coding chat on main and on this PR. It ran before the token-counting change, which this chat doesn't use.
Turns 2 to 12 main this PR
Full reprocesses 11 of 11 2 of 11
Tokens processed 50,889 9,219
Time spent processing the prompt 25.8 s 6.9 s

The 2 remaining reprocesses happened when a message needed a tool that wasn't listed yet.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 9, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-10T12:42:42.446237Z 98f1fc8 Manual request
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🎉

Reviewed commit: 7075aa6253

ℹ️ 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".

@danielhanchen

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep them coming!

Reviewed commit: 98f1fc8061

ℹ️ 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".

@danielhanchen
danielhanchen merged commit f12cd4b into unslothai:main Nov 10, 2026
danielhanchen added a commit to Imagineer99/unsloth that referenced this pull request Nov 20, 2026
…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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants