perf(agents): MCP 工具并发加载,多 MCP server 启动耗时从 sum 降到 max - #1012
Merged
Conversation
Owner
|
Codex Review: 仅供参考:本轮预 review 基于 本轮未发现可确认的功能缺陷。检查了实际 MCP loader:数据库读取各自使用 session,网络工具发现没有被全局 实际验证:在该 head 独立源码快照运行 建议把并发屏障/排序/取消的回归用例加入 PR;现有改动没有新增测试。性能数字目前仍是 PR 自述,本次没有连接真实 MCP server 测冷缓存启动耗时,不能据 mock 探针确认“1–2 秒降到单个最慢 server”。 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
现象
智能体启动时,配置了多个 MCP server 的情况下,工具加载是串行的:每个 server 逐个
await get_enabled_mcp_tools。单个 server 加载 33-300ms,6 个串行累加就是 1-2 秒——都花在智能体启动阶段,用户感知为「发出去后要等一下才开始」。机理
resolve_configured_runtime_tools里对context.mcps逐个串行 await:各 MCP server 之间互相独立,串行没有道理,总耗时是 sum 而非 max。
改进方法
改成
asyncio.gather并发加载全部 MCP server 的工具,取 max 而非 sum:asyncio.gather并发加载;(name, None)代替continue,保持容错语义)。这是上游 deepagents-code #4659 同款优化(deepagents 官方已采用并发加载)。
效果
多 MCP server 场景下,工具加载耗时从 sum 降到 max(6 个 server 约 1-2s → 单次最慢 server 耗时)。单 server 失败容错语义不变。