Skip to content

Studio: add search-only MCP listings for cache-sensitive local chats - #18142

Merged
shimmyshimmer merged 16 commits into
unslothai:mainfrom
0xalkinun:codex/mcp-search-only
Nov 19, 2026
Merged

shimmyshimmer merged 16 commits into
unslothai:mainfrom
0xalkinun:codex/mcp-search-only

Conversation

@0xalkinun

Copy link
Copy Markdown
Contributor

What changes

Long local chats with an oversized MCP catalog can lose their cached prefix when a follow-up mentions a new topic. Studio can then spend seconds reprocessing the conversation before it answers.

Set UNSLOTH_STUDIO_MCP_SEARCH_ONLY=1 to keep the up-front MCP listing stable across changes in wording and past tool use. The model gets mcp_tool_search, any explicitly forced tool, and the tools mapped to an attached image. The switch applies after Studio decides the full catalog is too large. Ranked preloading remains the default because smaller models may struggle to discover tools on their own.

Refs #17015. Earlier controlled reproduction.

Verification

I replayed a fixed synthetic conversation through Studio's GGUF tool selector and a dedicated llama-server slot on an RTX 3090, with Qwen3.8-27B Q4_K_M, a 65,536-token context and 96 MCP tools. Each pair started with an empty slot. The follow-up mentioned a document title without asking for a lookup.

Follow-up measurement Default Search only
Prompt tokens processed, each of 3 runs 6,015 35
Cached tokens reused, each of 3 runs 0 5,475
Median prompt processing time 4.04 s 0.19 s

For a separate request that explicitly asked for a lookup, both modes fetched the correct title from a loopback MCP server. The default called a library tool directly. Search-only made two catalog searches before calling the library tool. The timing above covers prompt processing on the incidental follow-up.

The new regression test fails on the base revision. All 170 MCP server tests and 14 related GGUF and safetensors tool-loop tests pass on the change. Coverage includes a catalog that fits the window, an explicit tool choice, a mapped image, and the next turn after a tool executes.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 17, 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-18T21:02:19.050767Z 1059a7d 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: 770b8f3c39

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

@oobabooga oobabooga self-assigned this Nov 17, 2026
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Chef's kiss.

Reviewed commit: 842146ffda

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

@LeoBorcherding LeoBorcherding left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Tested at 842146f on a MacBook Pro M5 (macOS 26, llama.cpp Metal), gemma-4-12b Q4_K_XL at 16K context, a 96-tool stdio MCP server. Same branch with the flag off vs on.

The cache win reproduces. On a follow-up that names new topics, the default reprocessed 484 tokens (4.9 s) because the preloaded tools changed; search-only reprocessed 44 (1.4 s). One run each.

Follow-up turn Flag off (default) UNSLOTH_STUDIO_MCP_SEARCH_ONLY=1
Tokens reused from cache 3,511 of 3,995 3,510 of 3,554
Tokens reprocessed 484 44
Prompt eval time 4.9 s 1.4 s

The tradeoff the docs mention showed up on a mid-size model. Asked for the title of doc-42, the default called get_document_title directly and answered. With the flag on, gemma-4-12b ran mcp_tool_search 8+ times and never called the loaded tool, so I stopped it. Since the flag is opt-in and the default is unchanged, I don't think this blocks the PR, but the MCP.md note could name what it was verified on (Qwen3.8-27B in your run) and that gemma-4-12b looped, so "smaller models" doesn't read as only tiny ones.

Code: the early return keeps the search tool and pinned tools and skips the held-tool bookkeeping, which is right for this mode. 170/170 MCP server tests pass locally, and the new stability test fails on main's tools.py.

CI: the one red shard (m-r) is two NPU tests in test_multi_model_serving.py that also fail on the main commit this branch merged (ad003cd), fixed on main by #18197; nothing in this diff touches them.

Screenshots

Lookup, flag off: one direct call, correct answer.

default lookup

Lookup, flag on: gemma-4-12b keeps searching.

search-only lookup loop

