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

+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";