Skip to content

Studio: list large MCP tool schemas compactly and load the full schema on demand - #11046

Merged
danielhanchen merged 19 commits into
unslothai:mainfrom
NilayYadav:mcp-lazy-tools
Sep 29, 2026
Merged

danielhanchen merged 19 commits into
unslothai:mainfrom
NilayYadav:mcp-lazy-tools

Conversation

@NilayYadav

@NilayYadav NilayYadav commented Sep 15, 2026 •

Copy link
Copy Markdown
Collaborator

Some MCP servers expose tools with very large parameter schemas. Notion's 44-tool catalog adds about 65,000 tokens to every request, more than the whole context window of many local models, so a one word prompt is refused before the model sees it. This is the problem reported in #10997.

What changes

  • The full MCP listing is sent exactly as before whenever it fits in three quarters of the loaded local model's context window. External providers always get the full listing.
  • When it does not fit, the largest tools (only those whose description and schema together exceed about 1,500 characters) are listed compactly, largest first, until the listing fits: first sentence of the description plus top-level parameters with types, required flags and short enums. Every other tool keeps its full schema, including nested and union parameters.
  • When any tool was compacted, the model also gets mcp_tool_schema, which returns a tool's full description and JSON Schema on demand, paged when it is longer than the room left for a tool result.
  • A compacted tool called without a required argument, or whose call the server rejects, answers with that tool's full schema so the model can correct the call. Arguments to a compacted tool are still typed against its full schema.
  • The token estimate used when no tokenizer is loaded is computed in C instead of a per-character Python loop (identical values), so pricing a large listing costs about 2 ms per request instead of about 8 ms.

Accuracy (Notion catalog, 29 single-action tasks, Q4_K_M, temperature 0)

Harness: a stdio stand-in serving Notion's real catalog that validates every call against the tool's real inputSchema. main cannot serve these requests at 64K, so it is measured at 100K as the full-listing reference.

model listing prompt tokens first call correct correct call anywhere in the turn
Qwen3-4B-Instruct-2507 main, full, 100K 65,004 69% 72%
Qwen3-4B-Instruct-2507 every large tool compacted, 64K 9,365 41% 55%
Qwen3-4B-Instruct-2507 this PR, largest first until it fits, 64K 43,943 69% 79%
Qwen3-30B-A3B-Instruct-2507 main, full, 100K 65,004 55% 86%
Qwen3-30B-A3B-Instruct-2507 every large tool compacted, 64K 9,365 41% 69%
Qwen3-30B-A3B-Instruct-2507 this PR, largest first until it fits, 64K 43,943 55% 90%

At 64K on Notion, 5 of 44 tools are compacted when no tokenizer is loaded, and fewer when the loaded GGUF measures the listing exactly. On a 32K window 11 are compacted.

Tests

studio/backend/tests/test_mcp_servers.py covers the full listing when it fits, compacting only the largest tools needed, the compact form, the summary, when the schema tool is offered, the missing argument and rejected call paths, argument typing by the full schema, and the schema tool itself (138 passed).

Before / after in the chat UI

Notion's catalog connected through a local stdio stand-in, Qwen3-4B-Instruct-2507 Q4_K_M loaded at a 65,536-token window, same prompt on both sides. Before (merge base 7617fc3): the request is refused with "Message too long: 194720 tokens exceeds the 65536-token context window". After (2712645): the model calls Notion's keyword search and replies, with the context meter at 44.5k of 65.5k.

Before: refused for exceeding the context window. After: the Notion search tool is called and the model replies.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@danielhanchen

Copy link
Copy Markdown
Member

This rewrites the description and parameter schema that large MCP tools present to the model, which changes what the model reads on every turn, and drops enums and nested properties from the listing. Could you confirm tool-call accuracy on a compacted server such as Notion against the current full listing before I review?

@NilayYadav

Copy link
Copy Markdown
Collaborator Author

I measured it, and the compacted listing does cost tool-call accuracy on Notion.

Setup: upstream main 38d4a48 (full listing) against this PR at be846ee (compacted listing), same install, same A100, 100K context, so the full 65K-token listing fits on both sides.

  • Models: Qwen3-30B-A3B-Instruct-2507 and Qwen3-4B-Instruct-2507 (Q4_K_M), temperature 0, fixed seed.
  • MCP server: Notion's real 44-tool catalog from a stdio stand-in that records every call and validates arguments against each tool's real inputSchema, returning an error on invalid calls.
  • Tasks: 29 single-action requests with every id and value given, 24 on compacted tools and 5 on tools listed in full. Each has a known correct call that passes its checks.
model listing prompt tokens first call correct correct call anywhere in the turn mcp_tool_schema calls invalid calls rejected
Qwen3-30B-A3B full 64,916 55% 83% n/a 0
Qwen3-30B-A3B compacted 9,409 45% 45% 0 30
Qwen3-4B full 64,960 72% 76% n/a 11
Qwen3-4B compacted 9,365 38% 38% 0 26

