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
5 changes: 5 additions & 0 deletions .changeset/fine-kiwis-rush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@modelcontextprotocol/client': patch
---

fix: validate sampling/createMessage with correct schema
6 changes: 4 additions & 2 deletions packages/client/src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import {
CompleteResultSchema,
CreateMessageRequestSchema,
CreateMessageResultSchema,
CreateMessageResultWithToolsSchema,
CreateTaskResultSchema,
ElicitRequestSchema,
ElicitResultSchema,
Expand Down Expand Up @@ -458,8 +459,9 @@ export class Client<
return taskValidationResult.data;
}

// For non-task requests, validate against CreateMessageResultSchema
const validationResult = safeParse(CreateMessageResultSchema, result);
// For non-task requests, validate against appropriate schema
const schema = params.tools ? CreateMessageResultWithToolsSchema : CreateMessageResultSchema;
const validationResult = safeParse(schema, result);
if (!validationResult.success) {
const errorMessage =
validationResult.error instanceof Error ? validationResult.error.message : String(validationResult.error);
Expand Down
104 changes: 104 additions & 0 deletions test/integration/test/sampling_tools.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import { Client } from '@modelcontextprotocol/client';
import { CreateMessageRequestSchema, InMemoryTransport } from '@modelcontextprotocol/core';
import { Server } from '@modelcontextprotocol/server';
import { describe, expect, test } from 'vitest';

describe('sampling/createMessage with tools', () => {
test('should support returning tool calls when tools are provided', async () => {
const server = new Server({ name: 'test server', version: '1.0' }, { capabilities: {} });

const client = new Client(
{ name: 'test client', version: '1.0' },
{
capabilities: {
sampling: {
tools: {}
}
}
}
);

// Implement request handler for sampling/createMessage that returns a tool call
client.setRequestHandler(CreateMessageRequestSchema, async _request => {
return {
model: 'test-model',
role: 'assistant',
stopReason: 'toolUse',
content: [
{
type: 'tool_use',
id: 'call_1',
name: 'test_tool',
input: { arg: 'value' }
}
]
};
});

const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair();
await Promise.all([client.connect(clientTransport), server.connect(serverTransport)]);

const result = await server.createMessage({
messages: [{ role: 'user', content: { type: 'text', text: 'use the tool' } }],
maxTokens: 100,
tools: [{ name: 'test_tool', inputSchema: { type: 'object' } }]
});

expect(result).toEqual({
model: 'test-model',
role: 'assistant',
stopReason: 'toolUse',
content: [
{
type: 'tool_use',
id: 'call_1',
name: 'test_tool',
input: { arg: 'value' }
}
]
});
});

test('should fail if returning tool calls when tools are NOT provided', async () => {
const server = new Server({ name: 'test server', version: '1.0' }, { capabilities: {} });

const client = new Client(
{ name: 'test client', version: '1.0' },
{
capabilities: {
sampling: {}
}
}
);

// Implement request handler for sampling/createMessage that returns a tool call
client.setRequestHandler(CreateMessageRequestSchema, async _request => {
return {
model: 'test-model',
role: 'assistant',
stopReason: 'toolUse',
content: [
{
type: 'tool_use',
id: 'call_1',
name: 'test_tool',
input: { arg: 'value' }
}
]
};
});

const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair();
await Promise.all([client.connect(clientTransport), server.connect(serverTransport)]);

// This should fail because the client validation will reject the tool call result
// when validating against CreateMessageResultSchema (since tools were not requested)
await expect(
server.createMessage({
messages: [{ role: 'user', content: { type: 'text', text: 'use the tool' } }],
maxTokens: 100
// No tools provided
})
).rejects.toThrow();
});
});
Loading