|
5 | 5 |
|
6 | 6 | import { Client } from '@modelcontextprotocol/client'; |
7 | 7 | import type { TextContent } from '@modelcontextprotocol/core'; |
8 | | -import { CallToolResultSchema, CompleteResultSchema, InMemoryTransport, ListToolsResultSchema } from '@modelcontextprotocol/core'; |
| 8 | +import { |
| 9 | + AjvJsonSchemaValidator, |
| 10 | + CallToolResultSchema, |
| 11 | + CompleteResultSchema, |
| 12 | + fromJsonSchema, |
| 13 | + InMemoryTransport, |
| 14 | + ListToolsResultSchema |
| 15 | +} from '@modelcontextprotocol/core'; |
9 | 16 | import { completable, McpServer } from '@modelcontextprotocol/server'; |
10 | 17 | import { toStandardJsonSchema } from '@valibot/to-json-schema'; |
11 | 18 | import { type } from 'arktype'; |
@@ -411,6 +418,59 @@ describe('Standard Schema Support', () => { |
411 | 418 | }); |
412 | 419 | }); |
413 | 420 |
|
| 421 | + describe('Raw JSON Schema via fromJsonSchema', () => { |
| 422 | + const validator = new AjvJsonSchemaValidator(); |
| 423 | + |
| 424 | + test('should register tool with raw JSON Schema input', async () => { |
| 425 | + const inputSchema = fromJsonSchema<{ name: string }>( |
| 426 | + { type: 'object', properties: { name: { type: 'string' } }, required: ['name'] }, |
| 427 | + validator |
| 428 | + ); |
| 429 | + |
| 430 | + mcpServer.registerTool('greet', { inputSchema }, async ({ name }) => ({ |
| 431 | + content: [{ type: 'text', text: `Hello, ${name}!` }] |
| 432 | + })); |
| 433 | + |
| 434 | + await connectClientAndServer(); |
| 435 | + |
| 436 | + const listed = await client.request({ method: 'tools/list' }, ListToolsResultSchema); |
| 437 | + expect(listed.tools[0].inputSchema).toMatchObject({ |
| 438 | + type: 'object', |
| 439 | + properties: { name: { type: 'string' } }, |
| 440 | + required: ['name'] |
| 441 | + }); |
| 442 | + |
| 443 | + const result = await client.request( |
| 444 | + { method: 'tools/call', params: { name: 'greet', arguments: { name: 'World' } } }, |
| 445 | + CallToolResultSchema |
| 446 | + ); |
| 447 | + expect((result.content[0] as TextContent).text).toBe('Hello, World!'); |
| 448 | + }); |
| 449 | + |
| 450 | + test('should reject invalid input via AJV validation', async () => { |
| 451 | + const inputSchema = fromJsonSchema( |
| 452 | + { type: 'object', properties: { count: { type: 'number' } }, required: ['count'] }, |
| 453 | + validator |
| 454 | + ); |
| 455 | + |
| 456 | + mcpServer.registerTool('double', { inputSchema }, async args => { |
| 457 | + const { count } = args as { count: number }; |
| 458 | + return { content: [{ type: 'text', text: `${count * 2}` }] }; |
| 459 | + }); |
| 460 | + |
| 461 | + await connectClientAndServer(); |
| 462 | + |
| 463 | + const result = await client.request( |
| 464 | + { method: 'tools/call', params: { name: 'double', arguments: { count: 'not a number' } } }, |
| 465 | + CallToolResultSchema |
| 466 | + ); |
| 467 | + |
| 468 | + expect(result.isError).toBe(true); |
| 469 | + const errorText = (result.content[0] as TextContent).text; |
| 470 | + expect(errorText).toContain('Input validation error'); |
| 471 | + }); |
| 472 | + }); |
| 473 | + |
414 | 474 | describe('Prompt completions with Zod completable', () => { |
415 | 475 | // Note: completable() is currently Zod-specific |
416 | 476 | // These tests verify that Zod schemas with completable still work |
|
0 commit comments