Skip to content

Studio: keep the MCP tool list fixed per chat and add each message's matches as a search - #18529

Merged
danielhanchen merged 12 commits into
unslothai:mainfrom
oobabooga:studio-mcp-tail-listing
Nov 20, 2026
Merged

danielhanchen merged 12 commits into
unslothai:mainfrom
oobabooga:studio-mcp-tail-listing

Conversation

@oobabooga

@oobabooga oobabooga commented Nov 19, 2026 •

Copy link
Copy Markdown
Member

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_search plus 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

  • 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.

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.

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.

Before / after (Studio, merge base vs this PR)

Second message of an MCP chat with a 96-tool catalog. Before: one get_document call. After: an mcp_tool_search call added by Studio, then the same get_document call.

Two clean Studio installs (merge base 4d93c834c, head 4faae01ff), one fastmcp HTTP server with 96 tools, unsloth/Qwen3-0.6B-GGUF Q4_K_M on CPU at a 4096 window. On the document lookup, the PR adds an mcp_tool_search card before the model's own get_document call; the base build shows only the call. First replies differ because the model samples.

…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.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 19, 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-20T02:36:55.593848Z 1a5b93f 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[bot]

This comment was marked as resolved.

@oobabooga

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: ebc5202320

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

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 19, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 19, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 19, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 19, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 19, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: ab122d2ee2

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

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Already looking forward to the next diff.

Reviewed commit: 397cb7fd1d

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

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Can't wait for the next one!

Reviewed commit: 20b05ce8d5

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

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Delightful!

Reviewed commit: 1a5b93f1c8

ℹ️ 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 b2079e5 into unslothai:main Nov 20, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 20, 2026
@unslothai unslothai deleted a comment from oobabooga Nov 20, 2026
@unslothai unslothai deleted a comment from oobabooga Nov 20, 2026
@unslothai unslothai deleted a comment from oobabooga Nov 20, 2026
@unslothai unslothai deleted a comment from oobabooga Nov 20, 2026
@unslothai unslothai deleted a comment from oobabooga Nov 20, 2026
@unslothai unslothai deleted a comment from oobabooga Nov 20, 2026
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

danielhanchen pushed a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Nov 20, 2026
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

danielhanchen added a commit to Imagineer99/unsloth that referenced this pull request Nov 20, 2026
danielhanchen added a commit to Imagineer99/unsloth that referenced this pull request Nov 20, 2026
bestoffersdirect-fork-sync Bot pushed a commit to BestOffersDirect/unsloth that referenced this pull request Nov 20, 2026
…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.
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.

Studio: repeated full prompt processing across turns with Qwen hybrid model after tool use

2 participants