Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
"client": "tsx src/cli.ts client"
},
"dependencies": {
"ajv": "^6.12.6",
"@cfworker/json-schema": "^4.1.1",
"content-type": "^1.0.5",
"cors": "^2.8.5",
"cross-spawn": "^7.0.5",
Expand Down Expand Up @@ -91,8 +91,8 @@
"@types/ws": "^8.5.12",
"eslint": "^9.8.0",
"eslint-config-prettier": "^10.1.8",
"prettier": "3.6.2",
"jest": "^29.7.0",
"prettier": "3.6.2",
"supertest": "^7.0.0",
"ts-jest": "^29.2.4",
"tsx": "^4.16.5",
Expand Down
21 changes: 10 additions & 11 deletions src/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ import {
ErrorCode,
McpError
} from '../types.js';
import Ajv from 'ajv';
import type { ValidateFunction } from 'ajv';
import { Validator, Schema } from '@cfworker/json-schema';

export type ClientOptions = ProtocolOptions & {
/**
Expand Down Expand Up @@ -82,8 +81,7 @@ export class Client<
private _serverVersion?: Implementation;
private _capabilities: ClientCapabilities;
private _instructions?: string;
private _cachedToolOutputValidators: Map<string, ValidateFunction> = new Map();
private _ajv: InstanceType<typeof Ajv>;
private _cachedToolOutputValidators: Map<string, Validator> = new Map();

/**
* Initializes this client with the given name and version information.
Expand All @@ -94,7 +92,6 @@ export class Client<
) {
super(options);
this._capabilities = options?.capabilities ?? {};
this._ajv = new Ajv();
}

/**
Expand Down Expand Up @@ -348,12 +345,14 @@ export class Client<
if (result.structuredContent) {
try {
// Validate the structured content (which is already an object) against the schema
const isValid = validator(result.structuredContent);
const validationResult = validator.validate(result.structuredContent);

if (!validationResult.valid) {
const errorMessages = validationResult.errors.map(error => `${error.instanceLocation}: ${error.error}`).join('; ');

if (!isValid) {
throw new McpError(
ErrorCode.InvalidParams,
`Structured content does not match the tool's output schema: ${this._ajv.errorsText(validator.errors)}`
`Structured content does not match the tool's output schema: ${errorMessages}`
);
}
} catch (error) {
Expand All @@ -375,10 +374,10 @@ export class Client<
this._cachedToolOutputValidators.clear();

for (const tool of tools) {
// If the tool has an outputSchema, create and cache the Ajv validator
// If the tool has an outputSchema, create and cache the validator
if (tool.outputSchema) {
try {
const validator = this._ajv.compile(tool.outputSchema);
const validator = new Validator(tool.outputSchema as Schema, '2020-12');
this._cachedToolOutputValidators.set(tool.name, validator);
} catch {
// Ignore schema compilation errors
Expand All @@ -387,7 +386,7 @@ export class Client<
}
}

private getToolOutputValidator(toolName: string): ValidateFunction | undefined {
private getToolOutputValidator(toolName: string): Validator | undefined {
return this._cachedToolOutputValidators.get(toolName);
}

Expand Down
Loading