Skip to content

fix(mcp): isolate OAuth authorization cancellation per waiter - #4643

Merged
7Sageer merged 2 commits into
MoonshotAI:mainfrom
pvzheroes125:feat/mcp-oauth-cancel-wait
Nov 3, 2026
Merged

7Sageer merged 2 commits into
MoonshotAI:mainfrom
pvzheroes125:feat/mcp-oauth-cancel-wait

Conversation

@pvzheroes125

@pvzheroes125 pvzheroes125 commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Requirement or Bug

同一 MCP 服务器的多个授权调用会共用浏览器回调和 token exchange。当前第一个调用的 signal 和超时直接控制共享流程,取消它可能同时中断其他仍在等待的调用;宿主也无法通过工具更新中的标识,只取消选中的一次授权等待。

本次为每个等待提供独立取消入口,让对应工具返回取消结果,其他等待可以继续完成。这里提供 Core 能力,宿主授权卡按钮与接线由消费方单独完成。

Bug Reproduction Steps

  1. 对同一服务器和 URL 发起两次 beginAuthorization(),两者开始 complete()。
  2. 中止第一个调用的 signal。
  3. 尝试完成第二个调用的浏览器回调:原实现中共享回调已被中断,第二个调用无法完成。

Root Cause

startCompletion() 复用同一个 Promise,却把首个调用者的 signal 和 timeout 交给共享回调与 token exchange。原有 cancel() 只释放句柄,没有独立终止该句柄正在进行的等待。

Code Changes

共享授权继续由原 OAuth service 负责;每个调用者独立等待其结果,并分别处理取消、signal 和回调超时。

同一服务器的共享授权:浏览器回调 → token exchange
  ├─ 等待 A:独立 waitId / cancel / signal / callback timeout
  └─ 等待 B:独立 waitId / cancel / signal / callback timeout
最后一个等待退出 → 停止无人等待的共享流程
  • oauth/service.ts:返回 waitId,增加 cancelAuthorizationWait(waitId);完成或取消后清理标识,迟到取消幂等。取消等待不撤销已有 token。
  • mcp/tools/auth.ts:在现有 OAuth URL 更新中附带 waitId。取消仍走工具已有错误返回,不中止整轮对话。
  • TUI 的 OAuth URL 解析返回类型明确为实际使用的服务器名与 URL 两字段;保留旧更新的解析行为,Core 新事件的 waitId 仍为必填。
  • 回调按时到达后清除等待超时,后续 token exchange 保持原来的超时范围。

Behavior Changes and Affected Users

行为 修改前 修改后 受影响调用方 兼容说明
同一服务器的并发授权等待 首个调用的取消或超时可能中断所有等待 每个等待独立取消或超时,其他等待继续 通过 Core 发起 MCP OAuth 的 CLI、SDK 与宿主 保留既有服务器级取消接口
取消正在完成的单个句柄 cancel() 仅释放句柄,无法独立结束其等待 本次等待返回取消;最后一个等待退出时停止底层流程 使用 BeginAuthorizationResult 的调用方 不撤销已有 token,迟到取消不影响已完成授权
按标识取消工具授权 工具更新没有可定位单次等待的标识 增加 waitId 和对应 service 方法 消费 OAuth custom update 的宿主 原有 URL、serverName、expiresAt 保留;需要取消的宿主显式接入
timeoutMs 的作用范围 限制等待 callback,不限制后续 exchange 保持原范围,每个等待单独计时 所有现有 OAuth 调用方 已覆盖 callback 按时到达但 exchange 较慢的回归

聚焦验证通过:取消首个工具等待后,第二个等待仍能授权成功;工具取消不设置 stopTurn,不 abort 原 turn signal、不 reconnect;迟到取消不撤销 token。未执行真实账号授权或宿主 UI 联调。

验证详情

在 packages/agent-core-v2 执行:

pnpm exec vitest run --config vitest.config.ts test/mcpCore/oauth/service.test.ts test/agent/mcp/tools/auth.test.ts
pnpm typecheck

OAuth service/auth 53 项测试通过;callback-server 10 项与 TUI OAuth 5 项测试通过。Core package 和 CLI 类型检查通过;改动文件 lint 无错误,Core 有 9 条既有警告;comment-free 与 git diff --check 通过。

CLI 类型检查命令(仓库根目录):pnpm exec tsc -p apps/kimi-code/tsconfig.json --noEmit。原有取消断言按单等待者取消结果更新,callback 关闭与资源释放测试保留。

两个其他既有测试文件仅补齐 3 个 mock 的 waitId 字段。

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.

No CLI command or configuration documentation changes are required. No related issue was provided.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-03T09:15:57.472278Z 48e9151 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 3, 2026 •

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

commit: 84d003a

@7Sageer
7Sageer merged commit 42cb675 into MoonshotAI:main Nov 3, 2026
17 checks passed
@github-actions github-actions Bot mentioned this pull request Nov 3, 2026
7723qqq pushed a commit to 7723qqq/kimi-code that referenced this pull request Nov 4, 2026
Port of upstream MoonshotAI/kimi-code 42cb675 (MoonshotAI#4643), adapted to this fork's
authorization flow.

Each attach() now gets its own waitId and abort controller, registered in an
authorizationWaits map with a cancelAuthorizationWait(waitId) lookup. Cancelling
one waiter only aborts that waiter; the shared flow's controller is aborted (and
its callback listener closed) when the last attached handle detaches. A joined
caller that aborts while another is still completing no longer tears the flow
down under it.

The per-waiter deadline also stops leaking into the exchange: the callback
deadline is created from the waiter's signal, cleared as soon as the callback
settles, and the code exchange no longer carries the caller's timeout.

Local adaptation: upstream's version hangs the cancellation off the
configuration-source registry this fork removed (it uses a credentials
coordinator instead), so cancelAuthorizationWait is the only new public entry
point; the begin signature passes the flow's AbortController instead of its
signal so the flow can abort with a reason. The auth tool's URL update and the
kap-server wire schema carry the new waitId, and the TUI parser narrows its
return type to the two fields it reads.

Verified: MCP suites 378 pass (mcpCore, agent/mcp, app/mcpManagement), oauth
service 42, callback-server 11, TUI mcp-oauth 5; engine/app/kap-server
typechecks clean; oxlint 0 errors, check:no-comments green. Reverse check:
dropping the attachedHandles guard reddens 'keeps a joined completion alive when
the first completing caller aborts', restoring it green. Two lifecycle cases
(joins concurrent begins, detaches a joined caller) failed intermittently with
ECONNREFUSED to the local fake auth server; the same failure reproduces with
service.ts reverted, so it is an existing environment flake, not this change.
7723qqq pushed a commit to 7723qqq/kimi-code that referenced this pull request Nov 4, 2026
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