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

+21 -10
View File
@@ -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());
}
{
+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);