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

+41
View File
@@ -30,11 +30,52 @@ std::string JsonToString(const Json::Value& v) {
return Json::writeString(b, v);
}
// 结果外露控制键:SetStructuredTextPayload 写入,BuildResult 消费后移除,
// 不会出现在响应里。'$' 前缀确保与任何业务字段不冲突。
static const char kStructuredTextPayloadKey[] = "$structuredTextPayload";
void AppendStructuredText(Json::Value& result, const Json::Value& payload) {
if (!payload.isObject() && !payload.isArray())
return; // null/标量载荷没有可外露的结构
if (payload.empty())
return; // 动作类结果(remote_mouse 等的 {})无内容可补
Json::Value content = result.isMember("content") ? result["content"]
: Json::Value(Json::arrayValue);
if (!content.isArray())
content = Json::Value(Json::arrayValue);
Json::Value item(Json::objectValue);
item["type"] = "text";
item["text"] = JsonToString(payload); // 紧凑 JSON,可被模型原样解析
content.append(item);
result["content"] = content;
}
void AppendStructuredText(Json::Value& result) {
if (!result.isMember("structuredContent"))
return;
AppendStructuredText(result, result["structuredContent"]);
}
void SetStructuredTextPayload(Json::Value& result, const Json::Value& payload) {
result[kStructuredTextPayloadKey] = payload;
}
std::string BuildResult(const Json::Value& id, const Json::Value& result) {
Json::Value resp(Json::objectValue);
resp["jsonrpc"] = "2.0";
resp["id"] = id;
resp["result"] = result;
// resp["result"] 本来就是深拷贝,直接在上面补文本块,不多拷一次。
Json::Value& out = resp["result"];
if (out.isMember(kStructuredTextPayloadKey)) {
Json::Value payload = out[kStructuredTextPayloadKey];
out.removeMember(kStructuredTextPayloadKey);
AppendStructuredText(out, payload);
} else {
AppendStructuredText(out);
}
return JsonToString(resp);
}
+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);
+9 -2
View File
@@ -276,9 +276,13 @@ Json::Value ActionOutputSchema() {
}
std::string BuildStructuredResult(const Json::Value& id, const Json::Value& sc,
const std::string& text) {
const std::string& text,
const Json::Value* textPayload = nullptr) {
Json::Value result(Json::objectValue);
result["structuredContent"] = sc;
// 默认由 BuildResult 把 structuredContent 原样外露到 content;工具可指定更小的
// 载荷(例如 hosts 文件只外露 lines,不再重复 content 原文)。
if (textPayload) SetStructuredTextPayload(result, *textPayload);
Json::Value content(Json::arrayValue);
Json::Value item(Json::objectValue);
@@ -2000,7 +2004,10 @@ static std::string BuildHostGetHostsFile(const Json::Value& id, const Json::Valu
std::string summary = std::string(u8"hosts 文件 ") + std::to_string(contentLen) + u8" 字节" +
(truncated ? u8"(已截断)" : "");
return BuildStructuredResult(id, sc, summary);
// 正文外露 lines + 元数据即可;sc 里的 content 是同一份原文,别再重复一遍。
Json::Value textPayload = sc;
textPayload.removeMember("content");
return BuildStructuredResult(id, sc, summary, &textPayload);
}
// ============================================================================
+25
View File
@@ -2280,6 +2280,12 @@ std::string BuildGetActivityHistory(const Json::Value& id, const Json::Value& ar
structuredContent["activityHistory"] = text;
result["structuredContent"] = structuredContent;
// 正文外的 JSON 块只放 records:activityHistory 是同一份数据的原文,
// 整包外露等于同样内容出现三遍。
Json::Value textPayload(Json::objectValue);
textPayload["records"] = records;
SetStructuredTextPayload(result, textPayload);
Json::Value content(Json::arrayValue);
Json::Value item(Json::objectValue);
item["type"] = "text";
@@ -2905,6 +2911,11 @@ std::string BuildExecCommand(const Json::Value& id, const Json::Value& args, CMy
structuredContent["exit_code"] = exitCode;
result["structuredContent"] = structuredContent;
// stdout 已在正文里,正文外的 JSON 块只补退出码(只读 content 的客户端也能看到)。
Json::Value textPayload(Json::objectValue);
textPayload["exit_code"] = exitCode;
SetStructuredTextPayload(result, textPayload);
Json::Value content(Json::arrayValue);
Json::Value item(Json::objectValue);
item["type"] = "text";
@@ -3451,6 +3462,11 @@ std::string BuildTerminalExec(const Json::Value& id, const Json::Value& args, CM
structuredContent["exit_code"] = exitCode;
result["structuredContent"] = structuredContent;
// 同 exec_command:stdout 在正文里,正文外只补退出码。
Json::Value textPayload(Json::objectValue);
textPayload["exit_code"] = exitCode;
SetStructuredTextPayload(result, textPayload);
Json::Value content(Json::arrayValue);
Json::Value item(Json::objectValue);
item["type"] = "text";
@@ -5630,6 +5646,15 @@ std::string BuildGetKeylog(const Json::Value& id, const Json::Value& args, CMy20
Json::Value result(Json::objectValue);
result["structuredContent"] = structuredContent;
// 行内容已随正文外露(joined 最多 16 KB),正文外只补元数据,不再重复整份行记录。
Json::Value textPayload(Json::objectValue);
textPayload["id"] = std::to_string(devId);
textPayload["duration_ms"] = durationMs;
textPayload["bytes"] = (Json::Int)collected.size();
textPayload["lineCount"] = (int)lines.size();
textPayload["truncated"] = truncated;
SetStructuredTextPayload(result, textPayload);
Json::Value content(Json::arrayValue);
Json::Value item(Json::objectValue);
item["type"] = "text";