A correct call has the right tool, arguments valid against the full schema, and the requested values. On the five tools that are not compacted, the first call was correct for 4/5 with the full listing and 5/5 with the compacted one, for both models.

What goes wrong: the losses are the nested and union parameters the compact listing drops.

  • filters.created_date_range gets sent as filters.created.
  • position, new_parent and data get the wrong union branch.
  • filter.filters[].filter.operator is left out.
  • Page properties arrive in the wrong shape.

Neither model ever called mcp_tool_schema, and neither recovered after its call was rejected with the validation errors. With the full listing, 30B often fetches first and then acts, which is the gap between its 55% and 83%. Single greedy sample per task, so read the per-task rows over the percentages.

Per-task results, the harness, tasks and scorer: https://github.com/NilayYadav/unsloth-staging/tree/evidence-pr11046/evidence/pr-11046/accuracy

What this means for the PR: compacting unconditionally would make Notion worse for anyone whose context window already fits the full listing, so I'm changing the approach before asking for review:

  1. Tools are listed in full whenever the catalog fits the loaded context window. Compacting only happens when the full listing would otherwise get the request refused, as in [Feature] MCP tools lazy loading #10997.
  2. When a compacted tool's call is rejected by the server, the error comes back with that tool's full schema. The missing-argument case already does this, so the model can correct the call without having to think of mcp_tool_schema.

I'll rerun the same 29 tasks against that change, including the compacted path, and post the numbers here.

@NilayYadav

Copy link
Copy Markdown
Collaborator Author

Follow-up to the numbers above. Pushed in ab222ac:

  1. The full listing is kept whenever it fits. MCP tools are compacted only when the full listing would take more than 75% of the loaded local model's context window. External providers always get the full listing.
  2. When a compacted tool's call is rejected by the server or is missing a required argument, the reply carries that tool's full schema, so the model can correct the call without having to reach for mcp_tool_schema.

Same harness, tasks and Notion catalog as before, run on ab222ac:

100K context: the listing stays full. count_tokens is 64,960 tokens with Qwen3-4B, identical to main. What the model reads is unchanged, so the earlier full-listing numbers (83% for 30B-A3B, 76% for 4B) apply.

64K context (65,536 tokens): main's listing alone is 64,960 of those tokens, so it cannot serve these requests. This PR compacts instead:

model listing prompt tokens first call correct correct call anywhere in the turn recovered after a rejected call
Qwen3-30B-A3B compacted, schema on rejection (64K) 9,365 41% 72% 9 tasks
Qwen3-4B compacted, schema on rejection (64K) 9,321 38% 48% 3 tasks
Qwen3-30B-A3B compacted, previous commit (100K) 9,409 45% 45% 0
Qwen3-4B compacted, previous commit (100K) 9,365 38% 38% 0
Qwen3-30B-A3B full, main (100K) 64,916 55% 83% n/a
Qwen3-4B full, main (100K) 64,960 72% 76% n/a

So the compacted path is still below the full listing, but it is only used when the full listing cannot be served at all. Returning the schema on a rejected call recovered most of the gap for 30B-A3B and some of it for 4B. Controls on uncompacted tools were 5/5 for both models. One greedy sample per task, as before.

Per-task results: https://github.com/NilayYadav/unsloth-staging/tree/evidence-pr11046/evidence/pr-11046/accuracy

# Conflicts:
#	studio/MCP.md
#	tests/studio/playwright_queue_localization.py
@danielhanchen

Copy link
Copy Markdown
Member

Thanks for the accuracy numbers and for limiting compaction to catalogs that would not otherwise fit. On the compacted path, Qwen3-4B still gets 48% of calls right against 76% with the full listing, so could you check whether keeping nested and union parameters for the most-used tools closes some of that gap before I review?

@danielhanchen danielhanchen self-assigned this Sep 29, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 29, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 29, 2026
@unslothai unslothai deleted a comment from NilayYadav Sep 29, 2026
@unslothai unslothai deleted a comment from NilayYadav Sep 29, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 29, 2026
@unslothai unslothai deleted a comment from NilayYadav Sep 29, 2026
@unslothai unslothai deleted a comment from NilayYadav Sep 29, 2026
@unslothai unslothai deleted a comment from NilayYadav Sep 29, 2026
@unslothai unslothai deleted a comment from NilayYadav Sep 29, 2026
@unslothai unslothai deleted a comment from NilayYadav Sep 29, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 29, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 29, 2026
danielhanchen pushed a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Sep 29, 2026
shimmyshimmer pushed a commit to shimmyshimmer/unsloth-staging-4 that referenced this pull request Sep 29, 2026
@danielhanchen
danielhanchen merged commit fb5a921 into unslothai:main Sep 29, 2026
46 of 49 checks passed
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