On-demand MCP tool loading
On-demand MCP tool loading
A connected MCP server sends every tool definition with every request. Notion's hosted server advertises 46 tools whose definitions total about 214,500 characters; one tool's input schema alone is about 83,000 characters. A Notion plugin that only registers the server therefore costs that context on every request, before the model uses a single Notion tool. The existing `ToolSchema.deferLoading` marker does not solve
English
Problem
A connected MCP server sends every tool definition with every request. Notion's hosted server advertises 46 tools whose definitions total about 214,500 characters; one tool's input schema alone is about 83,000 characters. A Notion plugin that only registers the server therefore costs that context on every request, before the model uses a single Notion tool.
The existing ToolSchema.deferLoading marker does not solve this. Only routes that declare toolUpdate honor it, and the default deepseek-flash entry is the only catalog route that does. The marker keeps a prefix reusable; it does not reduce context.
Decision
An mcp-client entry with toolLoading: on-demand keeps the server's tools out of the tool registry. The behavior lives in mcp-client; no session event, message type, or other package changes, and mcp-manager only passes the field to its instances.
One loader per server. The server registers mcp__<serverName>__load_tools where an eager server registers its tools, and withdraws it where an eager server removes them, so outages and reconnects follow the eager lifecycle. A server tool named load_tools is left out of the catalog with a logged warning.
The catalog is a system prompt section. The mcp:<serverName>:tools section lists each tool's raw name with the first line of its description, capped by catalogDescriptionMaxLength, and renders only for an agent that sees the loader. The session log already records the system prompt, so the catalog needs no new message type, and it changes only when the server's tool list does.
Loading registers in the agent's own scope. The loader validates every requested raw name before registering any, then registers the definitions in a scope the plugin creates for that agent. The plugin releases the scope on agent/disposed, and plugin unload disposes it. The next request header carries the definitions, and the agent loop records the addition. Subagents, forks, and resumed sessions start with no loaded tools.
Each published generation carries loaded tools forward. A tool definition calls the client of the generation that built it, so a reconnect must replace loaded definitions. Each published generation re-registers every agent's loaded names that it still offers and drops the rest. A failed re-registration drops only that agent's tool and logs the failure.
The loader decides access. Tool restrictions filter only inherited tools, so seeing the loader permits loading any listed tool, as seeing skill permits loading any listed skill. Execution guards and permissions still apply.
Alternatives considered
A shared load_mcp_tools tool in mcp-resources with a session catalog message. Rejected after implementation: it needed a cross-package catalog interface, a Host-wide-only restriction, an agent/pre-step listener that scanned session history on every step of every agent, and a persisted message type.
The catalog in the loader's tool description. Rejected: OpenAI and Azure OpenAI Chat Completions reject tool descriptions longer than 1024 characters, pi-ai routes pass descriptions through unchanged, and a catalog of a dozen tools exceeds that limit.
A static includeTools list per entry. Rejected: each plugin must choose its tools ahead of time, and the model cannot use the other tools during a session.
A proxy tool call_mcp_tool(server, tool, args). Rejected: the tool list never changes, but the model loses each tool's argument schema, and permissions, presentation, and guards that match tool names would need forwarding.
Mark MCP tools with deferLoading. Rejected: it saves no context and works only on routes that declare toolUpdate.
Global registration hidden per agent with tools.restrict({ deny }). Rejected: every agent needs a restriction scope, and every tool change rescans the registry and copies every schema.
Restore loaded tools on resume. Rejected: it needs a durable record of loaded names, which is a session format change. The agent loop already records which tools a resumed session lost.
Testing
mcp-client specs cover the loader-only registration and the prompt catalog, catalog omission for agents that cannot see the loader, loading into one agent's scope, invalid loads, carrying loaded tools across generations, withdrawal and restoration, agent release, a contained re-registration failure, the excluded load_tools server tool, a loader-name conflict, and the plugin path from load through call to unload. A manager spec mounts an on-demand entry. The mcp-tool-loading session snapshot pins the catalog section, the loader schema, the tool-addition record, and a call after loading.
Consequences
- An on-demand server costs the loader definition and one catalog line per tool until an agent loads tools.
- Each load changes the next request's tool list. Routes declaring tool updates keep the earlier prefix; other routes start a new request series once per load.
- The model needs one extra tool call before using a server's tools.
- Agent tool restrictions do not filter loaded tools;
tools.guarddenies a loadable tool by name. - A PTC program sees a tool loaded inside
run_codeonly from the next program.
中文
Problem
已连接的 MCP 服务器会在每次请求中发送全部工具定义。Notion 托管服务器提供 46 个工具,定义合计约 214,500 字符;其中一个工具的输入 schema 就约有 83,000 字符。因此,只注册服务器的 Notion 插件在模型使用任何 Notion 工具之前,就会让每次请求都承担这部分上下文。
现有的 ToolSchema.deferLoading 标记解决不了这个问题。只有声明了 toolUpdate 的路由才会遵循它,而目录中只有默认的 deepseek-flash 条目声明了。该标记能保留可复用的前缀,但不会减少上下文。
Decision
配置 toolLoading: on-demand 的 mcp-client 条目不把服务器的工具注册到工具注册表。该行为位于 mcp-client 内部;不改动会话事件、消息类型或其他包,mcp-manager 只把该字段传给它的实例。
每个服务器一个加载器。 服务器在 eager 服务器注册工具的位置注册 mcp__<serverName>__load_tools,并在 eager 服务器移除工具的位置撤下它,因此断线与重连遵循 eager 的生命周期。服务器自己名为 load_tools 的工具不列入目录,并记录一条警告。
目录是一个系统提示词段落。 mcp:<serverName>:tools 段落列出每个工具的原始名称及其描述的第一行,长度以 catalogDescriptionMaxLength 为上限,并且只对能看到加载器的 agent 渲染。会话日志本来就记录系统提示词,所以目录不需要新的消息类型;只有服务器的工具列表变化时目录才会变化。
加载注册到 agent 自己的作用域。 加载器先校验所有请求的原始名称,再注册任何工具;随后把这些定义注册到插件为该 agent 创建的作用域中。插件在 agent/disposed 时释放该作用域,插件卸载时也会释放它。下一次请求头带上这些定义,agent loop 记录这次添加。子 agent、fork 和恢复的会话开始时都没有已加载的工具。
每个发布的注册代都会延续已加载的工具。 工具定义调用的是构建它的那一代连接的 client,因此重连后必须替换已加载的定义。每次发布注册代时,会为每个 agent 重新注册该代仍提供的已加载名称,并丢弃其余名称。重新注册失败只丢弃该 agent 的这个工具,并记录失败。
加载器决定访问权限。 工具限制只过滤继承的工具,因此能看到加载器就能加载目录中的任何工具,就像能看到 skill 就能加载目录中的任何 skill。执行 guard 与权限检查照常生效。
Alternatives considered
在 mcp-resources 中放一个共享的 load_mcp_tools 工具,并发布会话目录消息。 实现后否决:它需要跨包的目录接口、只支持 Host 级服务器的限制、在每个 agent 的每一步都扫描会话历史的 agent/pre-step 监听器,以及一种持久化消息类型。
把目录放进加载器的工具描述。 否决:OpenAI 与 Azure OpenAI 的 Chat Completions 会拒绝超过 1024 字符的工具描述,pi-ai 路由原样传递描述,而十几个工具的目录就会超过这个上限。
每个条目一个静态 includeTools 列表。 否决:每个插件都必须预先选定工具,会话中模型无法使用其他工具。
代理工具 call_mcp_tool(server, tool, args)。 否决:工具列表从不变化,但模型失去每个工具的参数 schema,按工具名匹配的权限、展示与 guard 都需要转发。
给 MCP 工具加 deferLoading 标记。 否决:它不节省上下文,且只在声明 toolUpdate 的路由上生效。
全局注册,再用 tools.restrict({ deny }) 按 agent 隐藏。 否决:每个 agent 都需要一个限制作用域,每次工具变化都会重新扫描注册表并复制每个 schema。
恢复会话时恢复已加载的工具。 否决:这需要持久记录已加载的名称,也就是改动会话格式。agent loop 已经会记录恢复的会话失去了哪些工具。
Testing
mcp-client 规格覆盖:只注册加载器与提示词目录、看不到加载器的 agent 不渲染目录、加载到单个 agent 的作用域、无效加载、跨注册代延续已加载工具、撤下与恢复、释放 agent、限定在单个 agent 的重新注册失败、被排除的 load_tools 服务器工具、加载器名称冲突,以及从加载、调用到卸载的插件路径。一个 manager 规格挂载按需条目。mcp-tool-loading 会话快照固定目录段落、加载器 schema、工具添加记录,以及加载后的一次调用。
Consequences
- 在 agent 加载工具之前,按需服务器的开销是加载器定义加每个工具一行目录。
- 每次加载都会改变下一次请求的工具列表。声明了工具更新的路由保留之前的前缀;其他路由每次加载开始一个新的请求序列。
- 模型在使用服务器的工具之前需要多一次工具调用。
- agent 的工具限制不过滤已加载的工具;用
tools.guard可按名称禁止可加载的工具。 - PTC 程序要到下一个程序才能看到在
run_code内加载的工具。