Fix: Expose tool payloads in the content text block

List and query tools put only a human summary in content and kept the real
payload in structuredContent. Clients that hand just content to the model
(WorkBuddy does) therefore never saw a host id: search_hosts answered "1 host
matched" and every follow-up tool was out of reach, and guessed ids 1-3100
could never work because host ids are 64-bit snowflake values.

BuildResult now appends the serialized structuredContent as a second text
block, as the MCP spec recommends for tools with structured output. The append
sits in the single result choke point, so every tool in McpServer, McpMachine
and McpChat gains it, including future ones; empty action results such as
remote_mouse keep their bare "ok".

Tools whose text already carries the payload expose only the difference:
exec_command and terminal_exec add exit_code, get_keylog adds counters,
get_activity_history exposes records without the duplicated raw dump, and the
hosts file reader drops its copy of the content. The override rides in a $
key that BuildResult consumes and removes, so responses change only by the
extra block.

Verified on a Debug instance: list_all_hosts, search_hosts, get_server_status
and list_registry return parseable JSON in content[1] with ids intact, and
tools/mcp_smoke.ps1 -SkipHost passes 27/27 with four new assertions.

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-10-26 14:12:50 +01:00
1 parent a7dd86ff0f
commit 27d40f5678
6 files changed
+153 -5

No files matched your search

+22
View File
@@ -27,6 +27,28 @@ std::string JsonToString(const Json::Value& v);
std::string BuildResult(const Json::Value& id, const Json::Value& result);
std::string BuildError(const Json::Value& id, int code, const std::string& msg);
// ===== 结果外露(content 文本块)=====
//
// MCP 规范(Tools > Structured Content)要求:返回 structuredContent 的工具,
// 应同时把序列化后的 JSON 放进一个 TextContent 块。有些客户端(如 WorkBuddy)
// 只把 content 交给模型、完全忽略 structuredContent——不补这一块,模型就拿不到
// id / session_id / pid 这类串联下一跳的关键字段,只能看到「共 N 台主机匹配」。
//
// 因此 BuildResult 默认给每个带非空 structuredContent 的结果追加一个文本块
// (紧凑 JSON)。工具的正文已经承载同一份载荷时,用 SetStructuredTextPayload
// 指定只外露哪部分,避免同一份内容在上下文里出现两三次:
//
// exec_command / terminal_exec → {"exit_code":N} (stdout 已在正文里)
// get_keylog → 行数/字节数等元数据(行已在正文里)
// get_activity_history → {"records":[…]}
// host_get_hosts_file → 去掉 content 的 sc (原文已在 lines 里)
void AppendStructuredText(Json::Value& result, const Json::Value& payload); // 内部用
void AppendStructuredText(Json::Value& result); // 内部用
// 本次结果只外露 payload(BuildResult 消费后移除,不出现在响应里)。
// payload 为空对象/空数组时本次不追加文本块。
void SetStructuredTextPayload(Json::Value& result, const Json::Value& payload);
// 仅 ASCII 小写化;不区分大小写的子串匹配
std::string ToLowerAscii(const std::string& s);
bool ContainsCI(const std::string& haystack, const std::string& needle);