Skip to content

fix(agent-core-v2): keep the tool list when a connected MCP server adds tools mid-session - #5627

Open
RealKai42 wants to merge 1 commit into
mainfrom
fix/mcp-late-tools-keep-list
Open

RealKai42 wants to merge 1 commit into
mainfrom
fix/mcp-late-tools-keep-list

Conversation

@RealKai42

Copy link
Copy Markdown
Collaborator

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

  1. 用一个没有 dynamically_loaded_tools 能力的模型开会话,配置一个 MCP 服务 docs,开始时只有工具 search。
  2. 第一个回合结束后,让服务新加工具 lookup 并发出 notifications/tools/list_changed。
  3. 再发一条消息:请求的工具列表末尾多了 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 lateMcp
  • src/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 也是 initial

Behavior Changes and Affected Users

行为 之前(main,#5433 之后) 之后 谁依赖旧行为 退路
非 disclosure 会话里,基线中已有工具的 MCP 服务中途新加工具 追加到工具列表末尾,缓存前缀断开;模型可以直接调用 不进工具列表,只通过提醒告诉模型,和 #5433 之前一样;这个会话里模型调不到它 用没有 dynamically_loaded_tools 的模型、MCP 服务又会中途发 tools/list_changed 的用户,比如认证后才放出工具、按需开启工具集的服务。#5433 只在 main 上,还没发版,所以已发布的版本里没有人依赖它 新开会话:新会话的基线会包含这个工具。模型确实支持时,在模型配置里声明 dynamically_loaded_tools,改走按需加载
非 disclosure 会话里,基线中一个工具都没有的 MCP 服务(晚连上)的工具 追加 不变 — —
服务前缀取不到的 MCP 工具(清洗后的服务名约 50 个字符以上) 追加 不变,照旧追加 — —
disclosure 会话 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 的用例:正常名字、工具名尾部被哈希截断、服务名太长导致分界被截掉。
  • feat(agent-core-v2): defer MCP tools by default and cap startup wait at 2s #5433 自己的用例照旧通过:toolSelect.e2e.test.ts 里几条晚连上的用例,以及 toolSelectService.test.ts 的「appends late MCP tools only to the loop baseline request」。
  • 新 E2E 的 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 退出。
  • E2E mcp-tool-change-keeps-the-tool-list:ACP 面修复前失败、修复后通过;TUI 面修复后通过。Desktop、Web 由这个 PR 的 CI 跑(打了 e2e 标签)。

改动不涉及路径、子进程、shell 或终端输入。

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue (external PRs: issue must have a maintainer's /approve).
  • I have added tests that prove my feature works.
  • The behavior-change table above is complete, and every removed behavior or flipped default is named in the changeset and either has an escape hatch or was explicitly approved by a maintainer in this PR.
  • Ran gen-changesets skill, or this PR needs no changeset.
  • Ran gen-docs skill, or this PR needs no doc update.

…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.
@RealKai42 RealKai42 added the e2e Run the cross-surface E2E suite on this pull request label Nov 27, 2026
@changeset-bot

changeset-bot Bot commented Nov 27, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 41b9c10

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@moonshot-ai/agent-core-v2 Patch

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

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 27, 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-27T06:11:59.032848Z 41b9c10 PR opened
ℹ️ 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.

@pkg-pr-new

pkg-pr-new Bot commented Nov 27, 2026

Copy link
Copy Markdown
pnpm dlx https://pkg.pr.new/@moonshot-ai/kimi-code@41b9c10
npx https://pkg.pr.new/@moonshot-ai/kimi-code@41b9c10

commit: 41b9c10

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 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".

Comment on lines +184 to +185
const server = mcpServerPrefix(info.name);
if (server !== undefined && baselineServers.has(server)) return false;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge 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) ?? []));

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge 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 👍 / 👎.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

e2e Run the cross-surface E2E suite on this pull request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant