#!/usr/bin/env bash # mcp-smoke.sh —— MCP 工具层契约尺子(第十道闸门) # # 测什么:**AI 能不能只靠 4 个元工具,把「找工具 → 看坑 → 执行」跑通**。 # 这是 P1 的唯一 KPI(PLAN.md §6.7)。 # # 用法: # bash scripts/mcp-smoke.sh # 用 debug 产物,自起隔离库 # bash scripts/mcp-smoke.sh --bin # 指定二进制 # # 退出码:0 全过 / 1 有断言失败 / 2 环境错 # # ── 断言(每条一个「不变量」)── # M1 initialize 报出 protocolVersion 与 serverInfo # M2 tools/list **恰好 4 个**元工具(分层加载的硬约束) # M3 业务工具**不出现**在 tools/list(否则 token 预算失效) # M4 通知(无 id)不产生回复 # M5 sspf_context 返回 backend / tables / 能力域 # M6 sspf_discover 无 query ⇒ 分类树;有 query ⇒ 搜索结果 # M7 sspf_describe(level=pitfalls) 返回坑与前置条件 # M8 sspf_call 真执行业务工具并返回 ok=true # M9 sspf_call 未知工具 ⇒ ok=false + TOOL_NOT_FOUND + next_actions(**不是**协议错误) # M10 sspf_call(dry_run) 不产生副作用且能报缺参 # M11 **stdout 必须是纯 NDJSON**(每一行都能被 JSON 解析) # M12 **stderr 不得混入协议 JSON**(防止重定向写反) # # 🔴 M11/M12 是在踩过一次坑之后加的:内嵌 PostgreSQL 会往 fd 1 打 # `PostgreSQL stand-alone backend 17.5` / `backend>`,且它的 single-user 后端 # 会占用 fd 0。内核在开库前用 `dup` 把协议通道整个复制出来(`detach_stdio_for_mcp`), # 这两条断言就是钉住那个行为不被改回去。 set -u ROOT="$(cd "$(dirname "$0")/.." && pwd)" BIN="$ROOT/target/debug/ssw-platform" PASS=0 FAIL=0 while [ $# -gt 0 ]; do case "$1" in --bin) BIN="$2"; shift 2 ;; --help) sed -n '2,32p' "$0"; exit 0 ;; *) echo "未知参数: $1"; exit 2 ;; esac done [ -x "$BIN" ] || { echo "❌ 二进制不存在: $BIN(先 cargo build)"; exit 2; } command -v python3 >/dev/null || { echo "❌ 需要 python3"; exit 2; } ok() { PASS=$((PASS + 1)); printf ' ✅ %s\n' "$1"; } bad() { FAIL=$((FAIL + 1)); printf ' ❌ %s\n' "$1"; } TMP="$(mktemp -d /tmp/sspf-mcp-smoke.XXXXXX)" trap 'rm -rf "$TMP"' EXIT INT TERM REQ="$TMP/req.ndjson" OUT="$TMP/out.ndjson" ERR="$TMP/err.txt" cat > "$REQ" <<'NDJSON' {"jsonrpc":"2.0","id":1,"method":"initialize","params":{}} {"jsonrpc":"2.0","method":"notifications/initialized"} {"jsonrpc":"2.0","id":2,"method":"tools/list"} {"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"sspf_context","arguments":{}}} {"jsonrpc":"2.0","id":4,"method":"tools/call","params":{"name":"sspf_discover","arguments":{}}} {"jsonrpc":"2.0","id":5,"method":"tools/call","params":{"name":"sspf_discover","arguments":{"query":"表"}}} {"jsonrpc":"2.0","id":6,"method":"tools/call","params":{"name":"sspf_describe","arguments":{"tool_id":"schema.table.list","level":"pitfalls"}}} {"jsonrpc":"2.0","id":7,"method":"tools/call","params":{"name":"sspf_call","arguments":{"tool_id":"schema.table.list","args":{}}}} {"jsonrpc":"2.0","id":8,"method":"tools/call","params":{"name":"sspf_call","arguments":{"tool_id":"nope.nope","args":{}}}} {"jsonrpc":"2.0","id":9,"method":"tools/call","params":{"name":"sspf_call","arguments":{"tool_id":"schema.table.describe","args":{},"dry_run":true}}} {"jsonrpc":"2.0","id":10,"method":"ping","params":{}} {"jsonrpc":"2.0","id":11,"method":"tools/call","params":{"name":"sspf_call","arguments":{"tool_id":"permission.user.list","args":{}}}} {"jsonrpc":"2.0","id":12,"method":"tools/call","params":{"name":"sspf_call","arguments":{"tool_id":"permission.role.list","args":{}}}} {"jsonrpc":"2.0","id":13,"method":"tools/call","params":{"name":"sspf_call","arguments":{"tool_id":"dict.type.list","args":{}}}} {"jsonrpc":"2.0","id":14,"method":"tools/call","params":{"name":"sspf_call","arguments":{"tool_id":"data.count","args":{"table":"base_user"}}}} {"jsonrpc":"2.0","id":15,"method":"tools/call","params":{"name":"sspf_call","arguments":{"tool_id":"data.query","args":{"table":"base_user","columns":["f_account"],"limit":3}}}} {"jsonrpc":"2.0","id":16,"method":"tools/call","params":{"name":"sspf_call","arguments":{"tool_id":"schema.index.list","args":{"table":"base_user"}}}} {"jsonrpc":"2.0","id":17,"method":"tools/call","params":{"name":"sspf_call","arguments":{"tool_id":"data.query","args":{"table":"base_user","where":[{"column":"f_account; DROP TABLE x","op":"eq","value":"a"}]}}}} {"jsonrpc":"2.0","id":18,"method":"tools/call","params":{"name":"sspf_call","arguments":{"tool_id":"menu.create","args":{"fullName":"冒烟菜单","enCode":"permission.smoke.page","urlAddress":"smoke/Page"}}}} {"jsonrpc":"2.0","id":19,"method":"tools/call","params":{"name":"sspf_call","arguments":{"tool_id":"menu.create","args":{"fullName":"重复","enCode":"permission.smoke.page","urlAddress":"smoke/Other"}}}} {"jsonrpc":"2.0","id":20,"method":"tools/call","params":{"name":"sspf_call","arguments":{"tool_id":"menu.create","args":{"fullName":"坏路径","enCode":"permission.smoke.bad","urlAddress":"/bad/Path"}}}} {"jsonrpc":"2.0","id":21,"method":"tools/call","params":{"name":"sspf_call","arguments":{"tool_id":"permission.authorize.grant","args":{"objectType":"role","objectId":"role_user","menuEnCode":"permission.smoke.page"}}}} {"jsonrpc":"2.0","id":22,"method":"tools/call","params":{"name":"sspf_call","arguments":{"tool_id":"permission.user.create","args":{"account":"smoke_user","realName":"冒烟用户","password":"Smoke@123","roleIds":["role_user"]}}}} {"jsonrpc":"2.0","id":23,"method":"tools/call","params":{"name":"sspf_call","arguments":{"tool_id":"permission.effective","args":{"account":"smoke_user","withMenuDetail":true}}}} {"jsonrpc":"2.0","id":24,"method":"tools/call","params":{"name":"sspf_call","arguments":{"tool_id":"permission.authorize.revoke","args":{"objectType":"role","objectId":"role_user","menuEnCode":"permission.smoke.page"}}}} {"jsonrpc":"2.0","id":25,"method":"tools/call","params":{"name":"sspf_call","arguments":{"tool_id":"permission.effective","args":{"account":"smoke_user"}}}} {"jsonrpc":"2.0","id":26,"method":"tools/call","params":{"name":"sspf_call","arguments":{"tool_id":"permission.authorize.revoke","args":{"objectType":"role","objectId":"role_user","menuEnCode":"permission.smoke.page"}}}} {"jsonrpc":"2.0","id":27,"method":"prompts/list","params":{}} {"jsonrpc":"2.0","id":28,"method":"prompts/get","params":{"name":"new_business_module","arguments":{"module_name":"冒烟","module_code":"smoke"}}} {"jsonrpc":"2.0","id":29,"method":"prompts/get","params":{"name":"diagnose_menu_not_visible","arguments":{}}} NDJSON echo "── MCP 工具层契约尺子(4 元工具 + 分层加载)──" PGLITE_DIR="$TMP/db" "$BIN" --mcp < "$REQ" > "$OUT" 2> "$ERR" RC=$? if [ "$RC" -ne 0 ]; then echo "❌ MCP 进程退出码 $RC(期望 0)" echo " stderr 尾部:" tail -8 "$ERR" | sed 's/^/ /' exit 2 fi python3 - "$OUT" "$ERR" <<'PY' import json, sys out_path, err_path = sys.argv[1], sys.argv[2] PASS = FAIL = 0 def ok(m): global PASS; PASS += 1; print(f" ✅ {m}") def bad(m): global FAIL; FAIL += 1; print(f" ❌ {m}") raw = open(out_path, encoding='utf-8', errors='replace').read() lines = [l for l in raw.splitlines() if l.strip()] # ---- M11 stdout 必须是纯 NDJSON ---- parsed = {} bad_lines = [] for i, line in enumerate(lines, 1): try: m = json.loads(line) except Exception: bad_lines.append((i, line[:80])) continue if m.get("id") is not None: parsed[m["id"]] = m if bad_lines: bad(f"M11 stdout 有 {len(bad_lines)} 行不是合法 JSON(NDJSON 被污染)") for i, s in bad_lines[:3]: print(f" 第{i}行: {s}") else: ok(f"M11 stdout 是纯 NDJSON({len(lines)} 行全部可解析)") # ---- M12 stderr 不得混协议 ---- err = open(err_path, encoding='utf-8', errors='replace').read() if '"jsonrpc"' in err: bad("M12 stderr 混入了协议 JSON(重定向写反了)") else: ok("M12 stderr 无协议 JSON(日志与协议分离)") def result(rid): return parsed.get(rid, {}).get("result") # ---- M1 initialize ---- r = result(1) if r and r.get("protocolVersion") and r.get("serverInfo", {}).get("name") == "sspf-mcp": ok(f"M1 initialize 报出 protocol={r['protocolVersion']} server={r['serverInfo']['name']}") else: bad("M1 initialize 响应不完整") # ---- M2 恰好 4 个元工具 ---- tools = (result(2) or {}).get("tools", []) names = [t.get("name") for t in tools] expect = ["sspf_context", "sspf_discover", "sspf_describe", "sspf_call"] if len(tools) == 4 and sorted(names) == sorted(expect): ok("M2 tools/list 恰好 4 个元工具") else: bad(f"M2 tools/list 应为 4 个元工具,实际 {len(tools)}: {names}") # ---- M3 业务工具不出现 ---- blob = json.dumps(result(2) or {}, ensure_ascii=False) if "context.get" in blob or "schema.table.list" in blob: bad("M3 业务工具泄漏到 tools/list(分层加载失效)") else: ok("M3 业务工具不在 tools/list(只能经 discover 访问)") # ---- M4 通知不回复 ---- if len(parsed) == 29: ok("M4 29 条带 id 的请求各一个回复;通知无回复") else: bad(f"M4 回复数 {len(parsed)},期望 29(通知不该有回复)") # ---- M5 context ---- sc = (result(3) or {}).get("structuredContent", {}) if sc.get("backend") == "native" and isinstance(sc.get("tables"), int) and sc["tables"] > 0: ok(f"M5 sspf_context: backend=native tables={sc['tables']} 能力域={len(sc.get('domains', []))}") else: bad(f"M5 sspf_context 异常: {json.dumps(sc, ensure_ascii=False)[:120]}") # ---- M6 discover 两种模式 ---- d0 = (result(4) or {}).get("structuredContent", {}) d1 = (result(5) or {}).get("structuredContent", {}) if d0.get("mode") == "categories" and isinstance(d0.get("categories"), list): ok(f"M6a discover 无 query ⇒ 分类树({len(d0['categories'])} 类)") else: bad("M6a discover 无 query 应返回分类树") if d1.get("mode") == "search" and isinstance(d1.get("results"), list): ok(f"M6b discover(query=表) ⇒ 搜索结果 {d1.get('count')} 条") else: bad("M6b discover 带 query 应返回搜索结果") # ---- M7 describe pitfalls ---- dp = (result(6) or {}).get("structuredContent", {}) if dp.get("ok") and isinstance(dp.get("pitfalls"), list) and len(dp["pitfalls"]) > 0: ok(f"M7 describe(pitfalls) 返回 {len(dp['pitfalls'])} 个坑") else: bad("M7 describe(pitfalls) 应返回坑清单") # ---- M8 call 真执行 ---- c1 = (result(7) or {}).get("structuredContent", {}) if c1.get("ok") and c1.get("data", {}).get("count", 0) > 0: ok(f"M8 sspf_call 执行业务工具成功(表数 {c1['data']['count']})") else: bad(f"M8 sspf_call 执行失败: {json.dumps(c1, ensure_ascii=False)[:120]}") # ---- M9 未知工具 ⇒ 结构化错误(不是协议错误)---- r9 = parsed.get(8, {}) c2 = (r9.get("result") or {}).get("structuredContent", {}) if "error" not in r9 and c2.get("ok") is False \ and c2.get("error", {}).get("code") == "TOOL_NOT_FOUND" \ and isinstance(c2["error"].get("next_actions"), list): ok("M9 未知工具 ⇒ ok=false + TOOL_NOT_FOUND + next_actions(业务失败未变成协议错误)") else: bad(f"M9 未知工具应返回结构化业务错误,实际: {json.dumps(r9, ensure_ascii=False)[:160]}") # ---- M10 dry_run ---- c3 = (result(9) or {}).get("structuredContent", {}) if c3.get("dry_run") is True and isinstance(c3.get("problems"), list) and len(c3["problems"]) > 0: ok(f"M10 dry_run 报出缺失必填参数({len(c3['problems'])} 项)") else: bad(f"M10 dry_run 应报缺参,实际: {json.dumps(c3, ensure_ascii=False)[:120]}") def payload(rid): """取 sspf_call 的 data 段(业务工具的真实返回)。""" return ((result(rid) or {}).get("structuredContent") or {}).get("data", {}) # ---- M13~M16 四域只读工具 ---- u = payload(11) if isinstance(u.get("users"), list) and "password" not in json.dumps(u).lower(): ok(f"M13 permission.user.list 返回 {u.get('count')} 个用户且**不含口令字段**") else: bad(f"M13 permission.user.list 异常或泄漏口令: {json.dumps(u, ensure_ascii=False)[:120]}") r = payload(12) if isinstance(r.get("roles"), list) and r.get("count", 0) >= 1: ok(f"M14 permission.role.list 返回 {r.get('count')} 个角色") else: bad("M14 permission.role.list 应至少有一个角色(种子里有 admin)") dt = payload(13) if isinstance(dt.get("types"), list): ok(f"M15 dict.type.list 返回 {dt.get('count')} 个字典分类") else: bad("M15 dict.type.list 返回形状不对") dc = payload(14) if isinstance(dc.get("count"), int): ok(f"M16 data.count(base_user) = {dc['count']}") else: bad(f"M16 data.count 应返回整数 count,实际: {json.dumps(dc, ensure_ascii=False)[:100]}") # ---- M17 列白名单确实生效(只返回请求的列;键是去 f_ 前缀的 camelCase)---- dq = payload(15) rows = dq.get("rows") or [] if isinstance(rows, list) and rows and all(set(row.keys()) == {"account"} for row in rows): ok(f"M17 data.query 只返回白名单内的列({len(rows)} 行 × 1 列,键已转 camelCase)") else: bad(f"M17 data.query 列裁剪失效: {json.dumps(rows[:1], ensure_ascii=False)[:120]}") # ---- M18 索引清单 ---- ix = payload(16) if isinstance(ix.get("indexes"), list) and ix.get("count", 0) >= 1: ok(f"M18 schema.index.list 返回 {ix.get('count')} 条索引") else: bad("M18 schema.index.list 至少应有主键索引") # ---- M19 🔴 结构注入防护:列名里带 SQL 必须被拒 ---- r17 = parsed.get(17, {}) c17 = (r17.get("result") or {}).get("structuredContent", {}) if c17.get("ok") is False and c17.get("error", {}).get("code") == "UNKNOWN_COLUMN": ok("M19 列名带 SQL 片段 ⇒ UNKNOWN_COLUMN(结构注入被挡)") else: bad(f"M19 结构注入未被挡住: {json.dumps(c17, ensure_ascii=False)[:160]}") # ---- M20~M22 写工具(建 → 重复拒 → 坏路径拒)---- w1 = (result(18) or {}).get("structuredContent", {}) wd = w1.get("data", {}) if w1.get("ok") is True and wd.get("id") and wd.get("urlAddress") == "smoke/Page": ok(f"M20 menu.create 成功(id={str(wd['id'])[:8]}…)") else: bad(f"M20 menu.create 失败: {json.dumps(w1, ensure_ascii=False)[:140]}") w2 = (result(19) or {}).get("structuredContent", {}) we = w2.get("error", {}) if w2.get("ok") is False and we.get("code") == "MENU_EXISTS": ok("M21 重复 enCode ⇒ MENU_EXISTS(唯一性应用层兜住)") else: bad(f"M21 重复建未被拒: {json.dumps(w2, ensure_ascii=False)[:140]}") w3 = (result(20) or {}).get("structuredContent", {}) w3e = w3.get("error", {}) if w3.get("ok") is False and w3e.get("code") == "BAD_URL_ADDRESS": ok("M22 urlAddress 带前导斜杠 ⇒ BAD_URL_ADDRESS") else: bad(f"M22 坏路径未被拒: {json.dumps(w3, ensure_ascii=False)[:140]}") # ---- M23~M30 授权闭环(建菜单后的最后一公里)---- g = (result(21) or {}).get("structuredContent", {}) if g.get("ok") is True and g.get("data", {}).get("granted") is True: ok("M23 permission.authorize.grant 授给角色成功") else: bad(f"M23 授权失败: {json.dumps(g, ensure_ascii=False)[:160]}") u = (result(22) or {}).get("structuredContent", {}) if u.get("ok") is True and u.get("data", {}).get("isAdministrator") is False: ok("M24 user.create 建出的是**非管理员**账号(能测出授权问题)") else: bad(f"M24 建用户失败: {json.dumps(u, ensure_ascii=False)[:160]}") e1 = (result(23) or {}).get("structuredContent", {}).get("data", {}) vis1 = [m.get("urlAddress") for m in (e1.get("visible") or {}).get("menus", []) if isinstance(m, dict)] src1 = (e1.get("sources") or {}).get("role", {}).get("menuIds") or [] if e1.get("isAdmin") is False and "smoke/Page" in vis1 and src1: ok(f"M25 effective 看到授权的菜单(visible={e1['visible']['count']},来源=role)") else: bad(f"M25 授权后仍看不到: visible={vis1} isAdmin={e1.get('isAdmin')}") rv = (result(24) or {}).get("structuredContent", {}) if rv.get("ok") is True and rv.get("data", {}).get("revoked") is True: ok("M26 revoke 撤权成功(软删)") else: bad(f"M26 撤权失败: {json.dumps(rv, ensure_ascii=False)[:160]}") e2 = (result(25) or {}).get("structuredContent", {}).get("data", {}) vis2 = [m.get("urlAddress") for m in (e2.get("visible") or {}).get("menus", []) if isinstance(m, dict)] if "smoke/Page" not in vis2: ok("M27 撤权后 effective 立刻看不到(四源并集真的变了)") else: bad(f"M27 撤权后仍可见: {vis2}") e3 = (result(26) or {}).get("structuredContent", {}) if e3.get("ok") is False and e3.get("error", {}).get("code") == "AUTHORIZE_NOT_FOUND": ok("M28 重复撤权 ⇒ AUTHORIZE_NOT_FOUND(不静默成功)") else: bad(f"M28 重复撤权未被拒: {json.dumps(e3, ensure_ascii=False)[:160]}") pl = result(27) or {} names = [x.get("name") for x in (pl.get("prompts") or [])] if len(names) >= 3 and "new_business_module" in names: ok(f"M29 prompts/list 返回 {len(names)} 个模板(含 new_business_module)") else: bad(f"M29 prompts/list 异常: {names}") pg = result(28) or {} ptext = ((pg.get("messages") or [{}])[0].get("content") or {}).get("text", "") if "非管理员" in ptext and "menu.create" in ptext and "permission.authorize.grant" in ptext: ok("M30 prompts/get 正文含关键步骤(建菜单 + 授权 + 非管理员验证)") else: bad(f"M30 模板正文缺关键步骤(长度 {len(ptext)})") # 缺参时是 JSON-RPC error(没有 result),所以要拿整条响应 pm = parsed.get(29, {}) if pm.get("error", {}).get("code") == -32602: ok("M31 模板缺必填参数 ⇒ INVALID_PARAMS(不渲染带占位符的正文)") else: bad(f"M31 缺参未被拦: {json.dumps(pm, ensure_ascii=False)[:140]}") print() print(f"PASS={PASS} FAIL={FAIL}") sys.exit(0 if FAIL == 0 else 1) PY RC=$? echo "--------------------" if [ "$RC" -eq 0 ]; then echo "全绿 ✅(MCP 工具层契约闭环)" else echo "有 ❌,按上面修" fi exit "$RC"