fix: include tool result payload in text content, not just a count #27

Merged
jknapp merged 1 commits from fix/tool-results-text-payload into main 2026-11-14 18:56:07 +00:00
2 changed files with 77 additions and 1 deletions

No files matched your search

@@ -256,3 +256,66 @@ describe("memory.patch (P2)", () => {
expect(hit[0]!.n).toBe(1);
});
});
/**
* Regression: claude.ai (and any client that ignores `structuredContent`
* when no `outputSchema` is declared) only renders the `content` text
* blocks. When `ok()` put just the summary there, memory.search returned
* "8 result(s)" with no ids, so the caller could not read or memory.get
* anything it found. Every read tool must carry its payload in the FIRST
* text block — some clients render only that one.
*/
describe("tool results carry their payload in the text content (claude.ai)", () => {
const firstText = (res: ToolResultLike) => res.content[0]!.text;
type ToolResultLike = { content: Array<{ text: string }>; isError?: boolean };
test("memory.list text includes each memory's id and content", async () => {
const res = await toolMap["memory.list"]!.handler({ project: "test-project" }, ctx);
expect(res.isError).toBeUndefined();
expect(firstText(res)).toContain(memoryId);
expect(firstText(res)).toContain("## RECENTLY SHIPPED");
});
test("memory.search text includes the hit's id, content and rank", async () => {
const res = await toolMap["memory.search"]!.handler(
{ query: "roadmap patch primitive", project: "test-project" },
ctx,
);
expect(res.isError).toBeUndefined();
expect(firstText(res)).toContain(memoryId);
expect(firstText(res)).toContain("## RECENTLY SHIPPED");
expect(firstText(res)).toContain("rrfScore");
});
test("memory.get text includes the full content", async () => {
const res = await toolMap["memory.get"]!.handler({ id: memoryId }, ctx);
expect(firstText(res)).toContain("- patch primitive");
});
test("snippet.list and snippet.get text include the snippet", async () => {
const name = `text-payload-${Date.now()}`;
const put = await toolMap["snippet.put"]!.handler(
{ name, body: "SNIPPET-BODY-MARKER", description: "when to use it" },
ctx,
);
expect(put.isError).toBeUndefined();
const list = await toolMap["snippet.list"]!.handler({}, ctx);
expect(firstText(list)).toContain(name);
expect(firstText(list)).toContain("when to use it");
const get = await toolMap["snippet.get"]!.handler({ name }, ctx);
expect(firstText(get)).toContain("SNIPPET-BODY-MARKER");
await toolMap["snippet.delete"]!.handler({ name }, ctx);
});
test("the text block is the summary line followed by the structured JSON", async () => {
const res = await toolMap["memory.get"]!.handler({ id: memoryId }, ctx);
const [summary, ...rest] = firstText(res).split("\n");
expect(summary).toBe(`memory ${memoryId}`);
expect(JSON.parse(rest.join("\n"))).toEqual(
JSON.parse(JSON.stringify(res.structuredContent)),
);
});
});
+14 -1
View File
@@ -62,9 +62,22 @@ export interface ToolDef {
// ---------- helpers ----------
/**
* Success result. The payload goes in BOTH places:
*
* - `structuredContent` — for clients that consume it (Claude Code).
* - the first `content` text block, as `<summary>\n<JSON>` — for clients
* that only render text (claude.ai ignores `structuredContent` when the
* tool declares no `outputSchema`). The MCP spec asks servers that
* return structured content to also return the serialized JSON as text
* for exactly this reason. Before this, those clients saw only the
* summary — e.g. "8 result(s)" from memory.search, with no ids.
*
* One block, not two: some clients render only the first text block.
*/
function ok(structured: unknown, summary: string): ToolResult {
return {
content: [{ type: "text", text: summary }],
content: [{ type: "text", text: `${summary}\n${JSON.stringify(structured)}` }],
structuredContent: structured,
};
}