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
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-11-12 17:40:51 +01:00
1 parent c73db530f4
commit 5841f27df9
5 files changed
+218 -141

No files matched your search

+47 -6
View File
@@ -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);