Skip to content

fix(agent-core-v2): start a project's MCP servers only while something uses them - #5203

Merged
Grapedge merged 3 commits into
mainfrom
feat/mcp-checkout-anchor
Nov 23, 2026
Merged

Grapedge merged 3 commits into
mainfrom
feat/mcp-checkout-anchor

Conversation

@Grapedge

@Grapedge Grapedge commented Nov 21, 2026 •

Copy link
Copy Markdown
Collaborator

Requirement or Bug

打开一个项目就会启动它的 MCP 服务,哪怕这次只是查信任、列技能、补全文件名,或者归档一个没打开的会话。结果是 CLI 启动时同一个服务会被启动两次以上,占固定端口的服务第二份起不来。这个 PR 让 MCP 服务在真正有会话用到时才启动,最后一个会话关闭时停止。

下一个 PR(#5204)会把同一仓库的 git worktree 归到一个项目里。没有会话在用的检出不该启动服务,所以先把这一步单独做掉。

Bug Reproduction Steps

  1. 在 ~/.kimi-code/mcp.json 里配一个 stdio 服务,让它启动时往文件里追加一行。
  2. 在任意目录运行 kimi -p "hi"。
  3. 文件里有两行。改动后只有一行。

Root Cause

一套 MCP 由读配置和管连接两个服务组成,它们在项目打开时就创建,创建即启动。项目打开的原因很多,查信任、列技能、归档关着的会话都会打开它。每次打开都启动一份,关闭又是异步的,所以短时间内会叠出多份。这里是根本修复:启动和停止改由「谁在用」决定。

Code Changes

核心思路。 一个项目仍然只有一套 MCP,读的目录、启动目录、信任判断都不变。变的是它由谁拿着:会话打开时借一份,关闭时还;要列 MCP 的查询借了就还。第一个借的人触发启动,最后一个归还触发停止。停止还没完成又有人来借,就等旧进程退出再启动,所以不会和自己抢端口。

flowchart LR
  S1[会话 A] -->|借用| M[项目的一套 MCP]
  S2[会话 B] -->|借用| M
  Q1[列 MCP 的查询] -->|借用后立即归还| M
  Q2[查信任 列技能 补全文件] -.->|不再借用| M
Loading
决策 选择 没选 理由
何时启动 第一个会话,或者要列 MCP 的查询 项目打开 只读查询和对关着的会话的操作用不到它
何时停止 最后一个借用者归还 项目关闭 项目会被查询继续占着,那时服务已经没人用
停止期间又打开 等旧进程退出再启动 立刻启动新的一份 占固定端口的服务会起不来
配置要不要也借用 读配置和管连接作为一套一起借用 配置留在项目上一直读 下一个 PR 会给每个检出一套,配置也是每套一份;没有会话用的检出不必读配置、监听文件
阅读指南
  1. packages/agent-core-v2/src/program/programMcp.ts:借用、启动、停止。
  2. packages/agent-core-v2/src/workspace/sessionLifecycle/sessionLifecycleService.ts:会话在连接环境之前借 MCP,打开失败时归还。packages/agent-core-v2/src/program/program.ts:查询为什么借不到。
  3. packages/agent-core-v2/src/mcpCore/connection-manager.ts 和 client-stdio.ts:关闭之后不再启动任何服务,包括环境还在连接、进程正在启动时关闭的情形。这是第一个提交,单独可看。

可以略读:programDependencies.ts、workspaceInstanceManagerService.ts 的接线。

Behavior Changes and Affected Users

Behavior Before After Who relies on the old behavior Escape hatch
项目的 MCP 服务何时启动 项目一打开就启动,包括查信任、列技能、补全文件名,以及 fork、归档、删除没打开的会话 第一个会话打开(远端会话在连接远端之前就开始加载),或者查询要列 MCP 时启动 无 无
何时停止 项目关闭 最后一个会话关闭,即使项目还被查询占着 无 无
CLI 启动时的进程数 信任检查、列技能、会话各启动一份 一份 无(缺陷) 无
会话在 MCP 启动完成前关闭(首次加载前、stdio 服务的环境还在连接、进程正在启动) 之后照样启动,没人停;会话自带的临时服务也一样 已经启动的随之停掉,还没启动的不再启动 无(缺陷) 无
远端会话的 MCP 项目打开时就开始加载,和连接远端并行 会话打开时先借 MCP 再连接远端,仍然并行;连不上远端时 MCP 启动后随即停掉 无 无
项目还被别的请求占着,最后一个会话刚关闭又开会话 直接用一直在运行的那套 旧的一套随最后一个会话停掉,新会话等旧进程退出后再启动(stdio 服务最多等 5 秒) 无 无
项目就绪信号 program.ready 等 MCP 加载完 不等 MCP 无,产品代码没有地方等它 无
mcp_connected、mcp_failed 遥测的次数 每次打开项目都上报一次 每次真正启动才上报 按次数统计 MCP 使用的看板 无
没有会话在用时,项目级 MCP 配置的读取、文件监听和 OAuth 配置来源登记 项目打开期间一直在 只在有会话或查询借用时在 无 无
调试接口 /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

  • 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.

@changeset-bot

changeset-bot Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 77becf8

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

This PR includes changesets to release 5 packages
Name Type
@moonshot-ai/kimi-code Patch
kimi-code-app Patch
@moonshot-ai/kimi-code-sdk Patch
kimi-code Patch
@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 21, 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-23T06:43:45.066613Z 77becf8 New commits
ℹ️ 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 21, 2026 •

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

commit: 77becf8

@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: 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".

@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: 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".

@Grapedge
Grapedge added this pull request to stack #5225 November 22, 2026 15:17
@Grapedge
Grapedge force-pushed the feat/mcp-checkout-anchor branch from 09878c8 to c594082 Compare November 23, 2026 02:45
@Grapedge Grapedge changed the title feat(agent-core-v2): load project MCP servers from each session's checkout anchor fix(agent-core-v2): start a project's MCP servers only while something uses them Nov 23, 2026

@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: 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".

@Grapedge
Grapedge force-pushed the feat/mcp-checkout-anchor branch 2 times, most recently from 2ef2a40 to 1b63186 Compare November 23, 2026 06:14

@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: 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.
@Grapedge
Grapedge force-pushed the feat/mcp-checkout-anchor branch from 1b63186 to 77becf8 Compare November 23, 2026 06:41
@Grapedge
Grapedge merged commit 3d7f4c1 into main Nov 23, 2026
24 checks passed
@Grapedge
Grapedge deleted the feat/mcp-checkout-anchor branch November 23, 2026 09:02
@github-actions github-actions Bot mentioned this pull request Nov 24, 2026
7723qqq pushed a commit to 7723qqq/kimi-code that referenced this pull request Nov 24, 2026
Mark MoonshotAI#5048, MoonshotAI#5068, MoonshotAI#5203 and MoonshotAI#5260 ported with executable grep probes and
evidence from the fork commits that carried them.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants