|
| 1 | +import type { JSONRPCMessage, JsonSchemaType, JsonSchemaValidatorResult, jsonSchemaValidator } from '@modelcontextprotocol/core'; |
| 2 | +import { InMemoryTransport, LATEST_PROTOCOL_VERSION } from '@modelcontextprotocol/core'; |
| 3 | +import { Client } from '../../src/client/client.js'; |
| 4 | +import { fromJsonSchema } from '../../src/fromJsonSchema.js'; |
| 5 | + |
| 6 | +class RecordingValidator implements jsonSchemaValidator { |
| 7 | + schemas: JsonSchemaType[] = []; |
| 8 | + values: unknown[] = []; |
| 9 | + |
| 10 | + getValidator<T>(schema: JsonSchemaType) { |
| 11 | + this.schemas.push(schema); |
| 12 | + return (value: unknown): JsonSchemaValidatorResult<T> => { |
| 13 | + this.values.push(value); |
| 14 | + return { valid: true, data: value as T, errorMessage: undefined }; |
| 15 | + }; |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +async function connectInitializedClient(client: Client) { |
| 20 | + const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair(); |
| 21 | + serverTransport.onmessage = async message => { |
| 22 | + if ('method' in message && 'id' in message && message.method === 'initialize') { |
| 23 | + await serverTransport.send({ |
| 24 | + jsonrpc: '2.0', |
| 25 | + id: message.id, |
| 26 | + result: { |
| 27 | + protocolVersion: LATEST_PROTOCOL_VERSION, |
| 28 | + capabilities: { tools: {} }, |
| 29 | + serverInfo: { name: 'test-server', version: '1.0.0' } |
| 30 | + } |
| 31 | + }); |
| 32 | + } else if ('method' in message && 'id' in message && message.method === 'tools/list') { |
| 33 | + await serverTransport.send({ |
| 34 | + jsonrpc: '2.0', |
| 35 | + id: message.id, |
| 36 | + result: { |
| 37 | + tools: [ |
| 38 | + { |
| 39 | + name: 'structured-tool', |
| 40 | + description: 'A tool with structured output', |
| 41 | + inputSchema: { type: 'object' }, |
| 42 | + outputSchema: { |
| 43 | + type: 'object', |
| 44 | + properties: { count: { type: 'number' } }, |
| 45 | + required: ['count'] |
| 46 | + } |
| 47 | + } |
| 48 | + ] |
| 49 | + } |
| 50 | + } satisfies JSONRPCMessage); |
| 51 | + } |
| 52 | + }; |
| 53 | + |
| 54 | + await Promise.all([client.connect(clientTransport), serverTransport.start()]); |
| 55 | + return { clientTransport, serverTransport }; |
| 56 | +} |
| 57 | + |
| 58 | +describe('client JSON Schema validator overrides', () => { |
| 59 | + test('Client constructor uses a custom validator for tool output schema caching', async () => { |
| 60 | + const validator = new RecordingValidator(); |
| 61 | + const client = new Client( |
| 62 | + { name: 'test-client', version: '1.0.0' }, |
| 63 | + { |
| 64 | + capabilities: {}, |
| 65 | + jsonSchemaValidator: validator |
| 66 | + } |
| 67 | + ); |
| 68 | + const { clientTransport, serverTransport } = await connectInitializedClient(client); |
| 69 | + |
| 70 | + await expect(client.listTools()).resolves.toMatchObject({ |
| 71 | + tools: [ |
| 72 | + { |
| 73 | + name: 'structured-tool', |
| 74 | + outputSchema: { |
| 75 | + type: 'object', |
| 76 | + properties: { count: { type: 'number' } }, |
| 77 | + required: ['count'] |
| 78 | + } |
| 79 | + } |
| 80 | + ] |
| 81 | + }); |
| 82 | + |
| 83 | + expect(validator.schemas).toEqual([ |
| 84 | + { |
| 85 | + type: 'object', |
| 86 | + properties: { count: { type: 'number' } }, |
| 87 | + required: ['count'] |
| 88 | + } |
| 89 | + ]); |
| 90 | + |
| 91 | + await client.close(); |
| 92 | + await clientTransport.close(); |
| 93 | + await serverTransport.close(); |
| 94 | + }); |
| 95 | + |
| 96 | + test('fromJsonSchema uses an explicitly supplied custom validator', async () => { |
| 97 | + const validator = new RecordingValidator(); |
| 98 | + const schema: JsonSchemaType = { |
| 99 | + type: 'object', |
| 100 | + properties: { name: { type: 'string' } }, |
| 101 | + required: ['name'] |
| 102 | + }; |
| 103 | + |
| 104 | + const standardSchema = fromJsonSchema<{ name: string }>(schema, validator); |
| 105 | + expect(standardSchema['~standard'].validate({ name: 123 })).toEqual({ value: { name: 123 } }); |
| 106 | + |
| 107 | + expect(validator.schemas).toEqual([schema]); |
| 108 | + expect(validator.values).toEqual([{ name: 123 }]); |
| 109 | + }); |
| 110 | +}); |
0 commit comments