Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
23 changes: 20 additions & 3 deletions apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1000,22 +1000,39 @@ 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>): 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"
? [{ type: "text", text: content }]
: Array.isArray(content)
? content
: [],
...(record?.structuredContent === undefined
? {}
: { structuredContent: record.structuredContent }),
...(structuredContent === undefined ? {} : { structuredContent }),
...(record?.isError === true ? { isError: true } : {}),
...(record?._meta === undefined ? {} : { _meta: record._meta }),
};
Expand Down
16 changes: 13 additions & 3 deletions packages/client-runtime/src/mcpApps/host.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }),
});
Expand All @@ -116,15 +116,25 @@ 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,
},
});
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: [] });
Expand Down
29 changes: 26 additions & 3 deletions packages/client-runtime/src/mcpApps/host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
: {}),
Expand Down
Loading