Skip to main content
Glama

OpenAPI MCP Server

response-validation.md17.7 kB
# Response Validation Implementation Plan ## Overview This document outlines a comprehensive plan to implement dynamic Zod schema generation from OpenAPI response specifications and integrate response validation into the MCP OpenAPI server. This approach will provide runtime validation of API responses to ensure reliability and catch API changes immediately. ## Objectives ### Primary Goals - **Runtime Validation**: Catch API response format changes immediately - **Type Safety**: Ensure responses match expected structure - **Error Reporting**: Clear validation errors when APIs return unexpected data - **Reliability**: Prevent downstream errors from malformed API responses - **Development Experience**: Better debugging when APIs change ### Success Criteria - All OpenAPI response schemas can be converted to Zod schemas - API responses are validated against generated schemas - Configurable validation modes (strict/warn/off) - Comprehensive error reporting for validation failures - Minimal performance impact on API calls - Full test coverage for schema generation and validation ## Technical Architecture ### Core Components #### 1. Schema Generation (`OpenAPISpecLoader`) - Convert OpenAPI response schemas to Zod schemas - Handle complex schema types (objects, arrays, refs, composition) - Support recursive schema resolution with cycle detection - Cache generated schemas for performance #### 2. Response Validation (`ApiClient`) - Integrate Zod validation into API call pipeline - Configurable validation modes - Detailed error reporting and logging - Fallback handling for validation failures #### 3. Configuration System - Add response validation settings to server config - Support different validation modes per environment - Optional schema caching configuration #### 4. Error Handling & Monitoring - Structured validation error reporting - Optional metrics collection for validation failures - Development-friendly error messages ## Implementation Plan ### Phase 1: Foundation (Week 1) **Goal**: Basic response validation working for simple schemas #### Dependencies ```bash npm install zod npm install --save-dev @types/node # if not already present ``` #### Core Implementation 1. **Add Zod Schema Generation to OpenAPISpecLoader** ```typescript // New methods in src/openapi-loader.ts ;-generateResponseZodSchema() - openApiSchemaToZod() - selectPrimarySuccessResponse() - handleSchemaComposition() // for allOf, oneOf, anyOf ``` 2. **Basic Schema Type Support** - Primitive types: string, number, integer, boolean - Basic string formats: email, uuid, date-time - Simple objects with properties - Arrays with typed items - Number constraints (min, max) 3. **Integrate Validation into ApiClient** ```typescript // Enhanced executeApiCall method - Retrieve response schema from tool metadata - Validate response data with Zod - Handle validation errors gracefully ``` 4. **Basic Configuration** ```typescript // Add to OpenAPIMCPServerConfig responseValidation?: 'strict' | 'warn' | 'off' ``` #### Deliverables - Basic Zod schema generation for simple OpenAPI schemas - Response validation in ApiClient with error handling - Configuration option for validation mode - Unit tests for schema generation - Integration tests for response validation ### Phase 2: Advanced Schema Support (Week 2) **Goal**: Handle complex OpenAPI schema patterns #### Advanced Schema Features 1. **Reference Resolution** ```typescript // Enhanced $ref handling - Component schema resolution - Recursive reference detection and handling - Cross-reference validation ``` 2. **Schema Composition** ```typescript // Support for OpenAPI composition keywords - allOf: z.intersection() - oneOf: z.discriminatedUnion() or z.union() - anyOf: z.union() - not: Custom validation logic ``` 3. **Advanced String Formats** ```typescript // Extended format support - date, time, date-time - uri, uri-reference - hostname, ipv4, ipv6 - Custom format validators ``` 4. **Complex Object Patterns** ```typescript // Advanced object features - additionalProperties handling - patternProperties support - Property dependencies - Conditional schemas (if/then/else) ``` 5. **Array Constraints** ```typescript // Array validation enhancements - minItems, maxItems - uniqueItems validation - Tuple validation (fixed-length arrays) ``` #### Enhanced Error Handling ```typescript // Detailed validation error reporting interface ValidationError { toolId: string path: string[] message: string received: any expected: string context: { httpStatus: number responseHeaders: Record<string, string> requestUrl: string } } ``` #### Deliverables - Support for all major OpenAPI schema patterns - Comprehensive $ref resolution with cycle detection - Enhanced error reporting with context - Performance optimizations (schema caching) - Extended test coverage for complex schemas ### Phase 3: Production Features (Week 3) **Goal**: Production-ready validation with monitoring and optimization #### Performance Optimizations 1. **Schema Caching** ```typescript // Cache compiled Zod schemas - LRU cache for generated schemas - Cache invalidation strategies - Memory usage monitoring ``` 2. **Lazy Schema Generation** ```typescript // Generate schemas on-demand - Defer schema compilation until first use - Background schema pre-compilation option ``` #### Monitoring & Observability 1. **Validation Metrics** ```typescript // Optional metrics collection interface ValidationMetrics { toolId: string validationResult: "success" | "failure" | "skipped" validationTime: number errorCount: number errorTypes: string[] } ``` 2. **Structured Logging** ```typescript // Enhanced logging for debugging - Validation success/failure logs - Schema generation logs - Performance metrics - Error aggregation ``` #### Advanced Configuration ```typescript // Extended configuration options interface ResponseValidationConfig { mode: "strict" | "warn" | "off" cacheSize?: number enableMetrics?: boolean customFormats?: Record<string, (value: string) => boolean> failureThreshold?: number // Auto-disable after N failures excludeTools?: string[] // Skip validation for specific tools } ``` #### Schema Evolution Support 1. **Backward Compatibility** ```typescript // Handle schema changes gracefully - Optional field handling - Deprecated field warnings - Schema version tracking ``` 2. **Development Tools** ```typescript // Developer experience enhancements - Schema diff reporting - Validation playground/testing tools - Schema documentation generation ``` #### Deliverables - Production-ready performance optimizations - Comprehensive monitoring and metrics - Advanced configuration options - Schema evolution and compatibility tools - Complete documentation and examples ## Technical Specifications ### Schema Generation Algorithm #### 1. Response Schema Selection ```typescript /** * Priority order for selecting response schema: * 1. 200 OK (most common success) * 2. 201 Created (for POST operations) * 3. 202 Accepted (for async operations) * 4. 204 No Content (empty response) * 5. First 2xx response found * 6. Default response (if no 2xx found) */ ``` #### 2. Content Type Priority ```typescript /** * Content type selection priority: * 1. application/json (primary target) * 2. application/hal+json (HAL format) * 3. application/vnd.api+json (JSON:API) * 4. text/plain (for simple responses) * 5. First available content type */ ``` #### 3. Schema Conversion Rules ```typescript /** * OpenAPI to Zod conversion mapping: * * string -> z.string() * string(email) -> z.string().email() * string(uuid) -> z.string().uuid() * string(date-time) -> z.string().datetime() * number -> z.number() * integer -> z.number().int() * boolean -> z.boolean() * array -> z.array(itemSchema) * object -> z.object(shapeSchema) * allOf -> z.intersection(schemas) * oneOf -> z.discriminatedUnion() or z.union() * anyOf -> z.union(schemas) * $ref -> resolve and convert referenced schema */ ``` ### Error Handling Strategy #### 1. Validation Failure Modes ```typescript enum ValidationMode { STRICT = "strict", // Throw error on validation failure WARN = "warn", // Log warning, return unvalidated data OFF = "off", // Skip validation entirely } ``` #### 2. Error Recovery ```typescript /** * Error recovery strategies: * 1. Validation failure -> detailed error with path information * 2. Schema generation failure -> fallback to no validation * 3. Zod compilation error -> log error, disable validation for tool * 4. Performance threshold exceeded -> temporary validation disable */ ``` ### Performance Considerations #### 1. Schema Compilation Cost - **One-time cost**: Schema compilation during tool loading - **Runtime cost**: Validation per API call - **Memory usage**: Cached compiled schemas - **Mitigation**: Lazy loading, LRU cache, compilation pooling #### 2. Validation Performance - **Target**: <5ms validation overhead per API call - **Monitoring**: Track validation time per tool - **Optimization**: Schema simplification, fast-path for simple types #### 3. Memory Management - **Schema cache**: Configurable size limit (default: 100 schemas) - **Cache eviction**: LRU with tool usage frequency weighting - **Memory monitoring**: Optional memory usage reporting ## Testing Strategy ### Unit Tests 1. **Schema Generation Tests** ```typescript // Test categories: - Basic type conversion (string, number, boolean) - Complex object schemas with nested properties - Array schemas with various item types - Reference resolution ($ref handling) - Schema composition (allOf, oneOf, anyOf) - Edge cases (empty schemas, recursive refs) - Error conditions (invalid schemas, missing refs) ``` 2. **Validation Logic Tests** ```typescript // Test scenarios: - Successful validation with valid data - Validation failures with detailed error checking - Configuration mode behavior (strict/warn/off) - Performance under load - Memory usage with large schemas ``` ### Integration Tests 1. **End-to-End Validation** ```typescript // Test flows: - OpenAPI spec loading -> schema generation -> API call -> validation - Multiple tools with different response schemas - Error handling across the entire pipeline - Configuration changes affecting validation behavior ``` 2. **Real API Testing** ```typescript // Test with actual APIs: - JSONPlaceholder API for standard REST patterns - GitHub API for complex nested objects - Custom test server for edge cases ``` ### Performance Tests 1. **Schema Generation Performance** - Large OpenAPI specs (>100 operations) - Complex nested schemas - Memory usage over time 2. **Validation Performance** - High-frequency API calls - Large response payloads - Concurrent validation operations ## Configuration Examples ### Basic Configuration ```typescript // Minimal setup - validation enabled with warnings { responseValidation: "warn" } ``` ### Development Configuration ```typescript // Development environment - strict validation with detailed logging { responseValidation: 'strict', validationConfig: { enableMetrics: true, cacheSize: 50, customFormats: { 'custom-id': (value: string) => /^[A-Z]{2}\d{6}$/.test(value) } } } ``` ### Production Configuration ```typescript // Production environment - optimized for performance { responseValidation: 'warn', validationConfig: { cacheSize: 200, enableMetrics: true, failureThreshold: 10, excludeTools: ['health-check', 'metrics'] } } ``` ## Migration Strategy ### Backward Compatibility - **Default behavior**: Validation disabled by default (opt-in) - **Existing APIs**: No changes to existing tool behavior - **Configuration**: New optional configuration fields - **Error handling**: Validation errors don't break existing error flows ### Rollout Plan 1. **Phase 1**: Deploy with validation disabled by default 2. **Phase 2**: Enable warning mode for development environments 3. **Phase 3**: Gradual rollout to production with monitoring 4. **Phase 4**: Consider enabling strict mode for critical APIs ## Documentation Plan ### Developer Documentation 1. **Configuration Guide** - How to enable response validation - Configuration options and their effects - Performance tuning recommendations 2. **Schema Generation Guide** - How OpenAPI schemas are converted to Zod - Supported schema patterns and limitations - Troubleshooting schema generation issues 3. **Error Handling Guide** - Understanding validation errors - Debugging validation failures - Best practices for handling validation in applications ### API Documentation 1. **Type Definitions** - Updated Tool interface with validation metadata - Configuration interface documentation - Error type definitions 2. **Examples** - Common validation scenarios - Error handling patterns - Performance optimization examples ## Future Enhancements ### Potential Extensions 1. **Request Validation** - Validate request payloads before sending - Parameter validation beyond basic type checking - Custom validation rules 2. **Schema Evolution Tracking** - Detect API schema changes over time - Compatibility reporting - Automated schema migration suggestions 3. **Advanced Validation Features** - Custom validation rules per tool - Conditional validation based on response status - Response transformation and normalization 4. **Integration Features** - OpenAPI spec validation and linting - Schema documentation generation - Test case generation from schemas ### Performance Optimizations 1. **Streaming Validation** - Validate large responses incrementally - Early validation failure detection - Memory-efficient validation for large payloads 2. **Parallel Validation** - Concurrent validation for multiple tools - Background validation for non-critical paths - Validation result caching ## Risk Assessment ### Technical Risks 1. **Performance Impact** - **Risk**: Validation overhead slows API calls - **Mitigation**: Performance monitoring, configurable validation, optimization 2. **Memory Usage** - **Risk**: Schema cache consumes excessive memory - **Mitigation**: Configurable cache size, LRU eviction, memory monitoring 3. **Schema Complexity** - **Risk**: Complex OpenAPI schemas can't be converted to Zod - **Mitigation**: Graceful fallback, comprehensive testing, incremental support ### Operational Risks 1. **Validation Failures** - **Risk**: Strict validation breaks existing integrations - **Mitigation**: Default to warning mode, gradual rollout, escape hatches 2. **Configuration Complexity** - **Risk**: Too many configuration options confuse users - **Mitigation**: Sensible defaults, clear documentation, configuration validation ### Mitigation Strategies 1. **Gradual Rollout**: Start with warning mode, monitor impact 2. **Escape Hatches**: Allow disabling validation per tool or globally 3. **Monitoring**: Track validation performance and failure rates 4. **Documentation**: Comprehensive guides and examples 5. **Testing**: Extensive test coverage for edge cases ## Success Metrics ### Technical Metrics - **Schema Generation Success Rate**: >95% of OpenAPI schemas successfully converted - **Validation Performance**: <5ms average validation overhead - **Memory Usage**: <50MB additional memory for schema cache - **Error Rate**: <1% validation failures in production ### User Experience Metrics - **Developer Adoption**: >50% of projects enable validation within 3 months - **Error Detection**: Validation catches >80% of API schema changes - **Support Requests**: <5 validation-related support requests per month - **Documentation Usage**: >70% of users find documentation helpful ### Business Metrics - **API Reliability**: Reduced API integration failures by >30% - **Development Speed**: Faster debugging of API integration issues - **Production Stability**: Fewer production incidents due to API changes - **Developer Satisfaction**: Improved developer experience scores --- ## Implementation Checklist ### Phase 1: Foundation - [ ] Add Zod dependency to package.json - [ ] Implement basic schema generation in OpenAPISpecLoader - [ ] Add response validation to ApiClient - [ ] Create basic configuration options - [ ] Write unit tests for schema generation - [ ] Write integration tests for validation - [ ] Update documentation ### Phase 2: Advanced Features - [ ] Implement complex schema pattern support - [ ] Add comprehensive $ref resolution - [ ] Enhance error reporting and logging - [ ] Implement schema caching - [ ] Add performance monitoring - [ ] Extend test coverage - [ ] Update configuration options ### Phase 3: Production Ready - [ ] Optimize performance and memory usage - [ ] Add metrics and observability - [ ] Implement advanced configuration - [ ] Add schema evolution support - [ ] Complete documentation - [ ] Conduct performance testing - [ ] Plan rollout strategy --- _This plan provides a comprehensive roadmap for implementing response validation with Zod schemas. The phased approach ensures incremental value delivery while maintaining system stability and performance._

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/ivo-toby/mcp-openapi-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server