From 5841f27df93539576c2205bfd34b9f52e3b90757 Mon Sep 17 00:00:00 2001 From: yuanyuanxiang <962914132@qq.com> Date: Thu, 12 Nov 2026 17:40:51 +0100 Subject: [PATCH] Feature(mcp): unify window and service controls behind one name per state operate_window / host_control_window and control_service / host_control_service were two implementations of the same intent. tools/list now exposes exactly one of each pair, chosen by McpMachine: the host_control_* names when host management is enabled, the main-connection names when it is not. The advertised tool accepts the union of actions. host_control_window routes close/show to the main-connection implementation, which stays the only path with close/show semantics and requires McpManage; the machine path keeps its post-action verification for restore/hide/maximize/minimize/activate. host_control_service routes set_auto/set_manual the same way and rejects dry_run for them instead of pretending to run one. Both old names remain callable aliases, and service_name is accepted as an alias for the machine tool's name parameter. The manifest generator treats the two main-connection tools as !machine entries, so the two-capture union still covers both states. tools/list is 49 entries when everything is on (was 51) and 47 when host management is off; the response shrinks from 193,008 B to 168,634 B (-12.6%), short of the 15% estimate because state-conditional tools must stay in the manifest. The plan document records that measurement and the reason. Verified with a dedicated two-state test: visibility matrix, close/show and set_auto/set_manual routing through dry_run and validation paths, alias dispatch, and -32006 for host_* names when host management is off. Step 8a/8b regressions and verify_tools --live still pass. Co-Authored-By: deepseek-flash --- docs/Mcp_Tool_Optimization_Plan.md | 11 +- mcp/tools.json | 249 +++++++++++++++-------------- server/2015Remote/McpMachine.cpp | 31 ++-- server/2015Remote/McpServer.cpp | 53 +++++- tools/mcp_tools/make_tools_json.py | 15 +- 5 files changed, 218 insertions(+), 141 deletions(-) diff --git a/docs/Mcp_Tool_Optimization_Plan.md b/docs/Mcp_Tool_Optimization_Plan.md index bc0d9ee..d85f1d5 100644 --- a/docs/Mcp_Tool_Optimization_Plan.md +++ b/docs/Mcp_Tool_Optimization_Plan.md @@ -462,6 +462,14 @@ host_inspect(id, aspects[]) // processes|windows|network|software|browser_hist outputSchema 占 49,320 B 且合并后要保留,被删掉的主要是 17 个工具的 description + inputSchema,乐观估计降 25–35KB(**约 −15%**)。实际值以改动后用附录 B 的方法重测为准。 +**实测结果(2026-11-12,P2-1/2 落地后)**:开启全部开关时 `tools/list` 响应 +**168,634 B / 49 个工具**,较基线降 24,374 B(**−12.6%**),未达到 15%。原因是清单 +必须同时描述两种开关状态:`list_processes` / `list_windows` / `list_services` / +`operate_window` / `control_service` 在 McpMachine=0 时是唯一可用路径,因此以 +`gate: "!machine"` 留在清单里(见 P2-4),所有状态下的实际列表都是 49 或 47 个, +到不了估算里的 46。若还要再降,只有两条路:删掉降级路径(关掉主机管理就失去列进程/ +窗口/服务的能力)或压缩 description/outputSchema 正文——两者都不建议为凑 15% 去做。 + ### P2-4 别名/合并的同步项(v2 新增) 「旧名保留为别名、`tools/list` 只暴露新名」听起来只是一张别名表,但工具名在服务端还有 @@ -535,7 +543,8 @@ inputSchema,乐观估计降 25–35KB(**约 −15%**)。实际值以改动 - `timeout_ms` 手工覆盖次数从 41 降到 **10 以内** - 零 `-32002`(会话被销毁);超时后能轮询续取 - `tools/list` 响应体积**较实测基线 193,008 B 下降 ≥15%**(≈ ≤164KB)。不要沿用 - 「80KB」:按本次合并范围做不到,见 P2-3 + 「80KB」:按本次合并范围做不到,见 P2-3。**实测 168,634 B(−12.6%)**,未达标, + 原因见 P2-3 的实测结果段 --- diff --git a/mcp/tools.json b/mcp/tools.json index f264a2a..727ab28 100644 --- a/mcp/tools.json +++ b/mcp/tools.json @@ -2672,117 +2672,6 @@ "type": "object" } }, - { - "name": "operate_window", - "description": "对指定在线 Windows 主机的窗口执行操作:close(关闭)/show(显示)/hide(隐藏)/minimize(最小化)/maximize(最大化)/restore(还原)/activate(激活到前台)。hwnd 取 list_windows 或 host_inspect 的 windows 项返回的 hwnd。ok 仅表示已下发(窗口句柄可能已失效);action=activate 例外:工具会在动作后用窗口信息 RPC 复核 GetForegroundWindow,返回 ok/state(foreground|background|missing)/window,旧客户端不支持时返回 -32005(可退回 show)。关闭窗口只有本工具能做,host_control_window 不提供 close,且它会重新枚举可见窗口复核结果。dry_run=true 只校验 hwnd 是否还在可见窗口列表里并返回该窗口信息,不发送任何命令。", - "gate": "manage", - "write": true, - "platforms": [ - "win" - ], - "impl": "mfc", - "inputSchema": { - "properties": { - "action": { - "description": "操作:close(关闭) | show(显示) | hide(隐藏) | minimize(最小化) | maximize(最大化) | restore(还原) | activate(激活到前台,需新客户端)", - "type": "string" - }, - "dry_run": { - "description": "true 时只校验窗口是否还存在并返回将被影响的对象,不发送任何窗口命令(默认 false)", - "type": "boolean" - }, - "hwnd": { - "description": "窗口句柄,取 list_windows 返回的 hwnd 字段", - "type": "integer" - }, - "id": { - "description": "主机 id,取 list_hosts 返回的 id 字段", - "type": "string" - } - }, - "required": [ - "id", - "hwnd", - "action" - ], - "type": "object" - }, - "outputSchema": { - "properties": { - "action": { - "type": "string" - }, - "dry_run": { - "description": "true 表示本次只是试运行(dry_run),没有发送窗口命令", - "type": "boolean" - }, - "ok": { - "description": "命令已下发(不代表窗口已按预期变化);action=activate 时为前台复核结果", - "type": "boolean" - }, - "state": { - "description": "action=activate 时的复核状态:foreground / background / missing / unknown", - "type": "string" - }, - "truncated": { - "description": "为 true 时表示复核用的可见窗口列表被 96 条单次上限截断,未找到目标不代表窗口不存在(此时 state=unknown)", - "type": "boolean" - }, - "verifyError": { - "description": "action=activate 且前台复核请求失败时的错误说明", - "type": "string" - }, - "window": { - "description": "dry_run 时的目标窗口;action=activate 时为复核到的窗口快照(hwnd/title/pid/rect/flags 等),复核未找到为 null", - "type": [ - "object", - "null" - ] - } - }, - "type": "object" - } - }, - { - "name": "control_service", - "description": "控制指定在线 Windows 主机的服务:start(启动)/stop(停止)/set_auto(设为自动)/set_manual(设为手动)。service_name 取 list_services 返回的 service_name。ok 仅表示已下发(成功与否需再查 list_services 核对状态)。", - "gate": "manage", - "write": true, - "platforms": [ - "win" - ], - "impl": "mfc", - "inputSchema": { - "properties": { - "action": { - "description": "操作:start(启动) | stop(停止) | set_auto(设为自动) | set_manual(设为手动)", - "type": "string" - }, - "id": { - "description": "主机 id,取 list_hosts 返回的 id 字段", - "type": "string" - }, - "service_name": { - "description": "服务名,取 list_services 返回的 service_name 字段", - "type": "string" - } - }, - "required": [ - "id", - "service_name", - "action" - ], - "type": "object" - }, - "outputSchema": { - "properties": { - "ok": { - "type": "boolean" - } - }, - "type": "object" - } - }, { "name": "power_action", "description": "对指定在线 Windows 主机执行电源操作:logout(注销)/shutdown(关机)/reboot(重启)。不可撤销,主机将立即下线;需客户端管理员权限。ok/sent 只代表命令已送达,不代表真的关了机:要验证就给 wait=true,工具会等主机从在线列表消失(went_offline)、再等它重新上线(came_back),并返回 elapsed_ms;每个阶段最多等 wait_ms(默认 60000),等不到不算失败。注销后客户端一般不会自动回线。", @@ -4419,7 +4308,7 @@ }, { "name": "host_control_window", - "description": "对在线 Windows 主机的窗口执行可恢复操作:restore(还原)/hide(隐藏)/maximize(最大化)/minimize(最小化)/activate(激活到前台)。hwnd 取 host_inspect 的 windows 项或 list_windows 返回的 hwnd。动作后重新枚举可见窗口复核:hide 后 hwnd 应消失,其余动作后仍应存在;activate 会额外复核 GetForegroundWindow,目标未成为前台则 ok=false、reason=not_foreground。activate 需要新客户端(窗口信息能力),旧客户端返回 -32005,可退回 restore。不提供关闭窗口——需要 close / show 时改用 operate_window。dry_run=true 只核实 hwnd 并返回窗口信息。", + "description": "对在线 Windows 主机的窗口执行操作:restore(还原)/hide(隐藏)/maximize(最大化)/minimize(最小化)/activate(激活到前台)/close(关闭)/show(显示)。hwnd 取 host_inspect 的 windows 项或 list_windows 返回的 hwnd。restore 等五个动作走主机管理链路,动作后重新枚举可见窗口复核:hide 后 hwnd 应消失,其余动作后仍应存在;activate 会额外复核 GetForegroundWindow,目标未成为前台则 ok=false、reason=not_foreground,且需要新客户端(窗口信息能力),旧客户端返回 -32005,可退回 restore。close/show 没有主机管理实现,转由主连接版 operate_window 处理(因此动作后不复核,ok 只表示已下发;McpManage=0 时返回 -32006)——它们只在 McpMachine=0 时才以 operate_window 之名出现在 tools/list。dry_run=true 只核实 hwnd 并返回窗口信息,不下发动作。", "gate": "machine", "write": true, "platforms": [ @@ -4429,13 +4318,15 @@ "inputSchema": { "properties": { "action": { - "description": "restore/hide/maximize/minimize/activate", + "description": "restore/hide/maximize/minimize/activate,以及 close/show(转主连接实现,需 McpManage=1)", "enum": [ "restore", "hide", "maximize", "minimize", - "activate" + "activate", + "close", + "show" ], "type": "string" }, @@ -4511,7 +4402,7 @@ }, { "name": "host_control_service", - "description": "控制在线 Windows 主机的服务:start/stop/pause/continue/restart。客户端对每次控制都会自动回一份刷新后的服务列表,工具据此复核状态并如实返回 ok;stop 与 restart 归入危险操作,需要同时开启 McpMachineDangerous。restart 先 stop 再 start。不提供删除服务。dry_run=true 只核实服务并返回当前状态,不下发命令(是否已是目标状态由你按 state 判断)。", + "description": "控制在线 Windows 主机的服务:start/stop/pause/continue/restart/set_auto/set_manual。start 等五个动作走主机管理链路:客户端对每次控制都会自动回一份刷新后的服务列表,工具据此复核状态并如实返回 ok;stop 与 restart 归入危险操作,需要同时开启 McpMachineDangerous;restart 先 stop 再 start。set_auto/set_manual 没有主机管理实现,转由主连接版 control_service 处理(ok 只表示已下发、需另查状态;McpManage=0 时返回 -32006)——它们只在 McpMachine=0 时才以 control_service 之名出现在 tools/list。不提供删除服务。dry_run=true 只核实服务并返回当前状态,不下发命令(是否已是目标状态由你按 state 判断);set_auto/set_manual 不支持 dry_run(返回 -32602)。", "gate": "machine", "write": true, "platforms": [ @@ -4521,13 +4412,15 @@ "inputSchema": { "properties": { "action": { - "description": "start/stop/pause/continue/restart", + "description": "start/stop/pause/continue/restart,以及 set_auto/set_manual(转主连接实现,需 McpManage=1)", "enum": [ "start", "stop", "pause", "continue", - "restart" + "restart", + "set_auto", + "set_manual" ], "type": "string" }, @@ -4540,14 +4433,17 @@ "type": "string" }, "name": { - "description": "服务名(ServiceName,不是显示名),取 host_inspect 的 win32_services / driver_services 项的 name", + "description": "服务名(ServiceName,不是显示名),取 host_inspect 的 win32_services / driver_services 项的 name;与 service_name 二选一", + "type": "string" + }, + "service_name": { + "description": "name 的旧参数名别名(与 name 等价,二选一)", "type": "string" } }, "required": [ "id", - "action", - "name" + "action" ], "type": "object" }, @@ -5377,6 +5273,119 @@ ], "type": "object" } + }, + { + "name": "operate_window", + "description": "对指定在线 Windows 主机的窗口执行操作:close(关闭)/show(显示)/hide(隐藏)/minimize(最小化)/maximize(最大化)/restore(还原)/activate(激活到前台)。hwnd 取 list_windows 或 host_inspect 的 windows 项返回的 hwnd。ok 仅表示已下发(窗口句柄可能已失效);action=activate 例外:工具会在动作后用窗口信息 RPC 复核 GetForegroundWindow,返回 ok/state(foreground|background|missing)/window,旧客户端不支持时返回 -32005(可退回 show)。仅在 McpMachine=0(主机管理工具关闭)时出现在 tools/list;开启后请用 host_control_window,它也接受 close/show。dry_run=true 只校验 hwnd 是否还在可见窗口列表里并返回该窗口信息,不发送任何命令。", + "gate": "!machine", + "write": true, + "manage": true, + "platforms": [ + "win" + ], + "impl": "mfc", + "inputSchema": { + "properties": { + "action": { + "description": "操作:close(关闭) | show(显示) | hide(隐藏) | minimize(最小化) | maximize(最大化) | restore(还原) | activate(激活到前台,需新客户端)", + "type": "string" + }, + "dry_run": { + "description": "true 时只校验窗口是否还存在并返回将被影响的对象,不发送任何窗口命令(默认 false)", + "type": "boolean" + }, + "hwnd": { + "description": "窗口句柄,取 list_windows 返回的 hwnd 字段", + "type": "integer" + }, + "id": { + "description": "主机 id,取 list_hosts 返回的 id 字段", + "type": "string" + } + }, + "required": [ + "id", + "hwnd", + "action" + ], + "type": "object" + }, + "outputSchema": { + "properties": { + "action": { + "type": "string" + }, + "dry_run": { + "description": "true 表示本次只是试运行(dry_run),没有发送窗口命令", + "type": "boolean" + }, + "ok": { + "description": "命令已下发(不代表窗口已按预期变化);action=activate 时为前台复核结果", + "type": "boolean" + }, + "state": { + "description": "action=activate 时的复核状态:foreground / background / missing / unknown", + "type": "string" + }, + "truncated": { + "description": "为 true 时表示复核用的可见窗口列表被 96 条单次上限截断,未找到目标不代表窗口不存在(此时 state=unknown)", + "type": "boolean" + }, + "verifyError": { + "description": "action=activate 且前台复核请求失败时的错误说明", + "type": "string" + }, + "window": { + "description": "dry_run 时的目标窗口;action=activate 时为复核到的窗口快照(hwnd/title/pid/rect/flags 等),复核未找到为 null", + "type": [ + "object", + "null" + ] + } + }, + "type": "object" + } + }, + { + "name": "control_service", + "description": "控制指定在线 Windows 主机的服务:start(启动)/stop(停止)/set_auto(设为自动)/set_manual(设为手动)。service_name 取 list_services 返回的 service_name。ok 仅表示已下发(成功与否需再查 list_services 核对状态)。仅在 McpMachine=0(主机管理工具关闭)时出现在 tools/list;开启后请用 host_control_service,它也接受 set_auto/set_manual。", + "gate": "!machine", + "write": true, + "manage": true, + "platforms": [ + "win" + ], + "impl": "mfc", + "inputSchema": { + "properties": { + "action": { + "description": "操作:start(启动) | stop(停止) | set_auto(设为自动) | set_manual(设为手动)", + "type": "string" + }, + "id": { + "description": "主机 id,取 list_hosts 返回的 id 字段", + "type": "string" + }, + "service_name": { + "description": "服务名,取 list_services 返回的 service_name 字段", + "type": "string" + } + }, + "required": [ + "id", + "service_name", + "action" + ], + "type": "object" + }, + "outputSchema": { + "properties": { + "ok": { + "type": "boolean" + } + }, + "type": "object" + } } ] } diff --git a/server/2015Remote/McpMachine.cpp b/server/2015Remote/McpMachine.cpp index a9f1c83..e5d67e5 100644 --- a/server/2015Remote/McpMachine.cpp +++ b/server/2015Remote/McpMachine.cpp @@ -2431,8 +2431,11 @@ static std::string BuildHostControlWindow(const Json::Value& id, const Json::Val else if (action == "maximize") sw = SW_SHOWMAXIMIZED; else if (action == "minimize") sw = SW_SHOWMINIMIZED; else if (activate) sw = CMD_WINDOW_ACTIVATE_FLAG; + // close/show 在分派层已转给主连接实现(operate_window),不会走到这里; + // 错误信息仍按工具 schema 的完整枚举列出,免得调用方以为它们不存在。 else return BuildError(id, -32602, - "Invalid action: expected restore, hide, maximize, minimize or activate"); + "Invalid action: expected restore, hide, maximize, minimize, " + "activate, close or show"); uint32_t hwnd = 0; if (!ReadHwndArg(id, args, hwnd, errJson)) return errJson; @@ -2568,10 +2571,13 @@ static std::string BuildHostControlService(const Json::Value& id, const Json::Va std::string action = ToLowerAscii(OptStr(args, "action")); bool dangerous = (action == "stop" || action == "restart"); + // set_auto/set_manual 在分派层已转给主连接实现(control_service),不会走到这里; + // 错误信息仍按工具 schema 的完整枚举列出。 if (action != "start" && action != "stop" && action != "pause" && action != "continue" && action != "restart") return BuildError(id, -32602, - "Invalid action: expected start, stop, pause, continue or restart"); + "Invalid action: expected start, stop, pause, continue, restart, " + "set_auto or set_manual"); if (dangerous && !RequireDangerous(id, errJson)) return errJson; std::string serviceName = Trim(GetStringArg(args, "name")); @@ -3205,8 +3211,10 @@ void AppendTools(Json::Value& tools) { { Json::Value props(Json::objectValue); props["id"] = HostIdProp(); - props["action"] = PropStrEnum(u8"restore/hide/maximize/minimize/activate", - { "restore", "hide", "maximize", "minimize", "activate" }); + props["action"] = PropStrEnum(u8"restore/hide/maximize/minimize/activate," + u8"以及 close/show(转主连接实现,需 McpManage=1)", + { "restore", "hide", "maximize", "minimize", "activate", + "close", "show" }); Json::Value hwnd(Json::objectValue); Json::Value types(Json::arrayValue); types.append("integer"); @@ -3216,20 +3224,23 @@ void AppendTools(Json::Value& tools) { props["hwnd"] = hwnd; props["dry_run"] = PropBool(u8"true 时只校验 hwnd 仍在可见窗口列表里并返回该窗口,不下发操作,默认 false"); AddTool(tools, "host_control_window", - u8"对在线 Windows 主机的窗口执行可恢复操作:restore(还原)/hide(隐藏)/maximize(最大化)/minimize(最小化)/activate(激活到前台)。hwnd 取 host_inspect 的 windows 项或 list_windows 返回的 hwnd。动作后重新枚举可见窗口复核:hide 后 hwnd 应消失,其余动作后仍应存在;activate 会额外复核 GetForegroundWindow,目标未成为前台则 ok=false、reason=not_foreground。activate 需要新客户端(窗口信息能力),旧客户端返回 -32005,可退回 restore。不提供关闭窗口——需要 close / show 时改用 operate_window。dry_run=true 只核实 hwnd 并返回窗口信息。", + u8"对在线 Windows 主机的窗口执行操作:restore(还原)/hide(隐藏)/maximize(最大化)/minimize(最小化)/activate(激活到前台)/close(关闭)/show(显示)。hwnd 取 host_inspect 的 windows 项或 list_windows 返回的 hwnd。restore 等五个动作走主机管理链路,动作后重新枚举可见窗口复核:hide 后 hwnd 应消失,其余动作后仍应存在;activate 会额外复核 GetForegroundWindow,目标未成为前台则 ok=false、reason=not_foreground,且需要新客户端(窗口信息能力),旧客户端返回 -32005,可退回 restore。close/show 没有主机管理实现,转由主连接版 operate_window 处理(因此动作后不复核,ok 只表示已下发;McpManage=0 时返回 -32006)——它们只在 McpMachine=0 时才以 operate_window 之名出现在 tools/list。dry_run=true 只核实 hwnd 并返回窗口信息,不下发动作。", MakeSchema(props, { "id", "action", "hwnd" }), ActionOutputSchema()); } { Json::Value props(Json::objectValue); props["id"] = HostIdProp(); - props["action"] = PropStrEnum(u8"start/stop/pause/continue/restart", - { "start", "stop", "pause", "continue", "restart" }); - props["name"] = PropStr(u8"服务名(ServiceName,不是显示名),取 host_inspect 的 win32_services / driver_services 项的 name"); + props["action"] = PropStrEnum(u8"start/stop/pause/continue/restart," + u8"以及 set_auto/set_manual(转主连接实现,需 McpManage=1)", + { "start", "stop", "pause", "continue", "restart", + "set_auto", "set_manual" }); + props["name"] = PropStr(u8"服务名(ServiceName,不是显示名),取 host_inspect 的 win32_services / driver_services 项的 name;与 service_name 二选一"); + props["service_name"] = PropStr(u8"name 的旧参数名别名(与 name 等价,二选一)"); props["dry_run"] = PropBool(u8"true 时只校验服务存在并返回当前状态,不下发控制命令,默认 false"); AddTool(tools, "host_control_service", - u8"控制在线 Windows 主机的服务:start/stop/pause/continue/restart。客户端对每次控制都会自动回一份刷新后的服务列表,工具据此复核状态并如实返回 ok;stop 与 restart 归入危险操作,需要同时开启 McpMachineDangerous。restart 先 stop 再 start。不提供删除服务。dry_run=true 只核实服务并返回当前状态,不下发命令(是否已是目标状态由你按 state 判断)。", - MakeSchema(props, { "id", "action", "name" }), + u8"控制在线 Windows 主机的服务:start/stop/pause/continue/restart/set_auto/set_manual。start 等五个动作走主机管理链路:客户端对每次控制都会自动回一份刷新后的服务列表,工具据此复核状态并如实返回 ok;stop 与 restart 归入危险操作,需要同时开启 McpMachineDangerous;restart 先 stop 再 start。set_auto/set_manual 没有主机管理实现,转由主连接版 control_service 处理(ok 只表示已下发、需另查状态;McpManage=0 时返回 -32006)——它们只在 McpMachine=0 时才以 control_service 之名出现在 tools/list。不提供删除服务。dry_run=true 只核实服务并返回当前状态,不下发命令(是否已是目标状态由你按 state 判断);set_auto/set_manual 不支持 dry_run(返回 -32602)。", + MakeSchema(props, { "id", "action" }), ActionOutputSchema()); } { diff --git a/server/2015Remote/McpServer.cpp b/server/2015Remote/McpServer.cpp index 2e13132..5098f73 100644 --- a/server/2015Remote/McpServer.cpp +++ b/server/2015Remote/McpServer.cpp @@ -289,8 +289,10 @@ const char* kServerInstructions = u8"get_keylog 也始终可见,只读模式下只是它的 clear / set_offline 参数被拒,读取不受影响。\n" u8"同一能力存在两套工具时优先用主机管理族:host_inspect(aspects 选 processes / windows / " u8"win32_services 等,支持 pid/title/status 等服务端过滤,字段更全),list_processes / " - u8"list_windows / list_services 是它在 McpMachine=0 时的降级路径。关闭窗口只有 operate_window 能做," - u8"host_control_window 没有 close。\n" + u8"list_windows / list_services 是它在 McpMachine=0 时的降级路径。窗口与服务控制同样只有一套" + u8"名字(需 McpManage=1):McpMachine=0 时是 operate_window / control_service,开启后是 " + u8"host_control_window / host_control_service;后者也接受 close/show 与 set_auto/set_manual" + u8"(转给前一套实现)。\n" u8"\n" u8"3) 错误码与动作\n" u8"-32002 主机离线或不存在:先刷新主机列表再选,不要拿旧快照反复重试。\n" @@ -2627,6 +2629,10 @@ std::string BuildToolsListResult(const Json::Value& id) { // 18) kill_process / operate_window / control_service / power_action(P7:设备管理控制, // 除 kill_process 外仅 Windows,安全门:McpManage=1 且 McpReadonly=0) if (CMcpServer::Instance().IsManageEnabled() && !CMcpServer::Instance().IsReadonly()) { + // P2-2:窗口/服务控制各只保留一套名字。主机管理开启时由 host_control_window / + // host_control_service 承担(它们也接受本工具独有的动作),这里不再重复暴露; + // 关闭主机管理时这两条主连接路径是唯一可用的实现,必须列出。名字始终可调用。 + const bool showMainControls = !CMcpServer::Instance().IsMachineEnabled(); { Json::Value tool(Json::objectValue); tool["name"] = "kill_process"; @@ -2635,18 +2641,18 @@ std::string BuildToolsListResult(const Json::Value& id) { tool["outputSchema"] = BuildKillProcessOutputSchema(); tools.append(tool); } - { + if (showMainControls) { Json::Value tool(Json::objectValue); tool["name"] = "operate_window"; - tool["description"] = u8"对指定在线 Windows 主机的窗口执行操作:close(关闭)/show(显示)/hide(隐藏)/minimize(最小化)/maximize(最大化)/restore(还原)/activate(激活到前台)。hwnd 取 list_windows 或 host_inspect 的 windows 项返回的 hwnd。ok 仅表示已下发(窗口句柄可能已失效);action=activate 例外:工具会在动作后用窗口信息 RPC 复核 GetForegroundWindow,返回 ok/state(foreground|background|missing)/window,旧客户端不支持时返回 -32005(可退回 show)。关闭窗口只有本工具能做,host_control_window 不提供 close,且它会重新枚举可见窗口复核结果。dry_run=true 只校验 hwnd 是否还在可见窗口列表里并返回该窗口信息,不发送任何命令。"; + tool["description"] = u8"对指定在线 Windows 主机的窗口执行操作:close(关闭)/show(显示)/hide(隐藏)/minimize(最小化)/maximize(最大化)/restore(还原)/activate(激活到前台)。hwnd 取 list_windows 或 host_inspect 的 windows 项返回的 hwnd。ok 仅表示已下发(窗口句柄可能已失效);action=activate 例外:工具会在动作后用窗口信息 RPC 复核 GetForegroundWindow,返回 ok/state(foreground|background|missing)/window,旧客户端不支持时返回 -32005(可退回 show)。仅在 McpMachine=0(主机管理工具关闭)时出现在 tools/list;开启后请用 host_control_window,它也接受 close/show。dry_run=true 只校验 hwnd 是否还在可见窗口列表里并返回该窗口信息,不发送任何命令。"; tool["inputSchema"] = BuildOperateWindowInputSchema(); tool["outputSchema"] = BuildOperateWindowOutputSchema(); tools.append(tool); } - { + if (showMainControls) { Json::Value tool(Json::objectValue); tool["name"] = "control_service"; - tool["description"] = u8"控制指定在线 Windows 主机的服务:start(启动)/stop(停止)/set_auto(设为自动)/set_manual(设为手动)。service_name 取 list_services 返回的 service_name。ok 仅表示已下发(成功与否需再查 list_services 核对状态)。"; + tool["description"] = u8"控制指定在线 Windows 主机的服务:start(启动)/stop(停止)/set_auto(设为自动)/set_manual(设为手动)。service_name 取 list_services 返回的 service_name。ok 仅表示已下发(成功与否需再查 list_services 核对状态)。仅在 McpMachine=0(主机管理工具关闭)时出现在 tools/list;开启后请用 host_control_service,它也接受 set_auto/set_manual。"; tool["inputSchema"] = BuildControlServiceInputSchema(); tool["outputSchema"] = BuildControlServiceOutputSchema(); tools.append(tool); @@ -12083,6 +12089,41 @@ std::string DispatchSingleHostTool(const Json::Value& id, const std::string& too if (!platErr.empty()) return platErr; } + // P2-2:两对控制工具合并后的动作路由。tools/list 里随 McpMachine 只出现一套名字, + // 但两套名字都能调用:host_control_window 的 close/show 没有主机管理实现,转给主连接版 + // operate_window;host_control_service 的 set_auto/set_manual 转给 control_service。 + // 两者都先过主机管理总开关(与其它 host_* 一致),旧实现再各自复核 McpManage/McpReadonly。 + if (toolName == "host_control_window" || toolName == "host_control_service") { + if (!McpServer().IsMachineEnabled()) + return BuildError(id, -32006, + "Host management tools are disabled (set McpMachine=1 to enable)"); + std::string action = ToLowerAscii(Trim(GetStringArg(args, "action"))); + if (toolName == "host_control_window" && (action == "close" || action == "show")) + return BuildOperateWindow(id, args, parent); + if (toolName == "host_control_service") { + if (action == "set_auto" || action == "set_manual") { + // control_service 不支持 dry_run:与其假装试运行,不如明确拒绝。 + if (args.isMember("dry_run")) { + if (!args["dry_run"].isBool()) + return BuildError(id, -32602, "dry_run must be a boolean"); + if (args["dry_run"].asBool()) + return BuildError(id, -32602, + "dry_run is not supported for set_auto/set_manual"); + } + Json::Value a(args); + if (!a.isMember("service_name") && a.isMember("name")) + a["service_name"] = args["name"]; + return BuildControlService(id, a, parent); + } + // 旧的 control_service 参数名 service_name 在这里同样可用。 + if (!args.isMember("name") && args.isMember("service_name")) { + Json::Value a(args); + a["name"] = args["service_name"]; + return McpMachine::Call(id, toolName, a, parent); + } + } + } + // P8:连接管理 / 主机管理工具族(独立分发;门控在 McpMachine::Call 内复核)。 if (McpMachine::Handles(toolName)) return McpMachine::Call(id, toolName, args, parent); diff --git a/tools/mcp_tools/make_tools_json.py b/tools/mcp_tools/make_tools_json.py index 51926df..f3e4634 100644 --- a/tools/mcp_tools/make_tools_json.py +++ b/tools/mcp_tools/make_tools_json.py @@ -20,8 +20,11 @@ DST = sys.argv[2] OFF_SRC = sys.argv[3] if len(sys.argv) > 3 else None # 只在 McpMachine=0 时出现的降级工具(gate = "!machine")。改动这个集合要同步更新 -# McpServer.cpp 里 showDegradedLists 那段。 -MACHINE_OFF_FALLBACKS = {"list_processes", "list_windows", "list_services"} +# McpServer.cpp 里 showDegradedLists / showMainControls 两段。 +# P2-2:operate_window / control_service 与 host_inspect / host_control_* 之间同样按 +# McpMachine 二选一暴露(动作并集,两套名字都可调用)。 +MACHINE_OFF_FALLBACKS = {"list_processes", "list_windows", "list_services", + "operate_window", "control_service"} # gate: 该工具要求哪个服务端开关为开(null = 无条件出现) # write: 需要 !McpReadonly @@ -34,6 +37,8 @@ GATE = { # ---- McpMachine=0 时的降级路径(P2-2)---- # 开启主机管理后它们由 host_inspect 的 processes / windows / win32_services 项取代, # 所以只在开关关闭时进 tools/list(名字仍可作为别名调用)。 + # 同理,operate_window / control_service(见下方单开关段)在开启后被 + # host_control_window / host_control_service 取代。 "list_processes": ("!machine", False, False), "list_windows": ("!machine", False, False), "list_services": ("!machine", False, False), @@ -82,8 +87,10 @@ GATE = { # ---- manage(McpServer 侧的危险操作)---- "kill_process": ("manage", True, False), - "operate_window": ("manage", True, False), - "control_service": ("manage", True, False), + # P2-2:主机管理开启时由 host_control_window / host_control_service 承担 + # (后两者也接受 close/show、set_auto/set_manual),所以只在 McpMachine=0 时暴露。 + "operate_window": ("!machine", True, True), + "control_service": ("!machine", True, True), "power_action": ("manage", True, False), "uninstall_host": ("manage", True, False),