Feature: Reject MCP tool calls the target client cannot handle
tools/list 不带参数,无法按目标设备过滤工具列表。60 个工具里有 33 个只在 Windows 客户端上有实现,模型无从得知,只能下发命令后白等超时。 DispatchSingleHostTool 开头按 kToolPlatforms 表核对目标主机平台,不匹配立刻 返回 -32005;批量路径经由同一函数自动覆盖。主机不存在或离线时放行,把 -32002 留给工具自己报,避免同一个问题出现两种错误码。 支持面是刻意设计的阶梯:win 60/60、lnx 27、mac 16、apk 10。60 个工具全部含 win,所以 Windows 客户端永远不会被这道门禁拒绝,行为与改动前完全一致。 平台表按服务端实际放行来标,不按客户端能力标。两处会分叉的地方: - exec_command / terminal_* / kill_process:macOS 客户端有实现(main.mm:753 的 ShellworkingThread、SystemManager 的 KILLPROCESS),但服务端刻意拦着 (McpServer.cpp:3652「macOS 本轮不开放」、6870),故标 win+lnx。 - list_windows:Linux/macOS 的 SystemManager 都有 COMMAND_WSLIST 分支,但 BuildListWindows 把命令发在主连接上,而这两个平台的主循环只处理 COMMAND_SYSTEM (起子连接),WSLIST 只在子连接里才被处理,走不到。实测 Linux 上直接超时 -32001。那两个 case 是给 SystemDlg 用的,故标 win。 copy_files_c2c 是唯一的非对称工具(源端必须 Windows,目标端可 win/lnx/mac, 反向不支持),用 roles 按参数名分别声明,C++ 侧 CollectTargetReqs 对应处理。 配套 mcp/tools.json(60 个工具,version 2)与 tools/mcp_tools/ 下的抓取、生成、 比对、校验脚本。check_platform_table.py 校验 C++ 表、tools.json、instructions 三方一致,并把「Windows 全量」做成不变式。 已在 A6(Windows 11)与 Ubuntu24(Linux)上实测:Windows 侧 14/14 通过且无误拒, Linux 侧 11 项拒绝全部命中门禁,copy_files_c2c win->lnx 成功、lnx->win 被拒。 开关 McpPlatformGuard(默认 1),关闭只失去提前拦截,不影响任何执行路径。
This commit is contained in:
1 parent
8c0351b45b
commit
356aa27108
10 files changed
+7085
-1
No files matched your search
@@ -2549,6 +2549,10 @@ BOOL CMy2015RemoteDlg::OnInitDialog()
|
||||
McpServer().SetChatEnabled(THIS_CFG.GetInt("settings", "McpChat", 0) != 0);
|
||||
// N3 键盘记录(get_keylog):独立开关默认关,与只读开关无关。
|
||||
McpServer().SetKeylogEnabled(THIS_CFG.GetInt("settings", "McpKeylog", 0) != 0);
|
||||
// 平台门禁:默认开。调用前拦掉目标客户端根本没实现的工具(立刻 -32005),
|
||||
// 否则会照常下发命令然后白等超时。关掉它只影响这道拦截,不改任何工具的执行路径。
|
||||
McpServer().SetPlatformGuardEnabled(
|
||||
THIS_CFG.GetInt("settings", "McpPlatformGuard", 1) != 0);
|
||||
// 工具默认等待超时(ini-only 键,无设置界面):默认 20000ms,clamp 在 SetToolTimeoutMs 内。
|
||||
McpServer().SetToolTimeoutMs(THIS_CFG.GetInt("settings", "McpToolTimeoutMs", 20000));
|
||||
// HTTPS(native TLS):McpHttps=1 时对外端口说 TLS,httplib 退到回环内部端口。
|
||||
|
||||
@@ -325,7 +325,31 @@ const char* kServerInstructions =
|
||||
u8"客户端无管理员权限时 Windows 会让 BlockInput 静默失败,成功只代表命令已送达)。\n"
|
||||
u8"命令执行:exec_command 一次性取输出;需要保持 cd 或环境变量时用 terminal_open + terminal_exec,用毕 terminal_close。\n"
|
||||
u8"\n"
|
||||
u8"5) 安全约定\n"
|
||||
u8"5) 客户端平台差异\n"
|
||||
u8"工具能否执行取决于目标主机的平台,这与服务端开关无关,两者是叠加关系。调用前用 "
|
||||
u8"list_online_hosts / get_host_detail 的 capabilities.platform 确认,宁可先查一次,不要先试后错。\n"
|
||||
u8"支持面从宽到窄依次是 win > lnx > mac ≈ apk:Windows 是唯一全量支持的平台,Linux 次之,"
|
||||
u8"macOS 与 Android 只覆盖一部分。拿不准时按这个顺序假设。\n"
|
||||
u8"win(EXE / DLL / MEXE / SC / MDLL):全部工具可用。\n"
|
||||
u8"lnx(LNX):不支持服务、注册表、键日志、活动历史、文件搜索/删除/移动/建目录、"
|
||||
u8"窗口列表(list_windows)、网络连接列表与断开"
|
||||
u8"(list_network_connections / kill_network_connection)、"
|
||||
u8"块输入、host_* 全族、chat_* 全族、operate_window、power_action。远程控制与截屏要求目标机"
|
||||
u8"有 X11/Xorg(或 XWayland)桌面,纯 Wayland 或无桌面的服务器不行。\n"
|
||||
u8"mac(MAC):在 lnx 的基础上,还不支持远程控制全族(remote_open / remote_close / "
|
||||
u8"remote_mouse / remote_keyboard / remote_clipboard)、get_screenshot、"
|
||||
u8"exec_command、terminal_open / terminal_exec / terminal_close"
|
||||
u8"(客户端有 PTY 实现,但服务端本轮未开放)、kill_process;"
|
||||
u8"文件传输、进程列表、日志可用。\n"
|
||||
u8"apk(APK):只支持主机列表(list_online_hosts / list_all_hosts)/ 详情 / 搜索 / "
|
||||
u8"审计日志 / 服务状态 / 会话管理,以及 "
|
||||
u8"host_set_remark / host_set_watch_level 这两个只改服务端本地数据的工具。其余一律不支持"
|
||||
u8"(客户端未实现 SYSTEM 与 SHELL)。\n"
|
||||
u8"copy_files_c2c 是唯一有方向性的工具:源端 src_id 必须是 win,目标端 dst_id 可以是 "
|
||||
u8"win / lnx / mac。Linux 和 macOS 只能当接收方,不能当发送方(反向不支持)。\n"
|
||||
u8"这些不支持的组合会立刻返回 -32005,不会白等超时;反之,遇到超时不要当成平台不支持。\n"
|
||||
u8"\n"
|
||||
u8"6) 安全约定\n"
|
||||
u8"写操作前先读一次现状,动作后再用读工具复核;删改类工具的 ok 只表示命令已送达客户端,不代表成功。"
|
||||
u8"涉及删除、重启、结束进程、关机等不可逆操作时,先把目标和影响范围讲给用户,得到确认再执行。"
|
||||
u8"主机上可能有人正在办公,注入输入、截屏、关机都是会被察觉的动作。";
|
||||
@@ -8765,6 +8789,213 @@ static const long long kBatchDeadlineMs = 50000;
|
||||
// IsBatchReadOnlyTool 定义在 McpInternal.cpp:tools/list 要用它补 outputSchema,
|
||||
// tools/call 要用它做批量入口判定,两处必须共用同一份清单,故不再在此重复定义。
|
||||
|
||||
// ===== 平台门禁:调用前拦掉「该客户端根本没实现」的组合 =====
|
||||
//
|
||||
// 判据来自各平台客户端对相应 COMMAND_ 的处理实现,不能看「是否引用了常量」:
|
||||
// - android/.../main.cpp:534-540 的 COMMAND_SYSTEM / COMMAND_SHELL 都是空实现
|
||||
// - macOS 能处理 SEND_FILE_V2,但 DefaultCaps() 不给 SCREEN_PREVIEW,截屏仍不可用
|
||||
// 表内容与 mcp/tools.json 的 platforms 字段一一对应,
|
||||
// tools/mcp_tools/check_platform_table.py 会校验两边一致。
|
||||
//
|
||||
// 这里只做平台级粗筛。能力位门槛(SCREEN_PREVIEW / V2 / C2C_PATHS)仍留在各工具内部,
|
||||
// 因为像「Linux 这台机器有没有 X11 桌面」只有运行时才知道。
|
||||
enum : uint8_t {
|
||||
kPlatWin = 1,
|
||||
kPlatLnx = 2,
|
||||
kPlatMac = 4,
|
||||
kPlatApk = 8,
|
||||
};
|
||||
|
||||
struct ToolPlatform {
|
||||
const char* name;
|
||||
uint8_t mask;
|
||||
};
|
||||
|
||||
// mask 为 0 或表里查不到 = 不限制。新增工具忘了登记时按放行处理(宁可漏拦,不可误拒),
|
||||
// 遗漏由 check_platform_table.py 的工具数比对发现。
|
||||
const ToolPlatform kToolPlatforms[] = {
|
||||
// ---- win+lnx+mac+apk ---- 只读写服务端本地数据,与客户端平台无关
|
||||
{ "list_online_hosts", kPlatWin|kPlatLnx|kPlatMac|kPlatApk },
|
||||
{ "search_hosts", kPlatWin|kPlatLnx|kPlatMac|kPlatApk },
|
||||
{ "list_all_hosts", kPlatWin|kPlatLnx|kPlatMac|kPlatApk },
|
||||
{ "get_host_detail", kPlatWin|kPlatLnx|kPlatMac|kPlatApk },
|
||||
{ "get_audit_log", kPlatWin|kPlatLnx|kPlatMac|kPlatApk },
|
||||
{ "get_server_status", kPlatWin|kPlatLnx|kPlatMac|kPlatApk },
|
||||
{ "mcp_list_sessions", kPlatWin|kPlatLnx|kPlatMac|kPlatApk },
|
||||
{ "mcp_cancel_session", kPlatWin|kPlatLnx|kPlatMac|kPlatApk },
|
||||
{ "host_set_remark", kPlatWin|kPlatLnx|kPlatMac|kPlatApk },
|
||||
{ "host_set_watch_level", kPlatWin|kPlatLnx|kPlatMac|kPlatApk },
|
||||
// ---- win+lnx+mac ---- SYSTEM / LIST_DRIVE / QUERY_LOG / V2 文件传输
|
||||
{ "list_processes", kPlatWin|kPlatLnx|kPlatMac },
|
||||
{ "list_files", kPlatWin|kPlatLnx|kPlatMac },
|
||||
{ "get_client_log", kPlatWin|kPlatLnx|kPlatMac },
|
||||
{ "download_file", kPlatWin|kPlatLnx|kPlatMac },
|
||||
{ "upload_file", kPlatWin|kPlatLnx|kPlatMac },
|
||||
// ---- win+lnx ---- 远程控制全族 + 需 SCREEN_PREVIEW 的截屏 + macOS 未开放项
|
||||
{ "get_screenshot", kPlatWin|kPlatLnx },
|
||||
{ "remote_open", kPlatWin|kPlatLnx },
|
||||
{ "remote_close", kPlatWin|kPlatLnx },
|
||||
{ "remote_keyboard", kPlatWin|kPlatLnx },
|
||||
{ "remote_mouse", kPlatWin|kPlatLnx },
|
||||
{ "remote_clipboard", kPlatWin|kPlatLnx },
|
||||
// macOS 客户端有 PTY 实现(macos/main.mm:753),但服务端刻意不开放
|
||||
// (McpServer.cpp:3652「macOS 本轮不开放」),故实际不可用
|
||||
{ "exec_command", kPlatWin|kPlatLnx },
|
||||
{ "terminal_open", kPlatWin|kPlatLnx },
|
||||
{ "terminal_exec", kPlatWin|kPlatLnx },
|
||||
{ "terminal_close", kPlatWin|kPlatLnx },
|
||||
// macOS 客户端有 COMMAND_KILLPROCESS 分支,服务端同样拦着(McpServer.cpp:6870)
|
||||
{ "kill_process", kPlatWin|kPlatLnx },
|
||||
// ---- win ---- 服务 / 注册表 / 键日志 / 文件写 / host_* / chat_* / power_action
|
||||
// list_windows 单列:BuildListWindows 把 COMMAND_WSLIST 发在**主连接**上
|
||||
// (McpServer.cpp:2931 的 Send2Client),而 Linux/macOS 主循环只处理 COMMAND_SYSTEM
|
||||
// (起 SystemManager 子连接),WSLIST 只在子连接里处理 —— 走不到,实测 Linux 上
|
||||
// 直接超时 -32001。Windows 是在主循环里处理的(client/KernelManager.cpp:1491)。
|
||||
// 注意别被 linux/SystemManager.h:38 和 macos/SystemManager.mm:39 的 case 骗到,
|
||||
// 那是给 SystemDlg 窗口管理用的,不是 MCP 这条路径。
|
||||
{ "list_windows", kPlatWin },
|
||||
{ "get_activity_history", kPlatWin },
|
||||
{ "list_services", kPlatWin },
|
||||
{ "get_keylog", kPlatWin },
|
||||
{ "list_registry", kPlatWin },
|
||||
{ "remote_block_input", kPlatWin },
|
||||
// 两端并集。方向性差异由 CollectTargetReqs 精确处理:源端必须 win,目标端 win/lnx/mac
|
||||
{ "copy_files_c2c", kPlatWin|kPlatLnx|kPlatMac },
|
||||
{ "operate_window", kPlatWin },
|
||||
{ "control_service", kPlatWin },
|
||||
{ "power_action", kPlatWin },
|
||||
{ "search_file", kPlatWin },
|
||||
{ "delete_path", kPlatWin },
|
||||
{ "move_path", kPlatWin },
|
||||
{ "create_folder", kPlatWin },
|
||||
{ "list_network_connections", kPlatWin },
|
||||
{ "kill_network_connection", kPlatWin },
|
||||
{ "host_list_processes", kPlatWin },
|
||||
{ "host_list_windows", kPlatWin },
|
||||
{ "host_list_network_state", kPlatWin },
|
||||
{ "host_list_software", kPlatWin },
|
||||
{ "host_list_browser_history", kPlatWin },
|
||||
{ "host_list_favorites", kPlatWin },
|
||||
{ "host_list_win32_services", kPlatWin },
|
||||
{ "host_list_driver_services", kPlatWin },
|
||||
{ "host_list_scheduled_tasks", kPlatWin },
|
||||
{ "host_get_hosts_file", kPlatWin },
|
||||
{ "host_control_process", kPlatWin },
|
||||
{ "host_control_window", kPlatWin },
|
||||
{ "host_control_service", kPlatWin },
|
||||
{ "host_control_task", kPlatWin },
|
||||
{ "chat_open", kPlatWin },
|
||||
{ "chat_send", kPlatWin },
|
||||
{ "chat_wait", kPlatWin },
|
||||
{ "chat_close", kPlatWin },
|
||||
};
|
||||
|
||||
// 未知的 clientType 一律按 Windows:EXE/DLL/MEXE/SC/MDLL 都走这个分支,
|
||||
// 将来新增平台在没有明确登记前也不会被误拦。
|
||||
uint8_t CurrentClientPlatform(context* ctx) {
|
||||
CString t = ctx->GetAdditionalData(RES_CLIENT_TYPE);
|
||||
if (t == "LNX") return kPlatLnx;
|
||||
if (t == "MAC") return kPlatMac;
|
||||
if (t == "APK") return kPlatApk;
|
||||
return kPlatWin;
|
||||
}
|
||||
|
||||
const char* PlatformName(uint8_t p) {
|
||||
switch (p) {
|
||||
case kPlatLnx: return "lnx";
|
||||
case kPlatMac: return "mac";
|
||||
case kPlatApk: return "apk";
|
||||
default: return "win";
|
||||
}
|
||||
}
|
||||
|
||||
std::string PlatformNames(uint8_t mask) {
|
||||
std::string s;
|
||||
if (mask & kPlatWin) s += "win ";
|
||||
if (mask & kPlatLnx) s += "lnx ";
|
||||
if (mask & kPlatMac) s += "mac ";
|
||||
if (mask & kPlatApk) s += "apk ";
|
||||
if (!s.empty()) s.pop_back();
|
||||
return s;
|
||||
}
|
||||
|
||||
// 一台主机 + 它在这个工具里被允许的平台集合。
|
||||
struct TargetReq {
|
||||
uint64_t id;
|
||||
uint8_t mask;
|
||||
};
|
||||
|
||||
bool AddIdReq(const Json::Value& args, const char* key, uint8_t mask,
|
||||
std::vector<TargetReq>& reqs) {
|
||||
if (!args[key].isString()) return false;
|
||||
const std::string sid = args[key].asString();
|
||||
if (sid.empty() || sid.find_first_not_of("0123456789") != std::string::npos) return false;
|
||||
reqs.push_back({ strtoull(sid.c_str(), nullptr, 10), mask });
|
||||
return true;
|
||||
}
|
||||
|
||||
// 取出这个工具真正要操作的主机,以及每台主机各自的平台要求。
|
||||
// 绝大多数工具只有一个目标、用统一的 defaultMask;copy_files_c2c 是唯一的
|
||||
// 非对称工具,源端和目标端的平台要求不同,必须分开查:
|
||||
// src_id —— 只有 Windows 实现了 C2C 发送逻辑(client/KernelManager.cpp)
|
||||
// dst_id —— 只需 CLIENT_CAP_V2 收包,Windows / Linux / macOS 都行
|
||||
// 反过来 Linux → Windows 不支持:POSIX 客户端没有 C2C_PATHS 发送实现。
|
||||
// 返回 false = 这个工具不针对某台主机(服务端本地工具),不需要门禁。
|
||||
bool CollectTargetReqs(const std::string& toolName, const Json::Value& args,
|
||||
uint8_t defaultMask, std::vector<TargetReq>& reqs) {
|
||||
// 多主机工具必须先判断:它的 schema 里没有 "id",但调用方可以多塞一个 id 进来。
|
||||
// 若先走 ParseHostIdArg,就只会检查那一台主机,真正的 src_id / dst_id 反而漏过去。
|
||||
if (toolName == "copy_files_c2c") {
|
||||
AddIdReq(args, "src_id", kPlatWin, reqs);
|
||||
AddIdReq(args, "dst_id", kPlatWin|kPlatLnx|kPlatMac, reqs);
|
||||
return !reqs.empty();
|
||||
}
|
||||
|
||||
uint64_t devId = 0;
|
||||
std::string err;
|
||||
if (!ParseHostIdArg(args, devId, err)) return false; // 无 id:服务端本地工具
|
||||
reqs.push_back({ devId, defaultMask });
|
||||
return true;
|
||||
}
|
||||
|
||||
// 返回空串 = 放行;非空 = 可直接作为 tools/call 结果返回的错误 JSON。
|
||||
// 主机不存在/离线时跳过该 id,把 -32002 留给工具自己报,避免同一问题出现两种错误码。
|
||||
std::string CheckToolPlatform(const Json::Value& id, const std::string& toolName,
|
||||
const Json::Value& args, CMy2015RemoteDlg* parent) {
|
||||
if (!CMcpServer::Instance().IsPlatformGuardEnabled()) return "";
|
||||
|
||||
uint8_t mask = 0;
|
||||
for (const ToolPlatform& e : kToolPlatforms) {
|
||||
if (toolName == e.name) { mask = e.mask; break; }
|
||||
}
|
||||
if (mask == 0) return "";
|
||||
|
||||
std::vector<TargetReq> reqs;
|
||||
if (!CollectTargetReqs(toolName, args, mask, reqs)) return ""; // 无 id:服务端本地工具
|
||||
|
||||
for (const TargetReq& req : reqs) {
|
||||
context* ctx = FindMainContext(parent, req.id);
|
||||
if (!ctx) continue;
|
||||
|
||||
const uint8_t cur = CurrentClientPlatform(ctx);
|
||||
if (req.mask & cur) continue;
|
||||
|
||||
CString ct = ctx->GetAdditionalData(RES_CLIENT_TYPE);
|
||||
std::string msg;
|
||||
msg += std::string(u8"该主机平台是 ");
|
||||
msg += PlatformName(cur);
|
||||
msg += std::string(u8"(clientType=");
|
||||
msg += std::string((LPCSTR)ct);
|
||||
msg += std::string(u8"),");
|
||||
msg += toolName;
|
||||
msg += std::string(u8" 在此主机上仅支持 ");
|
||||
msg += PlatformNames(req.mask);
|
||||
msg += std::string(u8"。按 instructions 第 5 节换用该平台支持的等价工具。");
|
||||
return BuildError(id, -32005, msg);
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
// 单主机分派:批量路径复用它,保证批量与单机走同一套实现。
|
||||
std::string DispatchSingleHostTool(const Json::Value& id, const std::string& toolName,
|
||||
const Json::Value& args, CMy2015RemoteDlg* parent);
|
||||
@@ -8933,6 +9164,13 @@ std::string BuildToolsCall(const Json::Value& root, CMy2015RemoteDlg* parent) {
|
||||
|
||||
std::string DispatchSingleHostTool(const Json::Value& id, const std::string& toolName,
|
||||
const Json::Value& args, CMy2015RemoteDlg* parent) {
|
||||
// 平台门禁:放在所有分派之前,批量路径也会经过这里(BuildBatchCall 循环调用本函数),
|
||||
// 所以不需要在批量入口重复一次。主机不存在/离线时放行,让各工具自己报 -32002。
|
||||
{
|
||||
std::string platErr = CheckToolPlatform(id, toolName, args, parent);
|
||||
if (!platErr.empty()) return platErr;
|
||||
}
|
||||
|
||||
// P8:连接管理 / 主机管理工具族(独立分发;门控在 McpMachine::Call 内复核)。
|
||||
if (McpMachine::Handles(toolName)) return McpMachine::Call(id, toolName, args, parent);
|
||||
|
||||
|
||||
@@ -605,6 +605,11 @@ public:
|
||||
bool IsReadonly() const { return m_readonly; }
|
||||
const std::string& GetCmdWhitelist() const { return m_cmdWhitelist; }
|
||||
|
||||
// 平台门禁开关:默认开。关闭只影响「错误平台上的调用是否被提前拦住」,
|
||||
// 不影响任何工具的正常执行路径,可作为出问题时的应急回退手段。
|
||||
void SetPlatformGuardEnabled(bool enabled) { m_platformGuard = enabled; }
|
||||
bool IsPlatformGuardEnabled() const { return m_platformGuard; }
|
||||
|
||||
// 监听地址/端口(Start 时记录,供 get_server_status 报告)。
|
||||
const std::string& GetBind() const { return m_bind; }
|
||||
int GetPort() const { return m_port; }
|
||||
@@ -938,6 +943,10 @@ private:
|
||||
bool m_machineEnabled = false; // P8 主机管理族总开关(默认关;16 个 host_* 工具)
|
||||
bool m_machineDangerousEnabled = false; // P8 高风险子集(冻结/解冻进程、停止/重启服务)
|
||||
bool m_keylogEnabled = false; // N3 键盘记录开关(默认关;get_keylog)
|
||||
// 平台门禁(默认开):调用前拦掉「该客户端根本没实现」的工具,立刻返回 -32005。
|
||||
// 关闭后这类调用会照常下发命令,通常表现为白等超时。判据表见 McpServer.cpp 的
|
||||
// kToolPlatforms,与 mcp/tools.json 的 platforms 字段对应。
|
||||
bool m_platformGuard = true;
|
||||
int m_toolTimeoutMs = 20000; // 工具默认等待超时(McpToolTimeoutMs,见 SetToolTimeoutMs)
|
||||
};
|
||||
|
||||
|
||||
Reference in new issue
Block a user