Schema Basics
LeanMCP uses TypeScript classes with decorators to define JSON schemas. This provides:- Type safety - Full TypeScript support with IntelliSense
- Automatic validation - Input validated against schema with AJV
- No duplication - Single source of truth for types and schemas
- Clear documentation - Schema serves as API documentation
Defining Input Schemas
Input schemas are TypeScript classes decorated with@SchemaConstraint:
Required vs Optional Fields
Required fields (no@Optional() decorator):
@Optional() decorator):
String Constraints
Length Validation
Pattern Matching
Format Validation
email, uri, date, date-time, uuid, ipv4, ipv6, hostname
Enum Values
Number Constraints
Range Validation
Exclusive Ranges
Multiple Of
Array Constraints
Array Length
Unique Items
Nested Objects
Define complex nested structures:Output Schemas
Define expected output structure for documentation and type safety:Default Values
Provide sensible defaults for optional fields:Validation Patterns
Email Validation
URL Validation
Date Validation
UUID Validation
Custom Pattern Validation
Complex Real-World Examples
User Registration
Payment Processing
Database Query
Type Safety Tips
Use Non-Null Assertion
For required fields, use! to indicate they’ll always have a value:
Leverage Union Types
Extract Common Schemas
Reuse schema definitions across services:schemas/common.ts
Troubleshooting
Validation Errors
Problem: Input validation fails unexpectedly. Solutions:- Check constraint values match your data types
- Verify enum values are exact matches
- Ensure patterns are valid regex (escape special chars)
- Test with valid sample data first
TypeScript Errors
Problem: Type errors with schema classes. Solutions:- Enable decorators in
tsconfig.json - Use
!for required fields,?for optional - Import decorators from
@leanmcp/core - Ensure
experimentalDecoratorsandemitDecoratorMetadataare enabled
Schema Not Generated
Problem: Schema isn’t being created for input. Solutions:- Specify
inputClassin the decorator - Ensure class is decorated with
@SchemaConstraint - Check for TypeScript compilation errors
- Verify the class is exported if in a separate file
Next Steps
Error Handling
Handle validation and runtime errors
Creating Services
Build services with validated schemas
API Reference
Complete schema decorator reference
Examples
See real-world schema examples