Feature: Filter the MCP host lists by group, OS and client type

list_online_hosts and list_all_hosts could only page, so finding "the
hosts in this group" or "the Linux ones" meant walking pages or falling
back to search_hosts, which has no client type filter and treats online
and offline hosts differently.

Both tools now accept group, os and client_type. They are substring
matches, combined with AND and case-insensitive over ASCII, matching the
semantics search_hosts already used, and the shared implementation keeps
the two tools from drifting apart. The filter runs before paging, so
total counts the matches and totalAll keeps the pre-filter size, and the
summary switches to the "matches" wording with a trailing line naming
the unfiltered size - a mistyped condition then shows up immediately
instead of silently returning nothing.

Offline hosts carry no clientType in their stored record, so a
client_type filter never matches them; the tool description says so and
points at os for that case.

Verified on the Debug instance: client_type=exe matches the single EXE
host while client_type=lnx matches nothing and still reports totalAll=1;
list_all_hosts with group=default matches all 3161 recorded hosts and
splits them into 1 online and 3160 offline, and os=zzz matches none. The
smoke test gains two checks and passes 28/28.

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-10-25 14:37:28 +01:00
1 parent 5ce453797c
commit 929065e7be
3 files changed
+89 -8

No files matched your search

+75 -8
View File
@@ -580,6 +580,53 @@ void CollectAllHosts(CMy2015RemoteDlg* parent, Json::Value& hosts,
LeaveCriticalSection(&parent->m_cs);
}
// ===== C3:list_online_hosts / list_all_hosts 的公共过滤 =====
// 口径与 search_hosts 一致:条件都可选、AND 组合、子串匹配、ASCII 不区分大小写。
// 只过滤已有字段,不额外拉取主机数据;离线主机没有 clientType(历史记录里不存),
// 所以带 client_type 条件时离线主机不会命中——需要按平台筛历史主机请用 os。
struct HostListFilter {
std::string group;
std::string os;
std::string clientType;
bool Any() const { return !group.empty() || !os.empty() || !clientType.empty(); }
};
HostListFilter ReadHostListFilter(const Json::Value& args) {
HostListFilter f;
f.group = GetStringArg(args, "group");
f.os = GetStringArg(args, "os");
f.clientType = GetStringArg(args, "client_type");
return f;
}
void ApplyHostListFilter(const Json::Value& in, const HostListFilter& f, Json::Value& out) {
out = Json::Value(Json::arrayValue);
for (Json::Value::ArrayIndex i = 0; i < in.size(); ++i) {
const Json::Value& h = in[i];
if (!f.group.empty() && !ContainsCI(JsonStrField(h, "group"), f.group)) continue;
if (!f.os.empty() && !ContainsCI(JsonStrField(h, "os"), f.os)) continue;
if (!f.clientType.empty() && !ContainsCI(JsonStrField(h, "clientType"), f.clientType)) continue;
out.append(h);
}
}
void AppendHostListFilterProps(Json::Value& props) {
Json::Value groupProp(Json::objectValue);
groupProp["type"] = "string";
groupProp["description"] = u8"按分组过滤(子串,不区分大小写)。省略则不过滤。";
props["group"] = groupProp;
Json::Value osProp(Json::objectValue);
osProp["type"] = "string";
osProp["description"] = u8"按操作系统过滤(子串,不区分大小写,如 Windows、Linux、Android)。省略则不过滤。";
props["os"] = osProp;
Json::Value typeProp(Json::objectValue);
typeProp["type"] = "string";
typeProp["description"] = u8"按客户端类型过滤(子串,不区分大小写,如 EXE、LNX、MAC)。离线主机没有该字段,不会命中。省略则不过滤。";
props["client_type"] = typeProp;
}
// ========== 工具 schema ==========
// 单台主机字段 schema(hosts 数组元素 / 单机详情共用的形状)
@@ -698,6 +745,7 @@ Json::Value BuildListAllHostsInputSchema() {
offlineProp["description"] = u8"是否包含离线主机(历史连接过、当前未连接的),默认 true";
props["offline"] = offlineProp;
AppendHostListFilterProps(props);
AppendPagingProps(props);
Json::Value schema(Json::objectValue);
@@ -1365,11 +1413,12 @@ std::string BuildToolsListResult(const Json::Value& id) {
tool["name"] = "list_online_hosts";
// 说明文字为 UTF-8:项目 /execution-charset:.936 会把普通窄字面量编译成 GBK,
// 故用 u8 前缀确保输出到 JSON 的字节是 UTF-8。
tool["description"] = u8"获取当前所有在线主机的列表,包含计算机名、IP、操作系统、版本、备注、分组、活动窗口、延迟等实时信息。结果按 limit/offset 分页(默认 100 条,上限 1000),total 为在线主机总数,hasMore 表示还有下一页。tls=true 表示该客户端登录时使用了 TLS 加密传输(登录时上报,非实时探测;老客户端恒为 false)。";
tool["description"] = u8"获取当前所有在线主机的列表,包含计算机名、IP、操作系统、版本、备注、分组、活动窗口、延迟等实时信息。可选 group / os / client_type 三个过滤条件(均可选、AND 组合、子串匹配、ASCII 不区分大小写),过滤在分页之前生效:total 为命中台数,totalAll 为全部在线台数。结果按 limit/offset 分页(默认 100 条,上限 1000),hasMore 表示还有下一页。tls=true 表示该客户端登录时使用了 TLS 加密传输(登录时上报,非实时探测;老客户端恒为 false)。";
Json::Value inputSchema(Json::objectValue);
inputSchema["type"] = "object";
Json::Value props(Json::objectValue);
AppendHostListFilterProps(props);
AppendPagingProps(props);
inputSchema["properties"] = props;
inputSchema["required"] = Json::Value(Json::arrayValue);
@@ -1396,7 +1445,7 @@ std::string BuildToolsListResult(const Json::Value& id) {
{
Json::Value tool(Json::objectValue);
tool["name"] = "list_all_hosts";
tool["description"] = u8"列出服务端已知的全部主机:在线主机(实时信息)与离线主机(历史连接过、当前未连接,来自主机列表记录)。在线主机字段同 list_online_hosts;离线主机另有 last_seen(最后一次登录时间)、install_time 字段,但没有 rtt/version/activeWindow 等实时字段。authStatus 为主机授权状态(unauthorized/authorized_by_super/authorized_by_admin/forbidden)。默认两类都返回;结果按 limit/offset 分页(默认 100 条,上限 1000),total 为总条数。";
tool["description"] = u8"列出服务端已知的全部主机:在线主机(实时信息)与离线主机(历史连接过、当前未连接,来自主机列表记录)。在线主机字段同 list_online_hosts;离线主机另有 last_seen(最后一次登录时间)、install_time 字段,但没有 rtt/version/activeWindow 等实时字段,也没有 clientType。authStatus 为主机授权状态(unauthorized/authorized_by_super/authorized_by_admin/forbidden)。默认两类都返回;可选 group / os / client_type 过滤(AND 组合、子串匹配、ASCII 不区分大小写),过滤在分页之前生效:total 为命中台数,totalAll 为主机库总数。结果按 limit/offset 分页(默认 100 条,上限 1000)。";
tool["inputSchema"] = BuildListAllHostsInputSchema();
tool["outputSchema"] = BuildHostOutputSchema();
@@ -1810,9 +1859,15 @@ std::string BuildListOnlineHosts(const Json::Value& id, const Json::Value& args,
Json::Value all(Json::arrayValue);
CollectOnlineHosts(parent, all);
// 过滤在分页之前:total 是命中数,totalAll 是全部在线数(与 get_audit_log 同口径)。
HostListFilter filter = ReadHostListFilter(args);
Json::Value matched;
ApplyHostListFilter(all, filter, matched);
PageWindow page = ResolvePageWindow(args);
Json::Value hosts;
ApplyPageWindow(all, page, hosts);
ApplyPageWindow(matched, page, hosts);
if (filter.Any()) page.totalAll = (int)all.size();
Json::Value result(Json::objectValue);
Json::Value structuredContent(Json::objectValue);
@@ -1820,10 +1875,14 @@ std::string BuildListOnlineHosts(const Json::Value& id, const Json::Value& args,
WritePageFields(structuredContent, page);
result["structuredContent"] = structuredContent;
std::string text = PageSummary(page, filter.Any() ? u8"台主机匹配" : u8"台主机在线");
if (filter.Any())
text += std::string(u8"在线共 ") + std::to_string((int)all.size()) + std::string(u8" 台。");
Json::Value content(Json::arrayValue);
Json::Value item(Json::objectValue);
item["type"] = "text";
item["text"] = PageSummary(page, u8"台主机在线");
item["text"] = text;
content.append(item);
result["content"] = content;
result["isError"] = false;
@@ -1839,14 +1898,20 @@ std::string BuildListAllHosts(const Json::Value& id, const Json::Value& args, CM
Json::Value hosts(Json::arrayValue);
CollectAllHosts(parent, hosts, includeOnline, includeOffline);
// 过滤先于统计与分页:total/totalAll 与摘要里的在线/离线数都按命中条目算。
HostListFilter filter = ReadHostListFilter(args);
Json::Value matched;
ApplyHostListFilter(hosts, filter, matched);
int onlineCount = 0;
for (unsigned int i = 0; i < hosts.size(); ++i) {
if (hosts[i]["online"].asBool()) onlineCount++;
for (unsigned int i = 0; i < matched.size(); ++i) {
if (matched[i]["online"].asBool()) onlineCount++;
}
PageWindow page = ResolvePageWindow(args);
Json::Value pageItems;
ApplyPageWindow(hosts, page, pageItems);
ApplyPageWindow(matched, page, pageItems);
if (filter.Any()) page.totalAll = (int)hosts.size();
Json::Value result(Json::objectValue);
Json::Value structuredContent(Json::objectValue);
@@ -1857,9 +1922,11 @@ std::string BuildListAllHosts(const Json::Value& id, const Json::Value& args, CM
Json::Value content(Json::arrayValue);
Json::Value item(Json::objectValue);
item["type"] = "text";
std::string text = PageSummary(page, u8"台主机");
std::string text = PageSummary(page, filter.Any() ? u8"台主机匹配" : u8"台主机");
text += std::string(u8"其中在线 ") + std::to_string(onlineCount) +
std::string(u8",离线 ") + std::to_string(page.total - onlineCount) + std::string(u8"。");
if (filter.Any())
text += std::string(u8"主机库共 ") + std::to_string((int)hosts.size()) + std::string(u8" 台。");
item["text"] = text;
content.append(item);
result["content"] = content;