Error Types
LeanMCP handles several types of errors:- Validation errors - Input doesn’t match schema constraints
- Authentication errors - Missing or invalid auth tokens
- Runtime errors - Errors thrown during tool execution
- Tool errors - Business logic errors with custom messages
Validation Errors
Validation errors occur when input doesn’t match the schema defined with@SchemaConstraint:
Automatic Validation
LeanMCP automatically validates all input before calling your tool:Common Validation Errors
Missing required field:Runtime Errors
Throwing Errors
Throw standard JavaScript errors for business logic failures:Error Response Format
Errors are automatically formatted as MCP responses:Custom Error Classes
Create custom error classes for better error handling:Authentication Errors
Handle authentication errors from@Authenticated decorator:
Error Handling Patterns
Try-Catch in Tools
Handle external API failures gracefully:Validation in Tools
Add custom business logic validation:Retry Logic
Implement retry logic for transient failures:Fallback Values
Provide fallback values for non-critical failures:User-Friendly Error Messages
Provide Context
Suggest Solutions
Include Actionable Information
Error Logging
Structured Logging
Error Tracking
Integrate with error tracking services:Testing Error Scenarios
Unit Tests for Error Handling
Best Practices
1. Fail Fast
Validate input early and throw errors immediately:2. Never Swallow Errors
3. Use Specific Error Messages
4. Clean Up Resources
Always clean up resources in finally blocks:5. Document Error Conditions
Troubleshooting
Error Not Caught
Problem: Errors are not being caught properly. Solutions:- Ensure you’re using async/await correctly
- Check for unhandled promise rejections
- Verify try-catch blocks are around async calls
- Use
.catch()for promise chains
Validation Not Working
Problem: Schema validation isn’t catching invalid input. Solutions:- Verify
@SchemaConstraintdecorators are applied - Check
inputClassis specified in@Tooldecorator - Ensure TypeScript decorators are enabled
- Test with obviously invalid data first
Stack Traces in Production
Problem: Exposing internal stack traces to users. Solutions:- Catch errors and throw new user-friendly errors
- Use error tracking service for internal logging
- Strip stack traces in production environment
- Log detailed errors server-side only
Next Steps
Deployment
Deploy error-resilient services to production
Schema Design
Design schemas to prevent validation errors
Creating Services
Build robust services with error handling
Examples
See error handling in real examples