|
6 | 6 | import { Client } from '@modelcontextprotocol/client'; |
7 | 7 | import type { TextContent } from '@modelcontextprotocol/core'; |
8 | 8 | import { AjvJsonSchemaValidator, fromJsonSchema, InMemoryTransport } from '@modelcontextprotocol/core'; |
9 | | -import { completable, McpServer } from '@modelcontextprotocol/server'; |
| 9 | +import { completable, fromJsonSchema as serverFromJsonSchema, McpServer } from '@modelcontextprotocol/server'; |
10 | 10 | import { toStandardJsonSchema } from '@valibot/to-json-schema'; |
11 | 11 | import { type } from 'arktype'; |
12 | 12 | import * as v from 'valibot'; |
@@ -428,6 +428,44 @@ describe('Standard Schema Support', () => { |
428 | 428 | }); |
429 | 429 | }); |
430 | 430 |
|
| 431 | + describe('fromJsonSchema with default validator (server wrapper)', () => { |
| 432 | + test('should use runtime-appropriate default validator when none is provided', async () => { |
| 433 | + const inputSchema = serverFromJsonSchema<{ name: string }>({ |
| 434 | + type: 'object', |
| 435 | + properties: { name: { type: 'string' } }, |
| 436 | + required: ['name'] |
| 437 | + }); |
| 438 | + |
| 439 | + mcpServer.registerTool('greet-default', { inputSchema }, async ({ name }) => ({ |
| 440 | + content: [{ type: 'text', text: `Hello, ${name}!` }] |
| 441 | + })); |
| 442 | + |
| 443 | + await connectClientAndServer(); |
| 444 | + |
| 445 | + const result = await client.request({ method: 'tools/call', params: { name: 'greet-default', arguments: { name: 'World' } } }); |
| 446 | + expect((result.content[0] as TextContent).text).toBe('Hello, World!'); |
| 447 | + }); |
| 448 | + |
| 449 | + test('should reject invalid input with default validator', async () => { |
| 450 | + const inputSchema = serverFromJsonSchema({ type: 'object', properties: { count: { type: 'number' } }, required: ['count'] }); |
| 451 | + |
| 452 | + mcpServer.registerTool('double-default', { inputSchema }, async args => { |
| 453 | + const { count } = args as { count: number }; |
| 454 | + return { content: [{ type: 'text', text: `${count * 2}` }] }; |
| 455 | + }); |
| 456 | + |
| 457 | + await connectClientAndServer(); |
| 458 | + |
| 459 | + const result = await client.request({ |
| 460 | + method: 'tools/call', |
| 461 | + params: { name: 'double-default', arguments: { count: 'not a number' } } |
| 462 | + }); |
| 463 | + expect(result.isError).toBe(true); |
| 464 | + const errorText = (result.content[0] as TextContent).text; |
| 465 | + expect(errorText).toContain('Input validation error'); |
| 466 | + }); |
| 467 | + }); |
| 468 | + |
431 | 469 | describe('Prompt completions with Zod completable', () => { |
432 | 470 | // Note: completable() is currently Zod-specific |
433 | 471 | // These tests verify that Zod schemas with completable still work |
|
0 commit comments