基座(cad_mcp_frame): - 新增 RecvOneFrame: 以 '\n' 为消息边界分帧,去掉单次 recv(2048) 限制(上限 8MB); 用 select 短探测兼容不发 '\n' 的旧客户端;clientSocket 设 SO_RCVTIMEO 防卸载卡死 - JSON-RPC id 支持 string/number;无 id 的通知不回包 - isError 映射: 协议级错误保留顶层 error,工具执行错误转 result.isError=true - 响应补 '\n' 作为消息边界 Agent(新增 dwg_review 包): - mcp_client: 唯一 TCP/JSON-RPC 封装(单飞锁/分帧/重试/兼容 error 与 isError) - cad_tools: 从 tools/list 动态建表,含 15 个只读白名单 - phases: planning -> evidence -> review(受控只读取证子循环)-> report - annotator: 批注回写 AI_REVIEW 层,支持 dry-run 与撤销 - config: 配置全部走环境变量;Windows 下回退读注册表以读到新设的系统变量 其它: 新增 verify_mcp_frame.py 验证脚本;.gitignore 补 Python/AutoCAD 锁文件规则; ai.memory 记录架构决策与实测结论
81 lines
2.4 KiB
Python
81 lines
2.4 KiB
Python
"""工具表:从 tools/list 动态构建,新增工具无需改代码。"""
|
|
|
|
from .mcp_client import McpClient
|
|
|
|
# 只读工具白名单:审查阶段的受控子循环只允许调用这些(不做任何写库操作)
|
|
READ_ONLY_TOOLS = frozenset(
|
|
{
|
|
"query_entities",
|
|
"get_text_content",
|
|
"get_dimensions",
|
|
"get_block_references",
|
|
"get_polylines_info",
|
|
"get_entity_detail",
|
|
"get_layers",
|
|
"get_layouts",
|
|
"get_drawing_info",
|
|
"detect_drawing_frames",
|
|
"find_duplicate_entities",
|
|
"find_zero_length",
|
|
"zoom_to_window",
|
|
"zoom_to_entity",
|
|
"get_viewport_screenshot",
|
|
}
|
|
)
|
|
|
|
# 写类工具前缀/名称:调用时不重试,且默认禁止 LLM 在审查阶段触发
|
|
_WRITE_PREFIXES = ("add_", "delete_", "draw_", "set_")
|
|
_WRITE_EXACT = {"set_entity_properties", "highlight_entities"}
|
|
|
|
|
|
def is_write_tool(name: str) -> bool:
|
|
return name.startswith(_WRITE_PREFIXES) or name in _WRITE_EXACT
|
|
|
|
|
|
class CadTools:
|
|
"""工具注册表 + 调用代理。"""
|
|
|
|
def __init__(self, client=None):
|
|
self.client = client or McpClient()
|
|
self._by_name = {}
|
|
self.refresh()
|
|
|
|
def refresh(self):
|
|
tools = self.client.list_tools(use_cache=False)
|
|
self._by_name = {t["name"]: t for t in tools}
|
|
return tools
|
|
|
|
@property
|
|
def names(self):
|
|
return sorted(self._by_name)
|
|
|
|
def has(self, name):
|
|
return name in self._by_name
|
|
|
|
def describe(self, name):
|
|
return self._by_name.get(name)
|
|
|
|
def openai_tools(self, names=None):
|
|
"""转换为 OpenAI function 工具格式;names 为可选的名称白名单集合。"""
|
|
out = []
|
|
for name, tool in self._by_name.items():
|
|
if names is not None and name not in names:
|
|
continue
|
|
out.append(
|
|
{
|
|
"type": "function",
|
|
"function": {
|
|
"name": name,
|
|
"description": tool.get("description", ""),
|
|
"parameters": tool.get("inputSchema")
|
|
or {"type": "object", "properties": {}},
|
|
},
|
|
}
|
|
)
|
|
return out
|
|
|
|
def call(self, name, arguments=None, *, is_write=None):
|
|
if is_write is None:
|
|
is_write = is_write_tool(name)
|
|
return self.client.call_tool(name, arguments, is_write=is_write)
|