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());
}
{