Skip to content
Closed
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
74 changes: 74 additions & 0 deletions src/client/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1261,5 +1261,79 @@ describe('outputSchema validation', () => {
);
});

/***
* Test: client should automatically provide compatible content field
*/
test('should automatically provide compatible content field', async () => {
const server = new Server({
name: 'test-server',
version: '1.0.0',
}, {
capabilities: {
tools: {},
},
});

// Set up server handlers
server.setRequestHandler(InitializeRequestSchema, async (request) => ({
protocolVersion: request.params.protocolVersion,
capabilities: {},
serverInfo: {
name: 'test-server',
version: '1.0.0',
}
}));

server.setRequestHandler(ListToolsRequestSchema, async () => ({
tools: [
{
name: 'test-tool',
description: 'A test tool',
inputSchema: {
type: 'object',
properties: {},
},
outputSchema: {
type: 'object',
properties: {
result: { type: 'string' },
count: { type: 'number' },
},
required: ['result', 'count'],
additionalProperties: false,
},
},
],
}));

server.setRequestHandler(CallToolRequestSchema, async (request) => {
if (request.params.name === 'test-tool') {
return {
structuredContent: { result: 'success', count: 42 }
};
}
throw new Error('Unknown tool');
});

const client = new Client({
name: 'test-client',
version: '1.0.0',
});

const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair();

await Promise.all([
client.connect(clientTransport),
server.connect(serverTransport),
]);

// List tools to cache the schemas
await client.listTools();

// Call the tool - client should automatically provide compatible content field
const result = await client.callTool({ name: 'test-tool' });
expect(result.structuredContent).toEqual({ result: 'success', count: 42 });
expect(result.content).toEqual([{ type: 'text', text: JSON.stringify(result.structuredContent, null, 2) }]);
});

});
14 changes: 12 additions & 2 deletions src/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import {
Tool,
ErrorCode,
McpError,
CallToolResultWithContent,
} from "../types.js";
import Ajv from "ajv";
import type { ValidateFunction } from "ajv";
Expand Down Expand Up @@ -420,7 +421,7 @@ export class Client<
| typeof CallToolResultSchema
| typeof CompatibilityCallToolResultSchema = CallToolResultSchema,
options?: RequestOptions,
) {
): Promise<CallToolResultWithContent> {
const result = await this.request(
{ method: "tools/call", params },
resultSchema,
Expand Down Expand Up @@ -459,10 +460,19 @@ export class Client<
`Failed to validate structured content: ${error instanceof Error ? error.message : String(error)}`
);
}
// for compatibility, serialize structuredContent to content if none was provided
if (!result.content) {
result.content = [
{
type: "text",
text: JSON.stringify(result.structuredContent, null, 2),
},
];
}
}
}

return result;
return result as CallToolResultWithContent;
}

private cacheToolOutputSchemas(tools: Tool[]) {
Expand Down
6 changes: 6 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -946,6 +946,11 @@ export const CallToolResultSchema = z.union([
CallToolStructuredResultSchema,
]);

export const CallToolResultWithContentSchema = z.intersection(
CallToolResultSchema,
z.object({ content: ContentListSchema })
)

/**
* CallToolResultSchema extended with backwards compatibility to protocol version 2024-10-07.
*/
Expand Down Expand Up @@ -1400,6 +1405,7 @@ export type ContentList = Infer<typeof ContentListSchema>;
export type CallToolUnstructuredResult = Infer<typeof CallToolUnstructuredResultSchema>;
export type CallToolStructuredResult = Infer<typeof CallToolStructuredResultSchema>;
export type CallToolResult = Infer<typeof CallToolResultSchema>;
export type CallToolResultWithContent = Infer<typeof CallToolResultWithContentSchema>;
export type CompatibilityCallToolResult = Infer<typeof CompatibilityCallToolResultSchema>;
export type CallToolRequest = Infer<typeof CallToolRequestSchema>;
export type ToolListChangedNotification = Infer<typeof ToolListChangedNotificationSchema>;
Expand Down