Repository navigation
fix(agent-core-v2): start a project's MCP servers only while something uses them - #5203
Conversation
🦋 Changeset detectedLatest commit: 77becf8 The changes in this PR will be included in the next version bump. This PR includes changesets to release 5 packages
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: 049eef98ab
ℹ️ About Codex in GitHub
Codex has been enabled to automatically 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 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 0359a7054d
ℹ️ About Codex in GitHub
Codex has been enabled to automatically 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 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
09878c8 to
c594082
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: c594082109
ℹ️ About Codex in GitHub
Codex has been enabled to automatically 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 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
2ef2a40 to
1b63186
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 1b6318696d
ℹ️ About Codex in GitHub
Codex has been enabled to automatically 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 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
A manager that was shut down before its first load still connected its servers when the load arrived, and nothing stopped them afterwards. The servers of a project and the ones a session brings along now dispose their manager, which refuses later connections. A stdio server whose client was closed while its environment was still connecting, or while its process was being spawned, still started, and nothing stopped it either. The transport now checks after each step: it releases the environment it acquired and stops a process it has already spawned.
…g uses them Opening a project started its MCP servers, so checking trust, listing skills, suggesting files, or archiving a closed session started them too, and the CLI started every server more than once. The servers now start with the first session, or with the query that lists them, and stop with the last holder. Starting them again waits until the previous ones have exited. The project no longer exposes its servers; a caller borrows them and gives them back. Every session of the project, including pinned and remote ones, still shares one set. A session borrows them before it connects to its environment, so they load while a remote environment connects, as before, and the first request of the session offers their tools. A session that fails to open or resume gives them back.
1b63186 to
77becf8
Compare
Mark MoonshotAI#5048, MoonshotAI#5068, MoonshotAI#5203 and MoonshotAI#5260 ported with executable grep probes and evidence from the fork commits that carried them.
Requirement or Bug
打开一个项目就会启动它的 MCP 服务,哪怕这次只是查信任、列技能、补全文件名,或者归档一个没打开的会话。结果是 CLI 启动时同一个服务会被启动两次以上,占固定端口的服务第二份起不来。这个 PR 让 MCP 服务在真正有会话用到时才启动,最后一个会话关闭时停止。
下一个 PR(#5204)会把同一仓库的 git worktree 归到一个项目里。没有会话在用的检出不该启动服务,所以先把这一步单独做掉。
Bug Reproduction Steps
~/.kimi-code/mcp.json里配一个 stdio 服务,让它启动时往文件里追加一行。kimi -p "hi"。Root Cause
一套 MCP 由读配置和管连接两个服务组成,它们在项目打开时就创建,创建即启动。项目打开的原因很多,查信任、列技能、归档关着的会话都会打开它。每次打开都启动一份,关闭又是异步的,所以短时间内会叠出多份。这里是根本修复:启动和停止改由「谁在用」决定。
Code Changes
核心思路。 一个项目仍然只有一套 MCP,读的目录、启动目录、信任判断都不变。变的是它由谁拿着:会话打开时借一份,关闭时还;要列 MCP 的查询借了就还。第一个借的人触发启动,最后一个归还触发停止。停止还没完成又有人来借,就等旧进程退出再启动,所以不会和自己抢端口。
阅读指南
packages/agent-core-v2/src/program/programMcp.ts:借用、启动、停止。packages/agent-core-v2/src/workspace/sessionLifecycle/sessionLifecycleService.ts:会话在连接环境之前借 MCP,打开失败时归还。packages/agent-core-v2/src/program/program.ts:查询为什么借不到。packages/agent-core-v2/src/mcpCore/connection-manager.ts和client-stdio.ts:关闭之后不再启动任何服务,包括环境还在连接、进程正在启动时关闭的情形。这是第一个提交,单独可看。可以略读:
programDependencies.ts、workspaceInstanceManagerService.ts的接线。Behavior Changes and Affected Users
program.readymcp_connected、mcp_failed遥测的次数/api/v1/debug/workspaces和/api/v1/debug/workspace/:id/snapshot(要开--debug-endpoints)里的 MCP 列表会话能看到的内容不变:同一套配置、同一个启动目录、同项目会话共用一套,钉住目录的会话和远端会话也用这一套。
workspaceMcp.test.ts、workspaceMcpConfig.test.ts、configLoader.test.ts里的原有用例原样通过。受影响模块。 agent-core-v2 的 program、workspaceMcp、mcpCore。SDK 的
listWorkspaceMcpServers借了就还。CLI、桌面端、VS Code 的源码没有改动。测试。 打开项目不启动、第一个会话启动、会话共用、最后一个关闭才停、停止期间再打开、列完就停、只读查询和归档不启动、加载前关闭不启动,分别由引擎、KAP、SDK 和真实
kimi -p进程覆盖。远端环境比 MCP 服务慢 1 秒连上时,首个模型请求里就有 MCP 工具(KAP)。会话打开和恢复失败的 6 种情形都不留下 MCP 进程(SDK)。stdio 服务在环境连接中或进程启动中被关闭时不留进程(单测)。这些新测试在改动前会失败(远端那条在 main 上通过,在本 PR 早先的版本上失败)。没有在 Windows 和真实 SSH 主机上跑。Checklist
/approve).gen-changesetsskill, or this PR needs no changeset.gen-docsskill, or this PR needs no doc update.