This document describes the input validation schemas implemented for the EPR MCP Server tools.
The schema validation system uses Pydantic models to ensure that all input data to the MCP tools is properly validated before processing. This helps prevent errors, improves security, and provides clear error messages when invalid data is provided.
- BaseModel: All schemas inherit from Pydantic's BaseModel
- Field: Used for field definitions with validation rules and descriptions
- validator: Custom validation decorators for complex field validation
Validates input for searching events:
{
"name": "optional string",
"version": "optional string",
"release": "optional string",
"platform_id": "optional string",
"package": "optional string",
"description": "optional string",
"success": "optional boolean",
"event_receiver_id": "optional string"
}Validates input for searching event receivers:
{
"name": "optional string",
"type": "optional string",
"version": "optional string",
"description": "optional string"
}Validates input for searching event receiver groups:
{
"name": "optional string",
"type": "optional string",
"version": "optional string",
"description": "optional string"
}Validates input for creating events (all fields required):
{
"name": "string (min_length=1)",
"version": "string (min_length=1)",
"release": "string (min_length=1)",
"platform_id": "string (min_length=1)",
"package": "string (min_length=1)",
"description": "string (min_length=1)",
"event_receiver_id": "string (min_length=1)",
"success": "boolean",
"payload": "dict (required, must be dictionary)"
}Validates input for creating event receivers (all fields required):
{
"name": "string (min_length=1)",
"type": "string (min_length=1)",
"version": "string (min_length=1)",
"description": "string (min_length=1)"
}Validates input for creating event receiver groups (all fields required):
{
"name": "string (min_length=1)",
"type": "string (min_length=1)",
"version": "string (min_length=1)",
"description": "string (min_length=1)",
"event_receiver_ids": "list of strings (min_items=1, all non-empty)"
}Validates input for fetch operations:
{
"id": "string (min_length=1, trimmed)"
}Wraps search inputs in a data field:
{
"data": EventSearchInput | EventReceiverSearchInput | EventReceiverGroupSearchInput
}Wraps creation inputs in a data field:
{
"data": EventCreateInput | EventReceiverCreateInput | EventReceiverGroupCreateInput
}The main validation function is validate_input(operation, input_data):
from schemas import validate_input
from pydantic import ValidationError
try:
validated_data = validate_input("search_events", {
"data": {
"name": "foo",
"version": "1.0.0"
}
})
# Use validated_data...
except (ValidationError, ValueError) as e:
# Handle validation error
print(f"Validation error: {e}")The system supports these operations:
search_events→ SearchDataWrappersearch_receivers→ SearchDataWrappersearch_groups→ SearchDataWrappercreate_event→ CreateDataWrappercreate_receiver→ CreateDataWrappercreate_group→ CreateDataWrapperfetch_event→ FetchInputfetch_receiver→ FetchInputfetch_group→ FetchInput
Each MCP tool now includes validation:
@mcp.tool(title="Search Events", description="Search for events in EPR")
async def search_events(data: dict) -> str:
try:
validated_data = validate_input("search_events", data)
except (ValidationError, ValueError) as e:
return f"Validation error: {str(e)}"
# Process validated_data...- Type Safety: Ensures correct data types for all fields
- Required Field Validation: Prevents missing required fields
- Format Validation: Validates string lengths, list contents, etc.
- Clear Error Messages: Pydantic provides detailed validation errors
- Documentation: Schema serves as documentation for expected input formats
- Security: Prevents injection attacks and malformed data
Run the validation tests:
cd /home/bsmith/go/src/github.com/xbcsmith/epr-workshop/11-epr-mcp-server/src
python test_schemas.pyValidation errors are caught and returned as user-friendly error messages:
- Missing required fields
- Invalid data types
- Empty strings where content is required
- Invalid list contents
- Malformed dictionaries
pydantic>=2.0.0: Core validation librarytyping: Type hints for Python < 3.9 compatibility