Files
SimpleRemoter/tools/mcp_tools/check_platform_table.py
T
yuanyuanxianganddeepseek-flash fd1b085279 Feature: Add remote lock screen for Android hosts
Implement the Ctrl+Alt+Del path on Android as a system lock screen.
Android has no secure attention sequence, so COMMAND_SCREEN_CTRL_ALT_DEL
is mapped in the client to GLOBAL_ACTION_LOCK_SCREEN and the server
exposes it as the new MCP tool remote_lock_screen.

The Android client resolves ControlService.lockScreen()/canLockScreen()
in nativeInit and dispatches the command from the screen sub-connection
through the usual attach/detach JNI path. The lock action is posted to
the accessibility service handler because performGlobalAction must not
run on the receiver thread.

Login advertises the new CLIENT_CAP_LOCK_SCREEN bit only when the
accessibility service is bound and the API level is 28+, so the server
can reject old APKs and Windows/Linux hosts up front instead of
reporting a lock that never happened. There is no receipt: a successful
call only means the command was delivered, and the tool description
tells the caller to confirm with get_screenshot.

Server side adds the schema and handler next to remote_clipboard, audits
the call, and gates it on client type plus the capability bit. Since it
is the only APK-only tool, check_platform_table.py gains a documented
exemption from the Windows baseline invariant, and make_tools_json.py
learns an APK-only platform set. tools.json is regenerated from the live
server and the plan document now counts 62 tools.

Co-Authored-By: deepseek-flash
2026-11-11 20:59:10 +01:00

