Add schema validation to lowlevel server by bhosmer-ant · Pull Request #1005 · modelcontextprotocol/python-sdk

ihrpr

ihrpr previously approved these changes Jun 24, 2025

- Add jsonschema dependency for schema validation
- Implement tool definition cache in Server class that gets refreshed when list_tools is called
- Add _validate_tool_arguments helper method to validate tool arguments against inputSchema
- Update call_tool handler to validate arguments before execution
- Log warning and skip validation for tools not found in cache
- Add comprehensive tests for validation scenarios

This ensures tool arguments are validated against their JSON schemas before execution,
providing better error messages and preventing invalid tool calls from reaching handlers.
- Refactor code to extract _get_tool_definition helper and simplify validation
- Update call_tool to support three return types: content only, dict only, or both
- Add outputSchema validation that checks structured content matches the schema
- Serialize dict-only results to JSON text content
- Factor error result construction into _make_error_result helper
- Add comprehensive tests for all output validation scenarios

The server now validates tool outputs against their defined schemas, providing
better error messages and ensuring tool responses match their contracts.

@bhosmer-ant

@bhosmer-ant

@bhosmer-ant

@rekby rekby mentioned this pull request

Aug 11, 2025