Skip to content

feat(mcp): lazy server activation (deferLoading + activate_server) - #184

Closed
raymondginger2018-sudo wants to merge 3 commits into
HKUDS:mainfrom
raymondginger2018-sudo:feat/mcp-lazy-server-activation
Closed

feat(mcp): lazy server activation (deferLoading + activate_server)#184
raymondginger2018-sudo wants to merge 3 commits into
HKUDS:mainfrom
raymondginger2018-sudo:feat/mcp-lazy-server-activation

Conversation

@raymondginger2018-sudo

@raymondginger2018-sudo raymondginger2018-sudo commented Aug 17, 2026

Copy link
Copy Markdown
Contributor

特性

MCP 服务器延迟激活(deferLoading + activate_server):多 server 场景下,启动开销不应由未被用到的 server 承担。

改动

  • McpServerDefinition.defer_loading()(即 deferLoading 标记):ensure_started跳过被标记的服务器(状态为 deferred,不注册工具)。
  • McpSessionRuntime.activate_server(server_id):按需启动延迟的服务器,并注册其工具到 ToolRegistry;激活失败时状态置为 failed
  • _register_server_tools / ensure_started 均兼容 activate_server 路径。

动机

参考 CodeWhale 的 MCP 延迟连接设计:未被使用的服务器不应承担启动开销。

测试

  • tests/test_mcp_runtime_lazy.py 5 个用例:deferred 不启动 / activate 注册工具 / 幂等 / 未知 server 返回 False / 激活失败置 failed。

Add McpServerDefinition.defer_loading (deferLoading). Servers marked deferred
are skipped by ensure_started (status stays 'deferred', no connection, no
tools) and are brought up on demand via McpSessionRuntime.activate_server,
which starts the connection, registers the server's tools into the
ToolRegistry, and publishes status/capabilities. Idempotent; startup
failures mark the server 'failed' without raising.

Derived from the MCP lifecycle study of Hmbown/CodeWhale.
@Zongwei9888

Copy link
Copy Markdown
Collaborator

感谢 PR!已在最新 main(合并后)上试合验证:无冲突,你附带的 5 个用例与既有 MCP 运行时测试全部通过(15 passed),全量 1475 passed,ruff 通过。

deferLoading + activate_server 的设计我们认可 —— 按需激活对多 server 场景确实有价值,启动开销不该由未被用到的 server 承担。

两个小建议:

  1. 测试文件建议移到 tests/(仓库惯例是实现放 core/、测试放 tests/),当前在 core/mcp/test_runtime_lazy.py
  2. 本 PR 的描述正文目前显示为乱码,似乎是编码问题,方便的话重新编辑一下,便于其他人评审。

core/mcp/test_runtime_lazy.py sat outside pytest testpaths (tests/ +
quant_trading/tests), so CI never collected the 5 new cases. Move it to
tests/test_mcp_runtime_lazy.py; now collected and run by CI.
@raymondginger2018-sudo

Copy link
Copy Markdown
Contributor Author

Thanks for the review! Both suggestions are addressed:

  1. Test location — moved to tests/test_mcp_runtime_lazy.py. You were right that it mattered more than style: pytest.ini sets testpaths = ["tests", "quant_trading/tests"], so the old core/mcp/test_runtime_lazy.py was never collected by CI — only run manually. It is now picked up by the CI run.
  2. PR description — rewritten (the body was garbled by an encoding issue); it now describes the deferLoading/activate_server design, motivation and the test cases.

@raymondginger2018-sudo

raymondginger2018-sudo commented Aug 22, 2026 via email

Copy link
Copy Markdown
Contributor Author

Zongwei9888 added a commit that referenced this pull request Aug 23, 2026
Integrates repaired versions of #191, #186, #184, #172, and #164 after combined local verification and 14/14 successful CI checks.
pull Bot pushed a commit to ari1988/DeepCode that referenced this pull request Aug 23, 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