Feature(Go): verify_tools --subset and MCP deployment docs
Add a third mode to verify_tools.py that validates the Go server end to end: it fetches /mcp tools/list and requires the name set to equal the nine implemented tools exactly (seven in readonly mode), compares description / inputSchema / outputSchema field by field against mcp/tools.json, and also checks the published order. Unlike --live, extra or missing tools are failures, not notes. The field-comparison loop is factored into report_field_diffs so --offline / --live and --subset share one comparison, diff printer and FIELDS tuple. Document and deploy the endpoint: the systemd env example gains YAMA_MCP_TOKEN / READONLY / TERM_IDLE / MAX_CONCURRENCY / READ_MAX, the design doc moves to implemented and its section 10 now matches the code (embedded tools_minimal.json, the three packet dispatch groups). The tools README explains gen_go_tools.py --check and --subset. Verified against a freshly built debug Go server on 16543/18090: read-write mode passes with 9 tools, YAMA_MCP_READONLY=1 passes with 7, and both mismatch directions (extra tools, missing tools) fail with exit 1. gen_go_tools.py --check stays clean; no Go code changed. Co-Authored-By: deepseek-flash
This commit is contained in:
1 parent
9ce1a58bc3
commit
d4f10db1f2
4 files changed
+192
-39
No files matched your search
+123
-18
@@ -1,6 +1,6 @@
|
||||
"""校验 tools.json 与 MCP 服务端的 tools/list 是否一致。
|
||||
|
||||
两种模式:
|
||||
三种模式:
|
||||
python verify_tools.py --offline mcp/tools.json tools_raw.json
|
||||
对比 tools.json 与已保存的黄金样本(不联网)
|
||||
|
||||
@@ -9,6 +9,12 @@
|
||||
token 省略时依次取环境变量 YAMA_MCP_TOKEN、注册表 McpToken;
|
||||
port 省略时自动探测 6545(明文后端)/ 6544(TLS 前端)
|
||||
|
||||
python verify_tools.py --subset mcp/tools.json [token] [port] [--readonly]
|
||||
校验 Go 服务端(server/go)的 MCP 端点:名字集合必须恰好是它实现的那
|
||||
9 个工具(只读模式 7 个),且 description / inputSchema / outputSchema
|
||||
与 tools.json 里的同名工具逐字段一致。Go 端把 tools_minimal.json 内嵌
|
||||
进二进制,这一模式检查的就是线上真正发布的字节。
|
||||
|
||||
比对内容:name / description / inputSchema / outputSchema,逐字段深度比较,
|
||||
外加工具顺序。gate / write / manage / impl 是 tools.json 独有的元信息,不参与比对;
|
||||
例外是 gate == "!machine" 的降级工具——它们只在 McpMachine=0 时出现,不进本次比对。
|
||||
@@ -19,6 +25,24 @@ import os
|
||||
import ssl
|
||||
import sys
|
||||
|
||||
# Go 最小服务端实现的 9 个工具(docs/Mcp_Go_Minimal_Design.md §3.1)。
|
||||
# tools/list 恰好是这些,多一个少一个都算失败。
|
||||
GO_MINIMAL_TOOLS = (
|
||||
"list_hosts",
|
||||
"get_host_detail",
|
||||
"get_screenshot",
|
||||
"list_files",
|
||||
"get_audit_log",
|
||||
"get_server_status",
|
||||
"terminal_exec",
|
||||
"terminal_close",
|
||||
"read_file",
|
||||
)
|
||||
# YAMA_MCP_READONLY=1 时 Go 端隐藏的写工具(terminal_* 会创建/关闭会话)。
|
||||
GO_READONLY_HIDDEN = ("terminal_exec", "terminal_close")
|
||||
|
||||
FIELDS = ("description", "inputSchema", "outputSchema")
|
||||
|
||||
|
||||
def normalize(obj):
|
||||
"""规范化:键排序后序列化,消除 JSON object 的键序差异。"""
|
||||
@@ -91,11 +115,107 @@ def fetch_live(token, port=None):
|
||||
sys.exit(f"连接失败 -> {last}")
|
||||
|
||||
|
||||
def report_field_diffs(live_by_name, golden_by_name, names):
|
||||
"""逐字段比较同名工具,返回差异处数;一个工具最多报一处。"""
|
||||
diffs = 0
|
||||
for name in names:
|
||||
a, b = live_by_name[name], golden_by_name[name]
|
||||
for f in FIELDS:
|
||||
va, vb = normalize(a.get(f)), normalize(b.get(f))
|
||||
if va != vb:
|
||||
diffs += 1
|
||||
print(f"!! {name}.{f} 不一致")
|
||||
for i, (ca, cb) in enumerate(zip(va, vb)):
|
||||
if ca != cb:
|
||||
print(f" @{i}")
|
||||
print(f" 服务端: ...{va[max(0,i-60):i+80]}...")
|
||||
print(f" json : ...{vb[max(0,i-60):i+80]}...")
|
||||
break
|
||||
else:
|
||||
print(f" 长度不同: {len(va)} vs {len(vb)}")
|
||||
break
|
||||
return diffs
|
||||
|
||||
|
||||
def verify_subset(path, token, port, readonly):
|
||||
"""Go 最小服务端:名字集合恰好是 GO_MINIMAL_TOOLS,schema 与 tools.json 一致。"""
|
||||
expected = [n for n in GO_MINIMAL_TOOLS
|
||||
if not (readonly and n in GO_READONLY_HIDDEN)]
|
||||
tools_live, live_stats = fetch_live(token, port)
|
||||
print(f"tools.json: {path}")
|
||||
print(f"模式: {'只读(隐藏 ' + ' / '.join(GO_READONLY_HIDDEN) + ')' if readonly else '读写'}")
|
||||
print(f"期望: {len(expected)} 个工具 / 服务端: {len(tools_live)} 个")
|
||||
|
||||
live_names = [t["name"] for t in tools_live]
|
||||
expected_set, live_set = set(expected), set(live_names)
|
||||
diffs = 0
|
||||
extra = sorted(live_set - expected_set)
|
||||
missing = [n for n in expected if n not in live_set]
|
||||
if extra:
|
||||
diffs += len(extra)
|
||||
print(f"!! 服务端多出: {extra}")
|
||||
if missing:
|
||||
diffs += len(missing)
|
||||
print(f"!! 服务端缺少: {missing}")
|
||||
dupes = sorted({n for n in live_names if live_names.count(n) > 1})
|
||||
if dupes:
|
||||
diffs += len(dupes)
|
||||
print(f"!! 服务端重复: {dupes}")
|
||||
|
||||
# 顺序一致性:tools/list 的数组顺序会影响模型的选择倾向
|
||||
if not extra and not missing and not dupes:
|
||||
order = [n for n in live_names if n in expected_set]
|
||||
if order != expected:
|
||||
diffs += 1
|
||||
print("!! 工具顺序与 tools.json 不一致")
|
||||
for i, (x, y) in enumerate(zip(order, expected)):
|
||||
if x != y:
|
||||
print(f" 首个差异 @{i}: 服务端={x} 期望={y}")
|
||||
break
|
||||
else:
|
||||
print("工具顺序: 一致")
|
||||
|
||||
doc = json.load(open(path, encoding="utf-8"))
|
||||
tools_json = doc["tools"] if isinstance(doc, dict) else doc
|
||||
golden_by_name = {t["name"]: t for t in tools_json}
|
||||
live_by_name = {t["name"]: t for t in tools_live}
|
||||
|
||||
comparable = []
|
||||
for name in expected:
|
||||
if name not in golden_by_name:
|
||||
diffs += 1
|
||||
print(f"!! tools.json 缺少工具 {name}")
|
||||
elif name in live_by_name:
|
||||
comparable.append(name)
|
||||
diffs += report_field_diffs(live_by_name, golden_by_name, comparable)
|
||||
|
||||
print(f" 体积: 响应 {live_stats['response_bytes']} B / tools 数组 "
|
||||
f"{live_stats['tools_array_bytes']} B")
|
||||
if diffs == 0:
|
||||
print(f"\nPASS: Go 服务端 {len(expected)} 个工具与 tools.json 完全一致")
|
||||
return 0
|
||||
print(f"\nFAIL: {diffs} 处差异")
|
||||
return 1
|
||||
|
||||
|
||||
def main():
|
||||
args = sys.argv[1:]
|
||||
if not args:
|
||||
sys.exit(__doc__)
|
||||
|
||||
if args[0] == "--subset":
|
||||
argv = [a for a in args[1:]]
|
||||
readonly = "--readonly" in argv
|
||||
argv = [a for a in argv if a != "--readonly"]
|
||||
if not argv:
|
||||
sys.exit("用法: python verify_tools.py --subset <tools.json> [token] [port] [--readonly]")
|
||||
path = argv[0]
|
||||
token = resolve_token(argv[1] if len(argv) > 1 else "")
|
||||
port = int(argv[2]) if len(argv) > 2 else None
|
||||
if not token:
|
||||
sys.exit("未找到 McpToken(参数 / YAMA_MCP_TOKEN / 注册表 均无)")
|
||||
return verify_subset(path, token, port, readonly)
|
||||
|
||||
if args[0] == "--offline":
|
||||
if len(args) < 3:
|
||||
sys.exit("用法: python verify_tools.py --offline <tools.json> <黄金样本.json>")
|
||||
@@ -142,24 +262,9 @@ def main():
|
||||
if only_json:
|
||||
print(f"!! 仅 json 有: {only_json}")
|
||||
|
||||
FIELDS = ("description", "inputSchema", "outputSchema")
|
||||
diffs = 0
|
||||
for name in sorted(set(live_by_name) & set(json_by_name)):
|
||||
a, b = live_by_name[name], json_by_name[name]
|
||||
for f in FIELDS:
|
||||
va, vb = normalize(a.get(f)), normalize(b.get(f))
|
||||
if va != vb:
|
||||
diffs += 1
|
||||
print(f"!! {name}.{f} 不一致")
|
||||
for i, (ca, cb) in enumerate(zip(va, vb)):
|
||||
if ca != cb:
|
||||
print(f" @{i}")
|
||||
print(f" 服务端: ...{va[max(0,i-60):i+80]}...")
|
||||
print(f" json : ...{vb[max(0,i-60):i+80]}...")
|
||||
break
|
||||
else:
|
||||
print(f" 长度不同: {len(va)} vs {len(vb)}")
|
||||
break # 一个工具报一次即可
|
||||
FIELDS_SHARED = sorted(set(live_by_name) & set(json_by_name))
|
||||
diffs += report_field_diffs(live_by_name, json_by_name, FIELDS_SHARED)
|
||||
|
||||
# 顺序一致性:tools/list 的数组顺序会影响模型的选择倾向
|
||||
order_live = [t["name"] for t in tools_live]
|
||||
|
||||
Reference in new issue
Block a user