@@ -38,8 +38,7 @@ import {
38
38
ErrorCode ,
39
39
McpError
40
40
} from '../types.js' ;
41
- import Ajv from 'ajv' ;
42
- import type { ValidateFunction } from 'ajv' ;
41
+ import { Validator } from '@cfworker/json-schema' ;
43
42
44
43
export type ClientOptions = ProtocolOptions & {
45
44
/**
@@ -82,8 +81,7 @@ export class Client<
82
81
private _serverVersion ?: Implementation ;
83
82
private _capabilities : ClientCapabilities ;
84
83
private _instructions ?: string ;
85
- private _cachedToolOutputValidators : Map < string , ValidateFunction > = new Map ( ) ;
86
- private _ajv : InstanceType < typeof Ajv > ;
84
+ private _cachedToolOutputValidators : Map < string , Validator > = new Map ( ) ;
87
85
88
86
/**
89
87
* Initializes this client with the given name and version information.
@@ -94,7 +92,6 @@ export class Client<
94
92
) {
95
93
super ( options ) ;
96
94
this . _capabilities = options ?. capabilities ?? { } ;
97
- this . _ajv = new Ajv ( ) ;
98
95
}
99
96
100
97
/**
@@ -348,12 +345,14 @@ export class Client<
348
345
if ( result . structuredContent ) {
349
346
try {
350
347
// Validate the structured content (which is already an object) against the schema
351
- const isValid = validator ( result . structuredContent ) ;
348
+ const validationResult = validator . validate ( result . structuredContent ) ;
349
+
350
+ if ( ! validationResult . valid ) {
351
+ const errorMessages = validationResult . errors . map ( error => `${ error . instanceLocation } : ${ error . error } ` ) . join ( '; ' ) ;
352
352
353
- if ( ! isValid ) {
354
353
throw new McpError (
355
354
ErrorCode . InvalidParams ,
356
- `Structured content does not match the tool's output schema: ${ this . _ajv . errorsText ( validator . errors ) } `
355
+ `Structured content does not match the tool's output schema: ${ errorMessages } `
357
356
) ;
358
357
}
359
358
} catch ( error ) {
@@ -375,10 +374,10 @@ export class Client<
375
374
this . _cachedToolOutputValidators . clear ( ) ;
376
375
377
376
for ( const tool of tools ) {
378
- // If the tool has an outputSchema, create and cache the Ajv validator
377
+ // If the tool has an outputSchema, create and cache the validator
379
378
if ( tool . outputSchema ) {
380
379
try {
381
- const validator = this . _ajv . compile ( tool . outputSchema ) ;
380
+ const validator = new Validator ( tool . outputSchema as any , '2020-12' ) ;
382
381
this . _cachedToolOutputValidators . set ( tool . name , validator ) ;
383
382
} catch {
384
383
// Ignore schema compilation errors
@@ -387,7 +386,7 @@ export class Client<
387
386
}
388
387
}
389
388
390
- private getToolOutputValidator ( toolName : string ) : ValidateFunction | undefined {
389
+ private getToolOutputValidator ( toolName : string ) : Validator | undefined {
391
390
return this . _cachedToolOutputValidators . get ( toolName ) ;
392
391
}
393
392
0 commit comments