VALIDATION.md•4.34 kB
# Schema Validation in Automation Script Generator MCP Server
## Overview
Yes, your MCP server now includes comprehensive **schema validation** using AJV (Another JSON Schema Validator). Every tool call is validated against its defined JSON schema before execution.
## What's Validated
### 1. **Type Safety**
- Ensures parameters are the correct type (string, object, array, etc.)
- Prevents type-related runtime errors
### 2. **Required Fields**
- Validates that all mandatory parameters are provided
- Throws clear errors for missing required fields
### 3. **String Constraints**
- Enforces minimum length requirements (e.g., `scenario_title` cannot be empty)
- Prevents empty or invalid string inputs
### 4. **Additional Properties**
- Blocks unknown/extra properties in requests
- Maintains strict API contracts
### 5. **Enum Validation**
- Validates against predefined allowed values
- Example: `review_criteria` must be one of: `['docs', 'pom', 'functions', 'utils', 'format', 'existing_steps']`
## Schema Examples
### fetch_notion_scenarios
```json
{
"type": "object",
"properties": {
"database_id": { "type": "string" },
"filter": {
"type": "object",
"properties": {
"tags": { "type": "array", "items": { "type": "string" } },
"status": { "type": "string" }
}
}
},
"required": ["database_id"],
"additionalProperties": false
}
```
### generate_feature_file
```json
{
"type": "object",
"properties": {
"scenario_title": { "type": "string", "minLength": 1 },
"gherkin_syntax": { "type": "string", "minLength": 1 },
"tags": { "type": "array", "items": { "type": "string" } },
"output_path": { "type": "string", "minLength": 1 }
},
"required": ["scenario_title", "gherkin_syntax", "output_path"],
"additionalProperties": false
}
```
## Error Handling
When validation fails, the server returns clear error messages:
```javascript
// Missing required field
"Validation failed for tool fetch_notion_scenarios: root: must have required property 'database_id'";
// Invalid type
"Validation failed for tool generate_feature_file: /scenario_title: must be string";
// Empty string constraint
"Validation failed for tool generate_feature_file: /scenario_title: must NOT have fewer than 1 characters";
// Extra properties
"Validation failed for tool fetch_notion_scenarios: root: must NOT have additional properties";
```
## Implementation Details
### 1. **Schema Storage**
- All schemas are stored in `this.toolSchemas` during initialization
- Schemas are compiled once for performance
### 2. **Validation Method**
```javascript
validateArgs(toolName, args) {
const schema = this.toolSchemas[toolName];
const validate = ajv.compile(schema);
const valid = validate(args);
if (!valid) {
const errors = validate.errors
.map(err => `${err.instancePath || 'root'}: ${err.message}`)
.join(', ');
throw new McpError(ErrorCode.InvalidParams,
`Validation failed for tool ${toolName}: ${errors}`);
}
}
```
### 3. **Request Handler Integration**
```javascript
this.server.setRequestHandler(CallToolRequestSchema, async (request) => {
const { name, arguments: args } = request.params;
// Validate before execution
this.validateArgs(name, args || {});
// Execute tool...
});
```
## Benefits
1. **Early Error Detection**: Catch invalid inputs before processing
2. **Clear Error Messages**: Detailed feedback for debugging
3. **API Consistency**: Enforces consistent parameter formats
4. **Type Safety**: Prevents runtime type errors
5. **Documentation**: Schemas serve as API documentation
## Testing
Run the validation demo to see it in action:
```bash
npm run test:validation
```
This demonstrates various validation scenarios including:
- Valid requests that pass validation
- Invalid requests that correctly fail validation
- Different types of validation errors
## Conclusion
Your MCP server now has robust input validation that ensures:
- All tool calls receive valid, well-formed parameters
- Clear error messages guide users when requests are malformed
- The server maintains high reliability and predictable behavior
- API contracts are enforced automatically
The validation system is production-ready and follows JSON Schema standards for maximum compatibility and maintainability.