Feature: Harden the MCP transport and add non-destructive host tools
Cap MCP request bodies at 2 MB, set a 15 s idle read timeout and a 60 s
write timeout, and give httplib an explicit 32-thread pool: long tools
such as chat_wait hold a worker for up to 55 s, and the default pool of
8 to 15 left other callers queued.
Every tool returned by tools/list now carries readOnlyHint,
destructiveHint, idempotentHint and openWorldHint. The table is
per-tool; anything not registered defaults to destructive so a new
write tool cannot silently look harmless. Read-only tools are forced
to destructiveHint=false.
get_host_detail appends the technical fields the host store already
holds (cpuCores, memoryGB, systemBits, programBits, user, isAdmin,
installTime, expiredDate, monitors, exeVersion, clientPath, pid,
publicIp). The JSON writer is pinned to 15 significant digits so
memoryGB renders as 27.8 instead of 27.800000000000001. list hosts
output is unchanged.
Add host_set_remark and host_set_watch_level. Both validate the host
against the store, are refused in readonly mode, repaint the affected
row, and are audited without recording the value written.
Add search_file, which drives the client file-manager search channel
and returns paged results. The root path is normalized before sending:
the client enumerates with wsprintf("%s\*.*"), so a trailing
backslash produced C:\*.* and FindFirstFile failed, returning zero
hits instantly. The client only searches recursively, so non-recursive
requests are filtered server-side by comparing the parent directory.
Collection stops at 5000 records or 4 MB and the client is told to
stop; on timeout the partial results are returned. Search result
packets are never closed early: the sub-connection stays open until
TOKEN_SEARCH_FILE_FINISH, because the client's search thread holds a
pointer into the file manager object.
Batch reads report a skipped counter and are bounded to 50 ids and a
50 s deadline.
Add tools/mcp_smoke.ps1, a read-only end-to-end smoke test that asserts
auth rejection, the payload limit, annotation coverage, paging fields
and the main read tools. It takes the token from the environment and
never prints it.
Document all of the above in docs/Mcp_Design.md section 10, including
the client-side MAX_PATH stack overflow that a deep search_file call
can trigger, together with the workaround of searching shallow
directories.
Co-Authored-By: deepseek-flash
This commit is contained in:
1 parent
ac5b174b5f
commit
abe7e99a77
7 files changed
+1244
-11
No files matched your search
@@ -23,6 +23,10 @@ namespace McpInternal {
|
||||
std::string JsonToString(const Json::Value& v) {
|
||||
Json::StreamWriterBuilder b;
|
||||
b["indentation"] = "";
|
||||
// jsoncpp 默认按 17 位有效数字输出 double,27.8 会写成 27.800000000000001。
|
||||
// 我们只有 memoryGB 一个浮点字段(1 位小数),15 位足够且输出干净。
|
||||
b["precision"] = 15;
|
||||
b["precisionType"] = "significant";
|
||||
return Json::writeString(b, v);
|
||||
}
|
||||
|
||||
@@ -307,4 +311,109 @@ std::string PageSummary(const PageWindow& w, const char* unit) {
|
||||
return s + std::string(u8"。");
|
||||
}
|
||||
|
||||
// ===== 工具注解(A3)=====
|
||||
|
||||
namespace {
|
||||
|
||||
struct ToolHints {
|
||||
const char* name;
|
||||
bool readOnly;
|
||||
bool destructive;
|
||||
bool idempotent;
|
||||
bool openWorld; // false = 只读服务端本机状态(主机列表、审计日志),不触达外部主机
|
||||
};
|
||||
|
||||
// 52 个工具的语义登记表。新增工具必须在此登记(漏登记 → 走默认 destructiveHint=true)。
|
||||
const ToolHints kToolHints[] = {
|
||||
// --- 服务端本机只读 ---
|
||||
{ "list_online_hosts", true, false, true, false },
|
||||
{ "search_hosts", true, false, true, false },
|
||||
{ "list_all_hosts", true, false, true, false },
|
||||
{ "get_host_detail", true, false, true, false },
|
||||
{ "get_activity_history", true, false, true, false },
|
||||
{ "get_audit_log", true, false, true, false },
|
||||
// --- 主机只读 ---
|
||||
{ "list_processes", true, false, true, true },
|
||||
{ "list_windows", true, false, true, true },
|
||||
{ "list_files", true, false, true, true },
|
||||
{ "list_services", true, false, true, true },
|
||||
{ "get_client_log", true, false, true, true },
|
||||
{ "get_keylog", true, false, true, true },
|
||||
{ "get_screenshot", true, false, true, true },
|
||||
{ "list_registry", true, false, true, true },
|
||||
{ "download_file", true, false, true, true }, // 只读主机(写的是主控本机目录)
|
||||
{ "search_file", true, false, true, true },
|
||||
{ "list_network_connections", true, false, true, true },
|
||||
{ "host_list_processes", true, false, true, true },
|
||||
{ "host_list_windows", true, false, true, true },
|
||||
{ "host_list_network_state", true, false, true, true },
|
||||
{ "host_list_software", true, false, true, true },
|
||||
{ "host_list_browser_history", true, false, true, true },
|
||||
{ "host_list_favorites", true, false, true, true },
|
||||
{ "host_list_win32_services", true, false, true, true },
|
||||
{ "host_list_driver_services", true, false, true, true },
|
||||
{ "host_list_scheduled_tasks", true, false, true, true },
|
||||
{ "host_get_hosts_file", true, false, true, true },
|
||||
// --- 终端 ---
|
||||
{ "terminal_open", false, false, true, true },
|
||||
{ "terminal_exec", false, true, false, true }, // 任意命令,后果由命令决定
|
||||
{ "exec_command", false, true, false, true },
|
||||
{ "terminal_close", false, false, true, true },
|
||||
// --- 远程控制 ---
|
||||
{ "remote_open", false, false, true, true },
|
||||
{ "remote_close", false, false, true, true },
|
||||
{ "remote_keyboard", false, false, false, true },
|
||||
{ "remote_mouse", false, false, false, true },
|
||||
{ "remote_clipboard", false, false, false, true }, // 覆盖对端剪贴板
|
||||
// --- 文件传输 ---
|
||||
{ "upload_file", false, true, false, true }, // 覆盖/新增远端文件
|
||||
// --- 设备管理(McpManage)---
|
||||
{ "kill_process", false, true, false, true },
|
||||
{ "operate_window", false, true, false, true },
|
||||
{ "control_service", false, true, true, true }, // start/stop/set_* 重复执行同一状态
|
||||
{ "power_action", false, true, false, true },
|
||||
// --- 主机管理 / 连接管理(McpMachine)---
|
||||
{ "kill_network_connection", false, true, false, true },
|
||||
{ "host_control_process", false, true, false, true },
|
||||
{ "host_control_window", false, true, false, true },
|
||||
{ "host_control_service", false, true, true, true },
|
||||
{ "host_control_task", false, true, false, true },
|
||||
{ "host_set_remark", false, false, true, false }, // 只写服务端主机库
|
||||
{ "host_set_watch_level", false, false, true, false },
|
||||
// --- 远程交谈(McpChat)---
|
||||
{ "chat_open", false, false, false, true },
|
||||
{ "chat_send", false, false, false, true },
|
||||
{ "chat_wait", false, false, false, true }, // 会取走 inbox 消息,不是纯读
|
||||
{ "chat_close", false, false, true, true },
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
void ApplyToolAnnotations(Json::Value& tools) {
|
||||
if (!tools.isArray()) return;
|
||||
for (Json::Value& tool : tools) {
|
||||
if (!tool.isObject() || !tool["name"].isString()) continue;
|
||||
const std::string name = tool["name"].asString();
|
||||
|
||||
bool readOnly = false, destructive = true, idempotent = false, openWorld = true;
|
||||
for (const auto& h : kToolHints) {
|
||||
if (name == h.name) {
|
||||
readOnly = h.readOnly;
|
||||
destructive = h.destructive;
|
||||
idempotent = h.idempotent;
|
||||
openWorld = h.openWorld;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Json::Value ann(Json::objectValue);
|
||||
ann["readOnlyHint"] = readOnly;
|
||||
// 只读工具的 destructiveHint 按规范无意义,统一置 false,免得客户端误报「破坏性」。
|
||||
ann["destructiveHint"] = readOnly ? false : destructive;
|
||||
ann["idempotentHint"] = idempotent;
|
||||
ann["openWorldHint"] = openWorld;
|
||||
tool["annotations"] = ann;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace McpInternal
|
||||
Reference in new issue
Block a user