Follow-up turn, flag off / flag on (cache numbers above come from these two chats' llama-server logs).

default follow-up

search-only follow-up

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. More of your lovely PRs please.

Reviewed commit: 270075d1f4

ℹ️ 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. More of your lovely PRs please.

Reviewed commit: 58d8c7f88a

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

Reviewed commit: e84f99c1b9

ℹ️ 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 added a commit that referenced this pull request Nov 18, 2026
#18324)

## Multi-LoRA packing: load the num_proc policy without importing
unsloth


`studio/backend/tests/test_dataset_map_num_proc.py::test_the_batched_packing_pool_asks_the_same_veto`
fails in `(Python 3.13, a-f)` on every PR since #17701, for example
#18015, #18108, #18142 and #18303:

```
core/training/multi_lora_trainer.py:686: in _pack_num_proc
    from unsloth_zoo.dataset_num_proc import get_dataset_num_proc
E   ModuleNotFoundError: No module named 'unsloth_zoo'
core/training/multi_lora_trainer.py:688: in _pack_num_proc
    from unsloth.dataset_num_proc import get_dataset_num_proc
unsloth/_gpu_init.py:241: in <module>
E   ImportError: Unsloth: Please install unsloth_zoo via `pip install unsloth_zoo` then retry!
```

### Cause

`_pack_num_proc`, the multi-LoRA packing pool's worker count from
#15733, looks up the shared `dataset_num_proc` policy in two steps:

1. it imports `unsloth_zoo`;
2. if that fails, it falls back to `from unsloth.dataset_num_proc import
...`.

That fallback runs the whole `unsloth` package `__init__`, which needs
`unsloth_zoo` anyway and patches torch on the way.

The rest of the backend already avoids this.
`utils.hardware.hardware._shared_policy()` tries `unsloth_zoo` first and
otherwise loads the stdlib-only `dataset_num_proc.py` straight off disk,
precisely so that no caller runs `import unsloth`.

#17701 added the first test that reaches `_pack_num_proc`, and the
backend shard runs without `unsloth_zoo`, so the fallback fails there.

### Fix

`_pack_num_proc` now gets the policy from `_shared_policy()`, the same
loader the single-job tokenize pool uses.

- **With a policy**, which is every real install: the call is unchanged,
`get_dataset_num_proc(count)` on the same module.
- **With no policy at all:** it keeps that function's contract, so a
serial count of 1 still means `None`.

### Verification

- Faithful repro of the CI state: `core.training.trainer` already
imported, then `unsloth_zoo` made unimportable, then `_pack_num_proc`
called with a tight host-memory snapshot.
  - `main`: `ModuleNotFoundError: No module named 'unsloth_zoo'`.
  - This PR: returns `None`, as the test expects.
- With `unsloth_zoo` installed: `tests/test_dataset_map_num_proc.py` and
the multi-LoRA tests pass (88 passed).
- ruff is clean.
…h_zoo (main regression blocking this PR's CI)
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. You're on a roll.

Reviewed commit: c162906fa8

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

@LeoBorcherding

Copy link
Copy Markdown
Collaborator

Pushed two small follow-ups to this branch, neither worth blocking the merge on: 4df2358, on top of c162906.

  • tools.py: a one-line comment at the search-only early return saying why it exists, to match the other cache comments in _mcp_listing.
  • MCP.md: says only the value 1 turns the flag on (true silently does nothing) and that it's env-only, with no toggle in Settings.

No behavior change. test_mcp_servers.py passes 170/170 with it, and Codex found no issues on the same tree (LeoBorcherding#966).

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. More of your lovely PRs please.

Reviewed commit: 88b2a8e274

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

# Conflicts:
#	tests/studio/test_model_picker_contracts.py
@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. Swish!

Reviewed commit: 8b8b1b9630

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

# Conflicts:
#	studio/backend/tests/test_dataset_map_num_proc.py
@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. You're on a roll.

Reviewed commit: 1059a7d7ce

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

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.

5 participants