Repository navigation
Conversation
…ds tools mid-session For models without dynamic tool loading, the late-connected MCP step in requestTools() appended every active MCP tool missing from the baseline. That also caught tools an already-connected server adds through tools/list_changed, so each such addition changed the request tool list and invalidated the prompt cache prefix. Only append tools from MCP servers that have no tool in the baseline. Tools added mid-session by a server already in the baseline are announced in a reminder only, as before the late-connected step was introduced.
🦋 Changeset detectedLatest commit: 41b9c10 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
commit: |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 41b9c10d08
ℹ️ 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".
| const server = mcpServerPrefix(info.name); | ||
| if (server !== undefined && baselineServers.has(server)) return false; |
There was a problem hiding this comment.
Preserve distinct server identities when filtering late tools
For a non-disclosure session with docs connected at startup and docs_ connecting later, this check suppresses every tool from the late server. Both are valid, distinct server names: qualifyMcpToolName('docs', 'search') produces mcp__docs__search, while qualifyMcpToolName('docs_', 'fetch') produces mcp__docs___fetch, but mcpServerPrefix() returns mcp__docs__ for both. Previously the late server's tools were appended; now they remain absent from subsequent requests throughout the session. Use the actual server identity, or another unambiguous association, rather than the first __ delimiter to determine baseline membership.
Useful? React with 👍 / 👎.
| !present.has(info.name) && | ||
| this.toolPolicy.isToolActive(info.name, info.source), | ||
| ); | ||
| const baselineServers = new Set(result.flatMap((tool) => mcpServerPrefix(tool.name) ?? [])); |
There was a problem hiding this comment.
Exclude synthetic authentication tools from baseline server detection
For models without dynamic tool loading, an MCP server awaiting OAuth at startup can have its synthetic mcp__srv__authenticate tool in the baseline without having connected successfully. registerNeedsAuthMcpServer() in agent/mcp/mcpService.ts registers this placeholder, and successful authentication replaces it with the server's real tools. Counting the placeholder here marks the server as already connected, so every real tool is subsequently filtered out; requests retain only the now-unregistered authentication tool. Previously those real tools were appended after authentication. Distinguish authentication placeholders from successfully discovered server tools and preserve the same-session OAuth-to-tool-call flow.
AGENTS.md reference: AGENTS.md:L193-L193
Useful? React with 👍 / 👎.
Requirement or Bug
#5433 让 MCP 工具默认按需加载,并把启动时等 MCP 的时间限制在 2 秒。它在非 disclosure 分支(模型没有
dynamically_loaded_tools,第三方模型默认都是)加了一步:把晚连上的服务的工具追加到请求的工具列表末尾。但这一步同时把「已经连上的服务中途通过tools/list_changed新加的工具」也追加了进去。工具列表一变,prompt 缓存前缀就整段失效,用户看不到任何信号。main 上新 E2E 的缓存场景mcp-tool-change-keeps-the-tool-list从 #5433 起,在 TUI、ACP、Desktop、Web 四个面上都红。这个 PR 之后:已连上的服务中途新加的工具只发提醒,回到 #5433 之前的行为;晚连上的服务的工具照旧追加,#5433 想要的能力不变。
Bug Reproduction Steps
dynamically_loaded_tools能力的模型开会话,配置一个 MCP 服务docs,开始时只有工具search。lookup并发出notifications/tools/list_changed。mcp__docs__lookup,缓存前缀断开。重启后再发一条,工具列表里仍然带着它。E2E:
pnpm --filter @moonshot-ai/e2e exec playwright test --project=acp --workers=1 scenarios/cache/mcp-tool-change-keeps-the-tool-list.spec.ts。CI 上,36fdcaa39(#5433 的前一个提交)的 main 推送运行 45940392114 全绿;357a3118c(#5433)的 45940396599 四个面都只红在这一条。Root Cause
requestTools()的非 disclosure 分支判断「晚连上」时,只看工具在不在基线里。所以它分不清两种情况:这次是根本修复:改成按服务判断。一个服务只要有工具在基线里,就算已经连上。
Code Changes
requestTools(base, disclosure) shape base for the disclosure mode if disclosure or base is not the loop baseline: return lateMcp = current MCP tools not already in the request + whose server has no tool in the baseline that are active by policy append lateMcpsrc/tool/toolContract.ts新加mcpServerPrefix(),放在isMcpToolName()旁边,从mcp__<服务>__<工具>里取出mcp__<服务>__。服务名清洗时会把连续的下划线合成一个,所以清洗后的服务名不含__,第一个__就是分界。工具名超长、被截断加哈希、分界也被截掉时,返回undefined。src/agent/toolSelect/toolSelectService.ts:基线里的工具算出一组服务前缀;当前的 MCP 工具,服务前缀在这组里的不追加,其余条件不变。docs/{en,zh}/customization/mcp.md:feat(agent-core-v2): defer MCP tools by default and cap startup wait at 2s #5433 写的是不支持动态加载的模型会收到「启动后才发现的工具」,这句改成分开说:晚连上的服务的工具会追加;已连上的服务之后新增的工具只发提醒,要直接调用就新开会话。origin区分origin === 'runtime'才不追加origin是第一个回合定的。晚连上的服务如果赶在第一个回合之前连上,它的工具origin也是initialBehavior Changes and Affected Users
dynamically_loaded_tools的模型、MCP 服务又会中途发tools/list_changed的用户,比如认证后才放出工具、按需开启工具集的服务。#5433 只在 main 上,还没发版,所以已发布的版本里没有人依赖它dynamically_loaded_tools,改走按需加载requestTools()提前返回没有改提示词文本,也没有改配置项、持久化格式和契约文件。
已知的边界:服务名
docs和docs_会得到同一个前缀,两个服务同时存在时,其中一个晚连上不会被追加。这次没修、另记:晚连上的服务连上时仍断一次缓存,这是 #5433 接受的取舍;重启后晚连上的服务 2 秒内没连上,会多断两次;晚连上的服务中途新加工具,每加一次断一次。
release 分支: #5514 把 #5433 原样搬到
release/2026-11-26,带着同一段代码。release 分支上没有这条缓存场景,所以 #5514 的 E2E 全绿是假阴性。#5514 合入时需要把这个修复一起带上。受影响模块和测试覆盖:
toolSelectService.test.ts新加「keeps the baseline request tools when an MCP server already in the baseline adds a tool」:同一个服务中途新加的工具不追加,另一个晚连上的服务的工具照旧追加。修复前失败,多出的正是mcp__docs__lookup。tool-naming.test.ts新加mcpServerPrefix的用例:正常名字、工具名尾部被哈希截断、服务名太长导致分界被截掉。toolSelect.e2e.test.ts里几条晚连上的用例,以及toolSelectService.test.ts的「appends late MCP tools only to the loop baseline request」。mcp-tool-change-keeps-the-tool-list覆盖四个面上的整条路径,包括重启。本机运行记录(macOS)
pnpm exec vitest run packages/agent-core-v2/test/agent packages/agent-core-v2/test/session packages/agent-core-v2/test/mcpCore:115 个文件、2977 条用例通过。pnpm exec tsc -p packages/agent-core-v2/tsconfig.json --noEmit:通过。pnpm lint:以 0 退出。mcp-tool-change-keeps-the-tool-list:ACP 面修复前失败、修复后通过;TUI 面修复后通过。Desktop、Web 由这个 PR 的 CI 跑(打了e2e标签)。改动不涉及路径、子进程、shell 或终端输入。
Checklist
/approve).gen-changesetsskill, or this PR needs no changeset.gen-docsskill, or this PR needs no doc update.