feat(bridge): 新增 stdio↔TCP 桥,让标准 MCP 客户端可直连 CAD 基座
标准 MCP 客户端(Claude Desktop / CodeBuddy / MCP Inspector)只实现 stdio 与 Streamable HTTP,无法直连 cad_mcp_frame 的自定义裸 TCP。新增 mcp_bridge 包在 客户端与基座之间做协议适配,CAD 侧零改动: - 合成基座缺失的生命周期:initialize(含协议版本协商、capabilities、serverInfo) 与 ping;notifications/* 按规范不回包。 - tools/list、tools/call 原样转发到 127.0.0.1:8080,result 与顶层 error 忠实透传, 仅把 id 改写回客户端原始 id。 - 并发:读线程即时应答 initialize/ping,单工作线程串行转发工具调用(保证顺序、 慢工具不阻塞 ping)。 - stdout 只走 sys.stdout.buffer 并加锁,规避 Windows 的 \n→\r\n 翻译; 诊断信息一律走 stderr。 - CAD 未启动时桥照常启动,工具调用回 -32603(可重试)。 - 入口:python -m mcp_bridge,支持 --host/--port/--timeout/--verbose/--print-config。 dwg_review/mcp_client.py 增加 raw_rpc()(_rpc 新增 normalize 开关), 返回基座原始响应 dict 供桥透传,避免重复实现 TCP 封装。 验证: - verify_stdio_bridge.py 24/24 PASS(版本协商、通知不回包、ping、tools/list 与基座 等价、tools/call、错误透传、-32700、string id、stdout 纯净性、死端口降级)。 - 真实标准客户端 MCP Inspector 冒烟通过:tools/list 拿到 26 个工具, tools/list --strict 退出码 0(无 schema 可移植性问题), tools/call get_drawing_info 返回标准 content + isError:false。 - 回归:verify_mcp_frame.py 4/4、verify_annotate.py 9/9、dwg_review --list-tools/--check 正常。
This commit is contained in:
1 parent
e56ce09caf
commit
f33d42afe9
9 files changed
+660
-2
No files matched your search
@@ -112,7 +112,8 @@ class McpClient:
|
||||
# ------------------------------------------------------------------
|
||||
# 高层 RPC
|
||||
# ------------------------------------------------------------------
|
||||
def _rpc(self, method, params=None, *, timeout=None, retries=None, is_write=False):
|
||||
def _rpc(self, method, params=None, *, timeout=None, retries=None, is_write=False,
|
||||
normalize=True):
|
||||
retries = config.CALL_RETRIES if retries is None else retries
|
||||
if is_write:
|
||||
retries = 0 # 写类工具不重试,避免重复副作用
|
||||
@@ -126,7 +127,8 @@ class McpClient:
|
||||
if params is not None:
|
||||
payload["params"] = params
|
||||
try:
|
||||
return self._normalize(self._send_frame(payload, timeout))
|
||||
resp = self._send_frame(payload, timeout)
|
||||
return self._normalize(resp) if normalize else resp
|
||||
except (McpError, McpToolError):
|
||||
raise # 协议/工具错误是确定性的,不重试
|
||||
except (OSError, socket.timeout) as exc:
|
||||
@@ -156,6 +158,14 @@ class McpClient:
|
||||
params = {"name": name, "arguments": arguments or {}}
|
||||
return self._rpc("tools/call", params, timeout=timeout, retries=retries, is_write=is_write)
|
||||
|
||||
def raw_rpc(self, method, params=None, *, timeout=None, retries=None):
|
||||
"""转发式调用:返回基座**原始响应 dict**,不做 error / isError 归一化。
|
||||
|
||||
供 stdio 桥使用——桥需要把基座的 `result` 与顶层 `error` 原样透传给客户端,
|
||||
因此不能走 `_normalize`(它会把两者都变成异常)。
|
||||
"""
|
||||
return self._rpc(method, params, timeout=timeout, retries=retries, normalize=False)
|
||||
|
||||
def list_tools(self, *, use_cache=True):
|
||||
"""返回 tools/list 的 tools 数组。"""
|
||||
if use_cache and self._tools_cache is not None:
|
||||
|
||||
Reference in new issue
Block a user