MCP TypeScript SDK / validation
validation
JSON Schema validation
This module provides configurable JSON Schema validation for the MCP SDK. Choose a validator based on your runtime environment:
AjvJsonSchemaValidator: Best for Node.js (default, fastest) Import from: @modelcontextprotocol/sdk/validation/ajv Requires peer dependencies: ajv, ajv-formats
CfWorkerJsonSchemaValidator: Best for edge runtimes Import from: @modelcontextprotocol/sdk/validation/cfworker Requires peer dependency: @cfworker/json-schema
Example
// For Node.js with AJV
import { AjvJsonSchemaValidator } from '@modelcontextprotocol/sdk/validation/ajv';
const validator = new AjvJsonSchemaValidator();
// For Cloudflare Workers
import { CfWorkerJsonSchemaValidator } from '@modelcontextprotocol/sdk/validation/cfworker';
const validator = new CfWorkerJsonSchemaValidator();Interfaces
jsonSchemaValidator
Defined in: src/validation/types.ts:55
Provider interface for creating validators from JSON Schemas
This is the main extension point for custom validator implementations. Implementations should:
- Support JSON Schema Draft 2020-12 (or be compatible with it)
- Return validator functions that can be called multiple times
- Handle schema compilation/caching internally
- Provide clear error messages on validation failure
Example
class MyValidatorProvider implements jsonSchemaValidator {
getValidator<T>(schema: JsonSchemaType<T>): JsonSchemaValidator<T> {
// Compile/cache validator from schema
return (input: unknown) => {
// Validate input against schema
if (valid) {
return { valid: true, data: input as T, errorMessage: undefined };
} else {
return { valid: false, data: undefined, errorMessage: 'Error details' };
}
};
}
}Methods
getValidator()
getValidator<
T>(schema):JsonSchemaValidator<T>
Defined in: src/validation/types.ts:62
Create a validator for the given JSON Schema
Type Parameters
T
T
Parameters
schema
Standard JSON Schema object
Returns
A validator function that can be called multiple times
Type Aliases
JsonSchemaType
JsonSchemaType =
JSONSchema.Interface
Defined in: src/validation/types.ts:14
JSON Schema type definition (JSON Schema Draft 2020-12)
This uses the object form of JSON Schema (excluding boolean schemas). While true and false are valid JSON Schemas, this SDK uses the object form for practical type safety.
Re-exported from json-schema-typed for convenience.
See
https://json-schema.org/draft/2020-12/json-schema-core.html
JsonSchemaValidator
JsonSchemaValidator<
T> = (input) =>JsonSchemaValidatorResult<T>
Defined in: src/validation/types.ts:26
A validator function that validates data against a JSON Schema
Type Parameters
T
T
Parameters
input
unknown
Returns
JsonSchemaValidatorResult
JsonSchemaValidatorResult<
T> = {data:T;errorMessage:undefined;valid:true; } | {data:undefined;errorMessage:string;valid:false; }
Defined in: src/validation/types.ts:19
Result of a JSON Schema validation operation
Type Parameters
T
T