Class: Ask::MCP::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/ask/mcp/validator.rb

Overview

Validates tool call arguments against JSON Schema input schemas. Uses the json-schema gem to validate arguments before sending to a server.

Defined Under Namespace

Classes: ValidationError

Instance Method Summary collapse

Constructor Details

#initialize(schema) ⇒ Validator

Returns a new instance of Validator.



10
11
12
# File 'lib/ask/mcp/validator.rb', line 10

def initialize(schema)
  @schema = schema
end

Instance Method Details

#valid?(arguments) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
33
34
35
# File 'lib/ask/mcp/validator.rb', line 30

def valid?(arguments)
  validate!(arguments)
  true
rescue ValidationError
  false
end

#validate!(arguments) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/ask/mcp/validator.rb', line 14

def validate!(arguments)
  return true if @schema.nil? || @schema.empty?

  require "json-schema"

  string_schema = deep_stringify_keys(@schema)
  data = arguments.is_a?(Hash) ? deep_stringify_keys(arguments) : arguments

  errors = JSON::Validator.fully_validate(string_schema, data)
  if errors.any?
    raise ValidationError, "Validation failed: #{errors.join(", ")}"
  end

  true
end