Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions lib/mcp/tool.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,23 +93,30 @@ def icons(value = NOT_SET)
def input_schema(value = NOT_SET)
if value == NOT_SET
input_schema_value
elsif value.is_a?(Hash)
@input_schema_value = InputSchema.new(value)
elsif value.is_a?(InputSchema)
@input_schema_value = value
elsif (schema = coerce_schema(value, InputSchema))
@input_schema_value = schema
end
end

def output_schema(value = NOT_SET)
if value == NOT_SET
output_schema_value
elsif value.is_a?(Hash)
@output_schema_value = OutputSchema.new(value)
elsif value.is_a?(OutputSchema)
@output_schema_value = value
elsif (schema = coerce_schema(value, OutputSchema))
@output_schema_value = schema
end
end

def coerce_schema(value, schema_class)
case value
when Hash
schema_class.new(value)
when schema_class
value
end
end

private :coerce_schema

def meta(value = NOT_SET)
if value == NOT_SET
@meta_value
Expand Down
5 changes: 1 addition & 4 deletions lib/mcp/tool/input_schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ def missing_required_arguments(arguments)
end

def validate_arguments(arguments)
errors = fully_validate(arguments)
if errors.any?
raise ValidationError, "Invalid arguments: #{errors.join(", ")}"
end
fully_validate!(arguments, "arguments")
end
end
end
Expand Down
5 changes: 1 addition & 4 deletions lib/mcp/tool/output_schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ class OutputSchema < Schema
class ValidationError < StandardError; end

def validate_result(result)
errors = fully_validate(result)
if errors.any?
raise ValidationError, "Invalid result: #{errors.join(", ")}"
end
fully_validate!(result, "result")
end
end
end
Expand Down
7 changes: 5 additions & 2 deletions lib/mcp/tool/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,11 @@ def to_h

private

def fully_validate(data)
JSON::Validator.fully_validate(schema_for_validation, data)
def fully_validate!(payload, label)
errors = JSON::Validator.fully_validate(schema_for_validation, payload)
if errors.any?
raise self.class::ValidationError, "Invalid #{label}: #{errors.join(", ")}"
end
end

def validate_schema!
Expand Down