Feature: Add a get_server_status tool to the MCP server
Answering "did my rebuild actually take effect" or "why was that tool refused" used to mean reading list_online_hosts and get_audit_log and inferring the rest. A single read-only call now returns the server's own state: brand and version, the exe link time (taken from the file's last write, which unlike __DATE__ reflects the whole build), how long the process has been up, the address and port MCP is actually listening on, every MCP switch, online and offline host counts, the number of sessions in flight per family, and how many source IPs are currently blocked. The bind and port are recorded by Start rather than re-read from the configuration, so the tool reports what the listener really got. Session counters take each subsystem's lock in turn and release it immediately, so the numbers are an estimate of one moment, not a consistent snapshot. Nothing sensitive is exposed: the token and the command whitelist never appear, and the switches are reported as booleans only. The tool is registered as read-only, non-destructive, idempotent and server-local, which brings tools/list to 53 entries. The smoke test gains three checks for it, including one that fails if a token-shaped field ever appears in the output. Verified on the Debug instance: the summary line reports version, uptime and listener correctly, sessions.terminal goes 0 to 1 while a persistent terminal is open and back to 0 after terminal_close, and the smoke test passes 26/26. Co-Authored-By: deepseek-flash
This commit is contained in:
1 parent
ae37e812ee
commit
5ce453797c
5 files changed
+231
-2
No files matched your search
+23
-1
@@ -518,7 +518,7 @@ httplib 默认值对 MCP 场景不合适,改为显式配置:
|
||||
- `tools/list` 每个工具都带 `annotations`:`readOnlyHint` / `destructiveHint` / `idempotentHint` / `openWorldHint`,由 `McpInternal.cpp` 的 `kToolHints` 表按名登记,末尾统一补一次。
|
||||
- 未登记的工具按规范默认值处理(`destructiveHint=true`),即**新增工具必须显式登记**,漏登记只会更保守,不会误标成只读。
|
||||
- 只读工具的 `destructiveHint` 一律 `false`(按规范对只读工具无意义,避免客户端误报「破坏性」)。
|
||||
- `openWorldHint=false` 只给「只读服务端本机状态」的工具(`list_online_hosts` / `search_hosts` / `list_all_hosts` / `get_host_detail` / `get_activity_history` / `get_audit_log`),其余都会触达外部主机。
|
||||
- `openWorldHint=false` 只给「只读服务端本机状态」的工具(`list_online_hosts` / `search_hosts` / `list_all_hosts` / `get_host_detail` / `get_activity_history` / `get_audit_log` / `get_server_status`),其余都会触达外部主机。
|
||||
|
||||
### 10.3 A4 `get_host_detail` 字段补全
|
||||
|
||||
@@ -604,3 +604,25 @@ token 只从 `-Token` / env 读取,不落仓库、不打印;无在线主机
|
||||
- outputSchema 补 `total/totalAll/offset/limit/hasMore`(与其它列表工具一致)。
|
||||
- 注意 `get_audit_log` 读的是**主界面消息镜像**,不是 `logs\mcp_audit.log`;两者的条目内容和保留策略都不同(前者是 1000 条环形缓冲,后者是 5 MB 文件滚动)。
|
||||
- 验证(Debug 实例):`limit=2` 两页分别返回第 1-2 / 3-4 条且 `hasMore=true`,`offset=999` 返回「offset 超出范围」;`type=mcp` 命中 `MCP命令执行`(大小写不敏感)、`type=提示` 命中「使用提示 / 提示信息」、`keyword=监听` 命中「监听端口: 6543成功」;冒烟脚本 23/23 通过(新增分页字段、条数上限、过滤未命中 3 项断言)。
|
||||
|
||||
### 11.2 C2 新工具 `get_server_status`
|
||||
|
||||
排查「改动到底生效没有」「为什么某个工具被拒」时,原先只能靠 `list_online_hosts` 加 `get_audit_log` 侧面推断;现在有一个服务端自身的状态快照。纯内存、只读、不建子连接。
|
||||
|
||||
返回(`structuredContent.status`):
|
||||
|
||||
| 字段 | 含义 |
|
||||
|---|---|
|
||||
| `app_name` / `version` | 品牌名与 `BRAND_VERSION`(当前 1.4.1) |
|
||||
| `exe_build_time` | exe 文件的最后写入时间=链接时间(比 `__DATE__`/`__TIME__` 可靠,后者是编译单个 .cpp 的时间) |
|
||||
| `uptime_sec` | 进程已运行秒数(`GetProcessTimes` 的创建时间起算,不是开机时间,也不受 MCP 启停影响) |
|
||||
| `bind` / `port` | MCP 实际监听的地址与端口(`Start` 时记录,非从配置回读) |
|
||||
| `switches` | `readonly` / `terminal` / `remote_control` / `file_transfer` / `manage` / `machine` / `machine_dangerous` / `keylog` / `chat` |
|
||||
| `hosts` | `online` / `offline` 台数(与 `list_all_hosts` 同源) |
|
||||
| `sessions` | `pending`(一次性/管理/键盘记录挂起) / `terminal` / `remote_control` / `file_transfer` / `chat` 在飞会话数 |
|
||||
| `blocked_auth_ips` | 当前处于鉴权封禁期的来源 IP 数 |
|
||||
|
||||
- **不返回** token、`McpCmdWhitelist` 内容或任何配置原文;`switches` 只报布尔值。
|
||||
- `sessions` 各项各取各自的锁、取完即放,是「大致同一时刻」的估算值,不能当一致性快照用。
|
||||
- 工具注解登记为只读 + 非破坏性 + 幂等 + 不触达外部主机;`tools/list` 总数变为 53。
|
||||
- 验证(Debug 实例):返回 `Yama v1.4.1,已运行 17 秒,监听 127.0.0.1:6545;在线主机 1 台、离线 3160 台`;`terminal_open` 期间 `sessions.terminal=1`、`terminal_close` 后回到 0;冒烟脚本 26/26 通过(新增 3 项:正常返回、版本/端口/开关齐全、不含 token)。
|
||||
@@ -323,7 +323,7 @@ struct ToolHints {
|
||||
bool openWorld; // false = 只读服务端本机状态(主机列表、审计日志),不触达外部主机
|
||||
};
|
||||
|
||||
// 52 个工具的语义登记表。新增工具必须在此登记(漏登记 → 走默认 destructiveHint=true)。
|
||||
// 53 个工具的语义登记表。新增工具必须在此登记(漏登记 → 走默认 destructiveHint=true)。
|
||||
const ToolHints kToolHints[] = {
|
||||
// --- 服务端本机只读 ---
|
||||
{ "list_online_hosts", true, false, true, false },
|
||||
@@ -332,6 +332,7 @@ const ToolHints kToolHints[] = {
|
||||
{ "get_host_detail", true, false, true, false },
|
||||
{ "get_activity_history", true, false, true, false },
|
||||
{ "get_audit_log", true, false, true, false },
|
||||
{ "get_server_status", true, false, true, false },
|
||||
// --- 主机只读 ---
|
||||
{ "list_processes", true, false, true, true },
|
||||
{ "list_windows", true, false, true, true },
|
||||
|
||||
@@ -1133,6 +1133,36 @@ Json::Value BuildAuditLogOutputSchema() {
|
||||
return schema;
|
||||
}
|
||||
|
||||
// get_server_status 的 outputSchema(服务端自身状态;嵌套对象只声明类型,不逐字段展开)
|
||||
Json::Value BuildServerStatusOutputSchema() {
|
||||
Json::Value props(Json::objectValue);
|
||||
|
||||
const char* strFields[] = { "app_name", "version", "exe_build_time", "bind" };
|
||||
for (const char* f : strFields) {
|
||||
Json::Value s(Json::objectValue);
|
||||
s["type"] = "string";
|
||||
props[f] = s;
|
||||
}
|
||||
const char* intFields[] = { "uptime_sec", "port", "blocked_auth_ips" };
|
||||
for (const char* f : intFields) {
|
||||
Json::Value n(Json::objectValue);
|
||||
n["type"] = "integer";
|
||||
props[f] = n;
|
||||
}
|
||||
const char* objFields[] = { "switches", "hosts", "sessions" };
|
||||
for (const char* f : objFields) {
|
||||
Json::Value o(Json::objectValue);
|
||||
o["type"] = "object";
|
||||
props[f] = o;
|
||||
}
|
||||
|
||||
Json::Value schema(Json::objectValue);
|
||||
schema["type"] = "object";
|
||||
schema["properties"] = props;
|
||||
schema["required"] = Json::Value(Json::arrayValue);
|
||||
return schema;
|
||||
}
|
||||
|
||||
// get_screenshot 的 outputSchema(image 元数据;base64 数据在 content 的 image 块中)
|
||||
Json::Value BuildScreenshotOutputSchema() {
|
||||
Json::Value props(Json::objectValue);
|
||||
@@ -1552,6 +1582,23 @@ std::string BuildToolsListResult(const Json::Value& id) {
|
||||
tools.append(tool);
|
||||
}
|
||||
|
||||
// 11b) get_server_status(服务端自身状态:版本/运行时长/开关/监听地址/计数,纯内存)
|
||||
{
|
||||
Json::Value tool(Json::objectValue);
|
||||
tool["name"] = "get_server_status";
|
||||
tool["description"] = u8"获取 YAMA 服务端自身的状态:程序版本与 exe 构建时间、进程已运行秒数、MCP 监听地址与端口、各功能开关(只读/终端/远程控制/文件传输/管理/主机管理/高风险操作/键盘记录/远程交谈)、在线与离线主机数、各类在飞会话数、当前被封禁的来源 IP 数。只读、纯内存,不返回 token。";
|
||||
|
||||
Json::Value inputSchema(Json::objectValue);
|
||||
inputSchema["type"] = "object";
|
||||
inputSchema["properties"] = Json::Value(Json::objectValue);
|
||||
inputSchema["required"] = Json::Value(Json::arrayValue);
|
||||
tool["inputSchema"] = inputSchema;
|
||||
|
||||
tool["outputSchema"] = BuildServerStatusOutputSchema();
|
||||
|
||||
tools.append(tool);
|
||||
}
|
||||
|
||||
// 12) list_registry(P3:注册表查询,列根键/列子键+值,仅 Windows,一次性子链接)
|
||||
{
|
||||
Json::Value tool(Json::objectValue);
|
||||
@@ -2763,6 +2810,109 @@ std::string BuildGetAuditLog(const Json::Value& id, const Json::Value& args, CMy
|
||||
return BuildResult(id, result);
|
||||
}
|
||||
|
||||
// tools/call:get_server_status(服务端自身状态:版本/构建时间/运行时长/开关/监听地址/计数)
|
||||
std::string BuildGetServerStatus(const Json::Value& id, CMy2015RemoteDlg* parent) {
|
||||
CMcpServer& srv = CMcpServer::Instance();
|
||||
|
||||
Json::Value status(Json::objectValue);
|
||||
status["app_name"] = BRAND_APP_NAME;
|
||||
status["version"] = VERSION_STR;
|
||||
|
||||
// exe 的最后写入时间就是链接时间,比 __DATE__/__TIME__ 可靠(后者是编译本 .cpp 的时间)。
|
||||
{
|
||||
char exe[MAX_PATH] = { 0 };
|
||||
if (GetModuleFileNameA(NULL, exe, MAX_PATH) > 0) {
|
||||
WIN32_FILE_ATTRIBUTE_DATA fad;
|
||||
if (GetFileAttributesExA(exe, GetFileExInfoStandard, &fad)) {
|
||||
FILETIME local = fad.ftLastWriteTime;
|
||||
FileTimeToLocalFileTime(&fad.ftLastWriteTime, &local);
|
||||
SYSTEMTIME st = { 0 };
|
||||
if (FileTimeToSystemTime(&local, &st)) {
|
||||
char buf[32] = { 0 };
|
||||
wsprintfA(buf, "%04d-%02d-%02d %02d:%02d:%02d",
|
||||
(int)st.wYear, (int)st.wMonth, (int)st.wDay,
|
||||
(int)st.wHour, (int)st.wMinute, (int)st.wSecond);
|
||||
status["exe_build_time"] = buf;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 进程运行时长:从进程创建时间算起(不是系统开机时间,也不受 MCP 启停影响)。
|
||||
{
|
||||
FILETIME createTime, exitTime, kernelTime, userTime, nowFt;
|
||||
if (GetProcessTimes(GetCurrentProcess(), &createTime, &exitTime, &kernelTime, &userTime)) {
|
||||
GetSystemTimeAsFileTime(&nowFt);
|
||||
ULARGE_INTEGER c, n;
|
||||
c.LowPart = createTime.dwLowDateTime; c.HighPart = createTime.dwHighDateTime;
|
||||
n.LowPart = nowFt.dwLowDateTime; n.HighPart = nowFt.dwHighDateTime;
|
||||
LONGLONG ticks100ns = (LONGLONG)(n.QuadPart - c.QuadPart);
|
||||
if (ticks100ns > 0) status["uptime_sec"] = (Json::Int64)(ticks100ns / 10000000LL);
|
||||
}
|
||||
}
|
||||
|
||||
status["bind"] = srv.GetBind();
|
||||
status["port"] = srv.GetPort();
|
||||
|
||||
// 开关只报「开/关」,不输出 token、白名单内容等敏感配置。
|
||||
Json::Value switches(Json::objectValue);
|
||||
switches["readonly"] = srv.IsReadonly();
|
||||
switches["terminal"] = srv.IsTerminalEnabled();
|
||||
switches["remote_control"] = srv.IsRemoteControlEnabled();
|
||||
switches["file_transfer"] = srv.IsFileTransferEnabled();
|
||||
switches["manage"] = srv.IsManageEnabled();
|
||||
switches["machine"] = srv.IsMachineEnabled();
|
||||
switches["machine_dangerous"] = srv.IsMachineDangerousEnabled();
|
||||
switches["keylog"] = srv.IsKeylogEnabled();
|
||||
switches["chat"] = srv.IsChatEnabled();
|
||||
status["switches"] = switches;
|
||||
|
||||
int onlineCount = 0, offlineCount = 0;
|
||||
if (parent) {
|
||||
Json::Value all(Json::arrayValue);
|
||||
CollectAllHosts(parent, all, true, true);
|
||||
for (Json::Value::ArrayIndex i = 0; i < all.size(); ++i) {
|
||||
if (all[i]["online"].asBool()) onlineCount++;
|
||||
else offlineCount++;
|
||||
}
|
||||
}
|
||||
Json::Value hosts(Json::objectValue);
|
||||
hosts["online"] = onlineCount;
|
||||
hosts["offline"] = offlineCount;
|
||||
status["hosts"] = hosts;
|
||||
|
||||
int pending = 0, terminal = 0, screenCtrl = 0, fileXfer = 0, chat = 0, blockedIps = 0;
|
||||
srv.GetRuntimeCounters(pending, terminal, screenCtrl, fileXfer, chat, blockedIps);
|
||||
Json::Value sessions(Json::objectValue);
|
||||
sessions["pending"] = pending; // 一次性/管理/键盘记录挂起会话
|
||||
sessions["terminal"] = terminal;
|
||||
sessions["remote_control"] = screenCtrl;
|
||||
sessions["file_transfer"] = fileXfer;
|
||||
sessions["chat"] = chat;
|
||||
status["sessions"] = sessions;
|
||||
status["blocked_auth_ips"] = blockedIps;
|
||||
|
||||
Json::Value result(Json::objectValue);
|
||||
Json::Value structuredContent(Json::objectValue);
|
||||
structuredContent["status"] = status;
|
||||
result["structuredContent"] = structuredContent;
|
||||
|
||||
std::string text = std::string(BRAND_APP_NAME) + " v" + VERSION_STR +
|
||||
std::string(u8",已运行 ") + std::to_string((long long)status["uptime_sec"].asInt64()) +
|
||||
std::string(u8" 秒,监听 ") + srv.GetBind() + ":" + std::to_string(srv.GetPort()) +
|
||||
std::string(u8";在线主机 ") + std::to_string(onlineCount) +
|
||||
std::string(u8" 台、离线 ") + std::to_string(offlineCount) + std::string(u8" 台。");
|
||||
Json::Value content(Json::arrayValue);
|
||||
Json::Value item(Json::objectValue);
|
||||
item["type"] = "text";
|
||||
item["text"] = text;
|
||||
content.append(item);
|
||||
result["content"] = content;
|
||||
result["isError"] = false;
|
||||
|
||||
return BuildResult(id, result);
|
||||
}
|
||||
|
||||
// ===== P4:持久远程终端(terminal_open / terminal_exec / terminal_close)=====
|
||||
|
||||
static const int kTermIdleTimeoutSec = 300; // 持久终端 idle 回收超时(秒)
|
||||
@@ -5844,6 +5994,7 @@ std::string DispatchSingleHostTool(const Json::Value& id, const std::string& too
|
||||
if (toolName == "get_client_log") return BuildGetClientLog(id, args, parent);
|
||||
if (toolName == "get_keylog") return BuildGetKeylog(id, args, parent);
|
||||
if (toolName == "get_audit_log") return BuildGetAuditLog(id, args, parent);
|
||||
if (toolName == "get_server_status") return BuildGetServerStatus(id, parent);
|
||||
if (toolName == "list_registry") return BuildListRegistry(id, args, parent);
|
||||
if (toolName == "exec_command") return BuildExecCommand(id, args, parent);
|
||||
if (toolName == "terminal_open") return BuildTerminalOpen(id, args, parent);
|
||||
@@ -6309,6 +6460,9 @@ bool CMcpServer::Start(const std::string& bind, int port) {
|
||||
m_server.set_write_timeout(60, 0);
|
||||
m_server.new_task_queue = []() { return new httplib::ThreadPool(32); };
|
||||
|
||||
m_bind = bind;
|
||||
m_port = port;
|
||||
|
||||
m_thread = std::thread([this, bind, port]() {
|
||||
m_server.listen(bind, port);
|
||||
});
|
||||
@@ -6327,6 +6481,38 @@ void CMcpServer::Stop() {
|
||||
m_running.store(false);
|
||||
}
|
||||
|
||||
// get_server_status:各类在飞会话 + 当前被封禁的来源 IP 计数。
|
||||
// 每类只取自己的锁且立刻释放,不跨锁持有一致性(这些数字只作观测用)。
|
||||
void CMcpServer::GetRuntimeCounters(int& pending, int& terminal, int& screenCtrl,
|
||||
int& fileXfer, int& chat, int& blockedIps) {
|
||||
{
|
||||
std::lock_guard<std::mutex> lk(m_PendingMutex);
|
||||
pending = (int)m_Pending.size();
|
||||
}
|
||||
{
|
||||
std::lock_guard<std::mutex> lk(m_TermMutex);
|
||||
terminal = (int)m_TermSessions.size();
|
||||
}
|
||||
{
|
||||
std::lock_guard<std::mutex> lk(m_ScreenCtrlMutex);
|
||||
screenCtrl = (int)m_ScreenCtrlSessions.size();
|
||||
}
|
||||
{
|
||||
std::lock_guard<std::mutex> lk(m_FileXferMutex);
|
||||
fileXfer = (int)m_FileXferSessions.size();
|
||||
}
|
||||
{
|
||||
std::lock_guard<std::mutex> lk(m_ChatMutex);
|
||||
chat = (int)m_ChatSessions.size();
|
||||
}
|
||||
blockedIps = 0;
|
||||
auto now = std::chrono::steady_clock::now();
|
||||
std::lock_guard<std::mutex> lk(m_AuthMutex);
|
||||
for (const auto& kv : m_AuthFails) {
|
||||
if (kv.second.blockedUntil > now) blockedIps++;
|
||||
}
|
||||
}
|
||||
|
||||
// ===== 鉴权加固(N4)=====
|
||||
//
|
||||
// 两件事:
|
||||
|
||||
@@ -419,6 +419,15 @@ public:
|
||||
bool IsReadonly() const { return m_readonly; }
|
||||
const std::string& GetCmdWhitelist() const { return m_cmdWhitelist; }
|
||||
|
||||
// 监听地址/端口(Start 时记录,供 get_server_status 报告)。
|
||||
const std::string& GetBind() const { return m_bind; }
|
||||
int GetPort() const { return m_port; }
|
||||
|
||||
// get_server_status:在飞会话与鉴权封禁计数。每类各取一次自己的锁,
|
||||
// 返回前全部释放——调用方拿到的是「大致同一时刻」的估算值,不用来同步。
|
||||
void GetRuntimeCounters(int& pending, int& terminal, int& screenCtrl,
|
||||
int& fileXfer, int& chat, int& blockedIps);
|
||||
|
||||
private:
|
||||
CMcpServer();
|
||||
~CMcpServer();
|
||||
@@ -460,6 +469,8 @@ private:
|
||||
std::thread m_thread;
|
||||
std::atomic<bool> m_running{false};
|
||||
std::string m_token;
|
||||
std::string m_bind; // Start 传入的监听地址
|
||||
int m_port = 0; // Start 传入的监听端口
|
||||
CMy2015RemoteDlg* m_parent = nullptr;
|
||||
|
||||
// 挂起请求注册表(受 m_PendingMutex 保护,键 = device_id)。
|
||||
|
||||
@@ -139,6 +139,15 @@ Check 'get_audit_log 条数不超过 limit' (@($audit.result.structuredContent.e
|
||||
$auditFiltered = Rpc '' $null 'get_audit_log' @{ type = 'zzz-no-such-type'; limit = 5 }
|
||||
Check 'get_audit_log 过滤未命中返回 0 条' ($auditFiltered.error -eq $null -and @($auditFiltered.result.structuredContent.entries).Count -eq 0)
|
||||
|
||||
$status = Rpc '' $null 'get_server_status' @{}
|
||||
$st = $status.result.structuredContent.status
|
||||
Check 'get_server_status 正常返回' ($status.error -eq $null -and $null -ne $st)
|
||||
$basePort = 6544
|
||||
if ($Base -match ':(\d+)(/|$)') { $basePort = [int]$Matches[1] }
|
||||
Check 'get_server_status 版本/端口/开关齐全' `
|
||||
([string]$st.version -ne '' -and [int]$st.port -eq $basePort -and $null -ne $st.switches -and $null -ne $st.sessions)
|
||||
Check 'get_server_status 不含 token' (($st | ConvertTo-Json -Depth 6) -notmatch '(?i)token')
|
||||
|
||||
if (-not $HostId) {
|
||||
$first = @($sc.hosts)[0]
|
||||
if ($first) { $HostId = [string]$first.id }
|
||||
|
||||
Reference in new issue
Block a user