Skip to content

Commit 8b02940

Browse files
committed
align ToolResultContentSchema.content on CallToolResultSchema.content
1 parent b41580f commit 8b02940

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

src/examples/backfill/backfillSampling.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
This example implements an stdio MCP proxy that backfills context-agnostic sampling requests using the Claude API.
33
44
Usage:
5+
npx -y @modelcontextprotocol/inspector \
6+
npx -- -y --silent tsx src/examples/backfill/backfillSampling.ts \
7+
npx -y --silent tsx src/examples/server/adventureGame.ts
8+
59
npx -y @modelcontextprotocol/inspector \
610
npx -y --silent tsx src/examples/backfill/backfillSampling.ts -- \
711
npx -y --silent @modelcontextprotocol/server-everything
@@ -19,6 +23,7 @@ import {
1923
ToolChoiceAny,
2024
ToolChoiceTool,
2125
ToolChoiceNone,
26+
MessageParam,
2227
} from "@anthropic-ai/sdk/resources/messages.js";
2328
import { StdioServerTransport } from '../../server/stdio.js';
2429
import { StdioClientTransport } from '../../client/stdio.js';
@@ -55,7 +60,7 @@ ResourceUpdatedNotification,
5560
ToolListChangedNotificationSchema,
5661
} from "../../types.js";
5762
import { Transport } from "../../shared/transport.js";
58-
import { Server } from "src/server/index.js";
63+
import { Server } from "../../server/index.js";
5964

6065
const DEFAULT_MAX_TOKENS = process.env.DEFAULT_MAX_TOKENS ? parseInt(process.env.DEFAULT_MAX_TOKENS) : 1000;
6166

@@ -226,7 +231,10 @@ function contentBlockFromMcp(content: AssistantMessageContent | UserMessageConte
226231
return {
227232
type: 'tool_result',
228233
tool_use_id: content.toolUseId,
229-
content: content.content.map(c => {
234+
content: content.structuredContent ? [{
235+
type: 'text',
236+
text: JSON.stringify(content.structuredContent),
237+
}] : content.content.map(c => {
230238
if (c.type === 'text') {
231239
return {type: 'text', text: c.text};
232240
} else if (c.type === 'image') {
@@ -257,10 +265,11 @@ function contentBlockFromMcp(content: AssistantMessageContent | UserMessageConte
257265
}
258266
}
259267

260-
function contentFromMcp(content: CreateMessageRequest['params']['messages'][number]['content']): ContentBlockParam[] {
261-
// Handle both single content block and arrays
262-
const contentArray = Array.isArray(content) ? content : [content];
263-
return contentArray.map(contentBlockFromMcp);
268+
function messagesFromMcp(messages: CreateMessageRequest['params']['messages']): MessageParam[] {
269+
return messages.map(({role, content}) => (<MessageParam>{
270+
role,
271+
content: (Array.isArray(content) ? content : [content]).map(contentBlockFromMcp)
272+
}))
264273
}
265274

266275
export type NamedTransport<T extends Transport = Transport> = {
@@ -348,10 +357,7 @@ export async function setupBackfill(client: NamedTransport, server: NamedTranspo
348357
text: message.params.systemPrompt
349358
},
350359
],
351-
messages: message.params.messages.map(({role, content}) => ({
352-
role,
353-
content: contentFromMcp(content)
354-
})),
360+
messages: messagesFromMcp(message.params.messages),
355361
max_tokens: message.params.maxTokens ?? DEFAULT_MAX_TOKENS,
356362
temperature: message.params.temperature,
357363
stop_sequences: message.params.stopSequences,

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1189,7 +1189,7 @@ export const ToolChoiceSchema = z
11891189
export const ToolResultContentSchema = z.object({
11901190
type: z.literal("tool_result"),
11911191
toolUseId: z.string().describe("The unique identifier for the corresponding tool call."),
1192-
content: z.array(z.union([TextContentSchema, ImageContentSchema, AudioContentSchema])),
1192+
content: z.array(ContentBlockSchema).default([]),
11931193
structuredContent: z.object({}).passthrough().optional(),
11941194
isError: z.optional(z.boolean()),
11951195
})

0 commit comments

Comments
 (0)