From 0039669d0ab4c911085c99d83496864805839b29 Mon Sep 17 00:00:00 2001 From: Josh Knapp Date: Sat, 14 Nov 2026 10:54:05 -0800 Subject: [PATCH] fix: include tool result payload in text content, not just a count MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ok() put only a summary string ("8 result(s)") in `content` and the actual payload only in `structuredContent`. Tools declare no outputSchema, and claude.ai renders only the text blocks, so memory.search / memory.list returned counts with no ids — the caller could not read or memory.get anything it found. The same helper backs every tool, so memory.get, snippet.get (body dropped) and snippet.list were equally affected; project.identify and memory.write only looked fine because their summaries embed the id. The first text block is now `\n`, per the MCP spec's guidance that structured results also be returned as serialized JSON text. structuredContent is unchanged for clients that consume it. One block rather than two because some clients render only the first. Adds integration tests asserting that the first text block of memory.list, memory.search, memory.get, snippet.list and snippet.get carries ids/content, not just the count. Co-Authored-By: Claude Opus 5.5 --- apps/web/lib/mcp/tools.integration.test.ts | 63 ++++++++++++++++++++++ apps/web/lib/mcp/tools.ts | 15 +++++- 2 files changed, 77 insertions(+), 1 deletion(-) diff --git a/apps/web/lib/mcp/tools.integration.test.ts b/apps/web/lib/mcp/tools.integration.test.ts index cc58c8c..13beab7 100644 --- a/apps/web/lib/mcp/tools.integration.test.ts +++ b/apps/web/lib/mcp/tools.integration.test.ts @@ -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)), + ); + }); +}); diff --git a/apps/web/lib/mcp/tools.ts b/apps/web/lib/mcp/tools.ts index ec6888a..30c33a7 100644 --- a/apps/web/lib/mcp/tools.ts +++ b/apps/web/lib/mcp/tools.ts @@ -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 `\n` — 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, }; } -- 2.52.0