Skip to content

Commit 998f379

Browse files
committed
fix: remove ajv for @cfworker/json-schema
1 parent e0de082 commit 998f379

File tree

4 files changed

+33
-22
lines changed

4 files changed

+33
-22
lines changed

package-lock.json

Lines changed: 15 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
"client": "tsx src/cli.ts client"
6363
},
6464
"dependencies": {
65-
"ajv": "^6.12.6",
65+
"@cfworker/json-schema": "^4.1.1",
6666
"content-type": "^1.0.5",
6767
"cors": "^2.8.5",
6868
"cross-spawn": "^7.0.5",
@@ -90,8 +90,8 @@
9090
"@types/ws": "^8.5.12",
9191
"eslint": "^9.8.0",
9292
"eslint-config-prettier": "^10.1.8",
93-
"prettier": "3.6.2",
9493
"jest": "^29.7.0",
94+
"prettier": "3.6.2",
9595
"supertest": "^7.0.0",
9696
"ts-jest": "^29.2.4",
9797
"tsx": "^4.16.5",

src/client/index.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ import {
3838
ErrorCode,
3939
McpError
4040
} from '../types.js';
41-
import Ajv from 'ajv';
42-
import type { ValidateFunction } from 'ajv';
41+
import { Validator } from '@cfworker/json-schema';
4342

4443
export type ClientOptions = ProtocolOptions & {
4544
/**
@@ -82,8 +81,7 @@ export class Client<
8281
private _serverVersion?: Implementation;
8382
private _capabilities: ClientCapabilities;
8483
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();
8785

8886
/**
8987
* Initializes this client with the given name and version information.
@@ -94,7 +92,6 @@ export class Client<
9492
) {
9593
super(options);
9694
this._capabilities = options?.capabilities ?? {};
97-
this._ajv = new Ajv();
9895
}
9996

10097
/**
@@ -348,12 +345,14 @@ export class Client<
348345
if (result.structuredContent) {
349346
try {
350347
// 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('; ');
352352

353-
if (!isValid) {
354353
throw new McpError(
355354
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}`
357356
);
358357
}
359358
} catch (error) {
@@ -375,10 +374,10 @@ export class Client<
375374
this._cachedToolOutputValidators.clear();
376375

377376
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
379378
if (tool.outputSchema) {
380379
try {
381-
const validator = this._ajv.compile(tool.outputSchema);
380+
const validator = new Validator(tool.outputSchema as any, '2020-12');
382381
this._cachedToolOutputValidators.set(tool.name, validator);
383382
} catch {
384383
// Ignore schema compilation errors
@@ -387,7 +386,7 @@ export class Client<
387386
}
388387
}
389388

390-
private getToolOutputValidator(toolName: string): ValidateFunction | undefined {
389+
private getToolOutputValidator(toolName: string): Validator | undefined {
391390
return this._cachedToolOutputValidators.get(toolName);
392391
}
393392

src/server/index.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import {
3131
SetLevelRequestSchema,
3232
LoggingLevelSchema
3333
} from '../types.js';
34-
import Ajv from 'ajv';
34+
import { Validator } from '@cfworker/json-schema';
3535

3636
export type ServerOptions = ProtocolOptions & {
3737
/**
@@ -291,15 +291,15 @@ export class Server<
291291
// Validate the response content against the requested schema if action is "accept"
292292
if (result.action === 'accept' && result.content) {
293293
try {
294-
const ajv = new Ajv();
294+
const validator = new Validator(params.requestedSchema, '2020-12');
295+
const validationResult = validator.validate(result.content);
295296

296-
const validate = ajv.compile(params.requestedSchema);
297-
const isValid = validate(result.content);
297+
if (!validationResult.valid) {
298+
const errorMessages = validationResult.errors.map(error => `${error.instanceLocation}: ${error.error}`).join('; ');
298299

299-
if (!isValid) {
300300
throw new McpError(
301301
ErrorCode.InvalidParams,
302-
`Elicitation response content does not match requested schema: ${ajv.errorsText(validate.errors)}`
302+
`Elicitation response content does not match requested schema: ${errorMessages}`
303303
);
304304
}
305305
} catch (error) {

0 commit comments

Comments
 (0)