diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts index 1f5a5e5a10dd9..24e1d260ded9e 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts @@ -6124,6 +6124,37 @@ describe("ClaudeAdapterV2 background wake turns", () => { ), ); + it.effect("gives an app the JSON text of a Claude result with images as structuredContent", () => + Effect.gen(function* () { + const text = '{"query":"weather","screens":[]}'; + const { item } = yield* runClaudeMcpAppToolTurn( + MCP_APP_CAPABILITIES, + undefined, + undefined, + // Shape recorded from Claude Code 2.1.322: images make the result a bare block list. + [ + { type: "image", source: { type: "base64", media_type: "image/png", data: "iVBORw0K" } }, + { type: "text", text: "[Image: source: /tmp/screen.png]" }, + { type: "text", text }, + ], + ); + assert.isDefined(mcpAppFromToolItem(item)); + assert.deepInclude((item.output as { readonly result?: object }).result, { + structuredContent: { query: "weather", screens: [] }, + }); + }).pipe( + Effect.scoped, + Effect.provide( + Layer.mergeAll( + IdAllocator.layer, + McpProviderSessions.layer, + NodeServices.layer, + NodeCrypto.layer, + ), + ), + ), + ); + it.effect.each([ { name: "its ui:// resource cannot be read", diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts index 363971aa83cb0..894dbdd8aa3cd 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts @@ -1000,12 +1000,31 @@ function claudeMcpToolForApps( }; } +/** Claude reports a result with images as a bare block list without `structuredContent`; the JSON text block carries it. */ +function claudeMcpBlocksStructuredContent(blocks: ReadonlyArray): unknown { + for (const block of blocks.toReversed()) { + if (!Predicate.isObject(block) || block.type !== "text" || typeof block.text !== "string") { + continue; + } + try { + const parsed: unknown = JSON.parse(block.text); + if (Predicate.isObject(parsed) && !Array.isArray(parsed)) return parsed; + } catch { + // Prose, not JSON. + } + } + return undefined; +} + /** A model-turn MCP `tool_use_result` flattens text `content` to a string; app calls return MCP's raw `CallToolResult`. */ function claudeMcpCallToolResult( value: unknown, ): ProviderAdapter.ProviderAdapterV2McpCallToolResult { const record = Predicate.isObject(value) && !Array.isArray(value) ? value : undefined; const content = record === undefined ? value : record.content; + const structuredContent = Array.isArray(value) + ? claudeMcpBlocksStructuredContent(value) + : record?.structuredContent; return { content: typeof content === "string" @@ -1013,9 +1032,7 @@ function claudeMcpCallToolResult( : Array.isArray(content) ? content : [], - ...(record?.structuredContent === undefined - ? {} - : { structuredContent: record.structuredContent }), + ...(structuredContent === undefined ? {} : { structuredContent }), ...(record?.isError === true ? { isError: true } : {}), ...(record?._meta === undefined ? {} : { _meta: record._meta }), }; diff --git a/packages/client-runtime/src/mcpApps/host.test.ts b/packages/client-runtime/src/mcpApps/host.test.ts index 3e19e753d43de..881247c2e3540 100644 --- a/packages/client-runtime/src/mcpApps/host.test.ts +++ b/packages/client-runtime/src/mcpApps/host.test.ts @@ -107,7 +107,7 @@ describe("makeMcpAppHost", () => { ]); }); - it("drops null fields providers report, which the MCP Apps SDK rejects", async () => { + it("drops null fields and non-MCP content blocks, which the MCP Apps SDK rejects", async () => { const { host, sent } = setup({ callTool: async () => ({ content: [], structuredContent: null, _meta: null }), }); @@ -116,7 +116,14 @@ describe("makeMcpAppHost", () => { host.setToolCall({ arguments: {}, result: { - content: [{ type: "text", text: "t" }], + content: [ + // Claude's image shape, with its bytes stripped in storage. + { type: "image", source: { type: "base64", media_type: "image/webp", sizeBytes: 9 } }, + { type: "image", mimeType: "image/webp", sizeBytes: 9 }, + { type: "resource", resource: { uri: "u", mimeType: "image/png", sizeBytes: 9 } }, + { type: "image", data: "AAAA", mimeType: "image/png" }, + { type: "text", text: "t" }, + ], structuredContent: { time: "t" }, _meta: null, }, @@ -124,7 +131,10 @@ describe("makeMcpAppHost", () => { host.receive({ jsonrpc: "2.0", id: 2, method: "tools/call", params: { name: "get-time" } }); await flush(); expect(sent.find((m) => m.method === "ui/notifications/tool-result")?.params).toEqual({ - content: [{ type: "text", text: "t" }], + content: [ + { type: "image", data: "AAAA", mimeType: "image/png" }, + { type: "text", text: "t" }, + ], structuredContent: { time: "t" }, }); expect(sent.find((m) => m.id === 2)?.result).toEqual({ content: [] }); diff --git a/packages/client-runtime/src/mcpApps/host.ts b/packages/client-runtime/src/mcpApps/host.ts index e6bb82f916d1d..19ff72314454b 100644 --- a/packages/client-runtime/src/mcpApps/host.ts +++ b/packages/client-runtime/src/mcpApps/host.ts @@ -131,13 +131,36 @@ function isId(value: unknown): value is JsonRpcId { return typeof value === "string" || (typeof value === "number" && Number.isFinite(value)); } +/** Stored results lose image bytes, and Claude reports images in Anthropic's `source` shape. */ +function isMcpContentBlock(block: unknown): boolean { + if (!Predicate.isObject(block)) return false; + switch (block.type) { + case "text": + return Predicate.isString(block.text); + case "image": + case "audio": + return Predicate.isString(block.data) && Predicate.isString(block.mimeType); + case "resource_link": + return Predicate.isString(block.uri) && Predicate.isString(block.name); + case "resource": + return ( + Predicate.isObject(block.resource) && + Predicate.isString(block.resource.uri) && + (Predicate.isString(block.resource.text) || Predicate.isString(block.resource.blob)) + ); + default: + return false; + } +} + /** - * Providers can report absent fields as null (Codex sends `_meta: null`), which - * the MCP Apps SDK rejects, dropping the whole notification or response. + * Providers can report absent fields as null (Codex sends `_meta: null`) or content + * blocks MCP does not define, which the MCP Apps SDK rejects, dropping the whole + * notification or response. */ function normalizeMcpAppToolResult(result: McpAppCallToolResult): McpAppCallToolResult { return { - content: Array.isArray(result.content) ? result.content : [], + content: Array.isArray(result.content) ? result.content.filter(isMcpContentBlock) : [], ...(Predicate.isObject(result.structuredContent) ? { structuredContent: result.structuredContent } : {}),