codex icon indicating copy to clipboard operation
codex copied to clipboard

Add mcp search tool, lazy mcp load, reduce mcp tool occupying a lot of context

Open saidelike opened this issue 10 months ago • 8 comments

What feature would you like to see?

When MCP tool descriptions exceed 10% of the context window, they are automatically deferred and discovered via the MCPSearch tool instead of being loaded upfront. This reduces context usage for users with many MCP tools configured.

References:

  • high level description: https://x.com/i/status/2011523109871108570
  • tool search specification: https://platform.claude.com/docs/en/agents-and-tools/tool-use/tool-search-tool
  • original Claude Code issue: https://github.com/anthropics/claude-code/issues/7336

Related issues:

  • https://github.com/openai/codex/issues/2335

Additional information

No response

saidelike avatar Jan 15 '26 09:01 saidelike

I implemented optional / lazy loading of MCP servers in my fork, xCodex (xcodex), and shipped it in v0.3.5.

xCodex: https://github.com/Eriz1818/xCodex Release notes: https://github.com/Eriz1818/xCodex/blob/main/docs/xcodex/releases/0.3.5.md Docs: https://github.com/Eriz1818/xCodex/blob/main/docs/xcodex/lazy-mcp-loading.md

What it adds:

  • Global + per-server MCP “startup_mode”: eager (default) | lazy | manual
    • lazy: don’t start servers at session start; start on first tool/resource use
    • manual: never auto-start; load explicitly
  • In-session command: /mcp load <name> (plus /mcp status + /mcp retry ...)
  • CLI one-run override: xcodex --mcp-startup-mode lazy (or eager/manual)

This keeps startup fast for sessions that don’t use MCP, while still making it easy to opt-in when you do. AND saving on context!

Eriz1818 avatar Feb 04 '26 07:02 Eriz1818

I implemented optional / lazy loading of MCP servers in my fork, xCodex (xcodex), and shipped it in v0.3.5.

xCodex: https://github.com/Eriz1818/xCodex Release notes: https://github.com/Eriz1818/xCodex/blob/main/docs/xcodex/releases/0.3.5.md Docs: https://github.com/Eriz1818/xCodex/blob/main/docs/xcodex/lazy-mcp-loading.md

What it adds:

* Global + per-server MCP “startup_mode”: eager (default) | lazy | manual
  
  * lazy: don’t start servers at session start; start on first tool/resource use
  * manual: never auto-start; load explicitly

* In-session command: `/mcp load <name>` (plus `/mcp` status + `/mcp retry ...`)

* CLI one-run override: `xcodex --mcp-startup-mode lazy` (or eager/manual)

This keeps startup fast for sessions that don’t use MCP, while still making it easy to opt-in when you do. AND saving on context!

wow nice. Would you be able to do a PR for the official codex repo? Seems very useful :)

saidelike avatar Feb 06 '26 11:02 saidelike

This is very much needed

tokezooo avatar Feb 27 '26 08:02 tokezooo

While waiting for native lazy loading support, you can at least identify which MCP servers are the worst offenders using mcp-checkup:

npx mcp-checkup

It analyzes your setup, measures token cost per server/tool, and grades them A-F. Users with many MCP servers often discover 2-3 servers account for 80% of context overhead — disabling just those gives you most of the benefit of lazy loading without client-side changes.

Zero config, just npx and go.

yifanyifan897645 avatar Mar 30 '26 11:03 yifanyifan897645

+1

taotao365s avatar Jun 20 '26 10:06 taotao365s

Lazy MCP loading probably needs four states, not only eager versus lazy: configured, manifest-known, started, and invoked.

Tool search can operate over manifest-known servers without paying startup cost, then start a server only when a selected tool needs execution. The metric to watch is also two numbers: manifest tokens loaded into the prompt and first-use latency after tool selection. That prevents the fix from trading context overhead for opaque stalls on the first real MCP call.


Generated with ax.

Necmttn avatar Jun 21 '26 11:06 Necmttn

tool_search avoids eagerly loading every MCP tool.

etraut-openai avatar Aug 05 '26 03:08 etraut-openai

@saidelike hey — I'm researching recurring problems caused by large MCP/tool setups and found your proposal.

Several months later, is MCP context usage still a meaningful problem in your actual workflow?

Also, do you currently do anything manually to decide which tools get loaded, or have newer Codex versions mostly solved this for you?

aldi949 avatar Sep 23 '26 06:09 aldi949