Class: Otto::MCP::Validator
- Inherits:
-
Object
- Object
- Otto::MCP::Validator
- Defined in:
- lib/otto/mcp/schema_validation.rb
Overview
JSON Schema validator for MCP protocol requests
Instance Method Summary collapse
-
#initialize ⇒ Validator
constructor
A new instance of Validator.
- #validate_request(data) ⇒ Object
- #validate_tool_arguments(tool_name, arguments, schema) ⇒ Object
Constructor Details
#initialize ⇒ Validator
Returns a new instance of Validator.
19 20 21 22 |
# File 'lib/otto/mcp/schema_validation.rb', line 19 def initialize @schemas = {} @json_schemer_available = defined?(JSONSchemer) end |
Instance Method Details
#validate_request(data) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/otto/mcp/schema_validation.rb', line 24 def validate_request(data) return true unless @json_schemer_available schema = mcp_request_schema validation_errors = schema.validate(data).to_a unless validation_errors.empty? = validation_errors.map { |error| error['details'] || error['error'] || error.to_s }.join(', ') raise ValidationError, "Invalid MCP request: #{}" end true end |
#validate_tool_arguments(tool_name, arguments, schema) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/otto/mcp/schema_validation.rb', line 38 def validate_tool_arguments(tool_name, arguments, schema) return true unless @json_schemer_available && schema schemer = JSONSchemer.schema(schema) validation_errors = schemer.validate(arguments).to_a unless validation_errors.empty? = validation_errors.map { |error| error['details'] || error['error'] || error.to_s }.join(', ') raise ValidationError, "Invalid arguments for tool #{tool_name}: #{}" end true end |