213 lines
8.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""校验 C++ 的 kToolPlatforms 表与 mcp/tools.json 的 platforms 字段是否一致。
C++ 侧不读 JSON(当前架构如此),两边是双份维护。这个脚本负责让它们不漂移:
- 工具集合必须完全相同(漏登记 / 多登记都报错)
- 每个工具的掩码必须对应同一组平台
- 受限平台的工具必须让门禁拿得到主机 id(否则 CheckToolPlatform 会静默放行)
用法:
python check_platform_table.py [mcp/tools.json] [McpServer.cpp]
"""
import json
import re
import sys
JSON_PATH = sys.argv[1] if len(sys.argv) > 1 else "mcp/tools.json"
CPP_PATH = sys.argv[2] if len(sys.argv) > 2 else "server/2015Remote/McpServer.cpp"
BIT = {"kPlatWin": "win", "kPlatLnx": "lnx", "kPlatMac": "mac", "kPlatApk": "apk"}
ORDER = ["win", "lnx", "mac", "apk"]
def plats_of(expr, where):
out = []
for token in re.findall(r"kPlat\w+", expr):
if token not in BIT:
sys.exit(f"未知的平台常量 {token}({where})")
out.append(BIT[token])
# 保持与 tools.json 相同的顺序约定:win, lnx, mac, apk
return [p for p in ORDER if p in out]
def parse_cpp(path):
src = open(path, encoding="utf-8", errors="replace").read()
start = src.find("const ToolPlatform kToolPlatforms[]")
if start < 0:
sys.exit("找不到 kToolPlatforms 表")
end = src.find("};", start)
if end < 0:
sys.exit("kToolPlatforms 表没有闭合")
body = src[start:end]
table = {}
for line in body.splitlines():
m = re.search(r'\{\s*"([a-z_0-9]+)"\s*,\s*([^}]+)\}', line)
if not m:
continue
table[m.group(1)] = plats_of(m.group(2), f"工具 {m.group(1)}")
return table, src
def parse_cpp_roles(src):
"""从 CollectTargetReqs 里提取 AddIdReq(args, "<key>", <mask>, reqs) 三元组。"""
i = src.find("bool CollectTargetReqs(")
if i < 0:
return None
j = src.find("\n}", i)
fn_body = src[i:j] if j > i else ""
roles = {}
for m in re.finditer(r'AddIdReq\(\s*args\s*,\s*"(\w+)"\s*,\s*([^,]+),', fn_body):
roles[m.group(1)] = plats_of(m.group(2), f"CollectTargetReqs 的 {m.group(1)}")
return roles
def check_coverage(tools, src):
"""两件事:
1. 受限平台的工具若拿不到主机 id,CheckToolPlatform 会直接放行,等于没拦。
2. 多主机工具的方向性要求(roles)必须三方一致:tools.json / C++ / 本脚本。
"""
problems = 0
cpp_roles = parse_cpp_roles(src)
if cpp_roles is None:
print("!! 找不到 CollectTargetReqs(门禁实现可能被改名或删除)")
return 1
for t in tools:
name = t["name"]
props = t["inputSchema"].get("properties", {})
roles = t.get("roles")
if roles:
outer = set(t["platforms"])
for key, plats in roles.items():
if key not in props:
problems += 1
print(f"!! {name}.roles.{key}: 不在 inputSchema 里")
extra = set(plats) - outer
if extra:
problems += 1
print(f"!! {name}.roles.{key}: 超出 platforms 范围 {sorted(extra)}")
got = cpp_roles.get(key)
if got is None:
problems += 1
print(f"!! {name}.roles.{key}: C++ CollectTargetReqs 未处理")
elif got != list(plats):
problems += 1
print(f"!! {name}.roles.{key}: tools.json={plats} C++={got}")
continue
if len(t["platforms"]) >= 4:
continue # 全平台可用,不需要判断主机
if "id" in props:
continue # 标准路径,ParseHostIdArg 能取到
problems += 1
print(f"!! {name}: platforms={t['platforms']} 但入参无 id,门禁拿不到主机")
return problems
def check_win_baseline(tools):
"""Windows 是唯一全量支持的平台,这是产品定位,不是数据巧合。
任何工具都不该缺 win:缺了要么是标注出错,要么是这个工具根本不该注册。
支持面从宽到窄依次是 win > lnx > mac ≈ apk,这条阶梯也在这里顺带核对,
倒挂说明某一档标反了。
APK_ONLY_EXEMPT 是这条不变式的白名单:只放「语义上只属于 Android」的工具。
目前只有 remote_lock_screen——COMMAND_SCREEN_CTRL_ALT_DEL 在 Android 映射为系统
锁屏,Windows 侧要实现 Ctrl+Alt+Del(SendSAS)需要客户端具备 SYSTEM 或 UIAccess,
本轮不做。新增白名单条目必须写清楚这个理由。
"""
APK_ONLY_EXEMPT = {"remote_lock_screen"}
problems = 0
for t in tools:
if "win" not in t["platforms"] and t["name"] not in APK_ONLY_EXEMPT:
problems += 1
print(f"!! {t['name']}: platforms={t['platforms']} 不含 win,但 Windows 应全量支持")
def covered(p):
return sum(1 for t in tools if p in t["platforms"])
win, lnx, mac, apk = covered("win"), covered("lnx"), covered("mac"), covered("apk")
if not (win >= lnx >= mac and win >= apk):
problems += 1
print(f"!! 支持面阶梯倒挂: win={win} lnx={lnx} mac={mac} apk={apk}"
f"(应为 win >= lnx >= mac/apk)")
print(f"支持面: win={win} lnx={lnx} mac={mac} apk={apk}")
return problems
def check_instructions(src, tools):
"""instructions 第 5 节是平台矩阵的摘要,模型只读它不读表。
mac 段是逐个点名列出 win+lnx 组工具的,漏一个模型就会误以为 macOS 支持它
(实际发生过:remote_close 漏了)。这里做一次点名核对。
"""
i = src.find('u8"5) 客户端平台差异')
if i < 0:
print("!! 找不到 instructions 第 5 节(客户端平台差异)")
return 1
j = src.find('u8"6) 安全约定', i)
if j < 0:
print("!! 找不到 instructions 第 6 节,无法确定第 5 节的边界")
return 1
# 把这一节里所有 u8"..." 拼成连续文本,再按平台段落切开
text = "".join(re.findall(r'u8"((?:[^"\\]|\\.)*)"', src[i:j]))
mac_at, apk_at = text.find("mac(MAC)"), text.find("apk(APK)")
if mac_at < 0 or apk_at < mac_at:
print("!! instructions 第 5 节里找不到 mac / apk 段落")
return 1
mac_seg = text[mac_at:apk_at]
problems = 0
for t in tools:
if t["platforms"] == ["win", "lnx"] and t["name"] not in mac_seg:
problems += 1
print(f"!! {t['name']} 不支持 mac,但 instructions 的 mac 段没点名")
return problems
def main():
doc = json.load(open(JSON_PATH, encoding="utf-8"))
tools = doc["tools"]
json_table = {t["name"]: list(t["platforms"]) for t in tools}
cpp_table, cpp_src = parse_cpp(CPP_PATH)
print(f"{JSON_PATH}: {len(json_table)} 个工具")
print(f"{CPP_PATH}: {len(cpp_table)} 项")
problems = 0
only_json = sorted(set(json_table) - set(cpp_table))
only_cpp = sorted(set(cpp_table) - set(json_table))
if only_json:
problems += len(only_json)
print(f"!! 只在 tools.json 里(C++ 表漏登记): {only_json}")
if only_cpp:
problems += len(only_cpp)
print(f"!! 只在 C++ 表里(tools.json 已无此工具): {only_cpp}")
for name in sorted(set(json_table) & set(cpp_table)):
a, b = json_table[name], cpp_table[name]
if a != b:
problems += 1
print(f"!! {name}: tools.json={a} C++={b}")
problems += check_coverage(tools, cpp_src)
problems += check_win_baseline(tools)
problems += check_instructions(cpp_src, tools)
if problems == 0:
print(f"\nPASS: {len(json_table)} 个工具的 platforms 两边完全一致,"
f"Windows 全量覆盖,多主机工具的方向性要求三方一致,"
f"门禁都能拿到主机 id,instructions 第 5 节无漏项")
return 0
print(f"\nFAIL: {problems} 处不一致")
return 1
if __name__ == "__main__":
sys.exit(main())