Validate ToolCall Args against Schema

(cherry picked from commit eeb287117535dfb43785704171625323f01d5bda)
This commit is contained in:
Tyler Rowsell 2025-05-22 15:24:10 -04:00
commit 600c96f66a

View file

@ -82,6 +82,23 @@ module MCP
assert_equal expected, tool.input_schema.to_h
end
test "raises detailed error message for invalid schema" do
error = assert_raises(ArgumentError) do
Class.new(MCP::Tool) do
input_schema(
properties: {
count: { type: "integer", minimum: "not a number" },
},
required: [:count],
)
end
end
assert_includes error.message, "Invalid JSON Schema"
assert_includes error.message, "#/properties/count/minimum"
assert_includes error.message, "string did not match the following type: number"
end
test ".define allows definition of simple tools with a block" do
tool = Tool.define(name: "mock_tool", description: "a mock tool for testing") do |_|
Tool::Response.new([{ type: "text", content: "OK" }])
@ -226,5 +243,23 @@ module MCP
assert_equal response.content, [{ type: "text", content: "OK" }]
assert_equal response.is_error, false
end
test "input_schema rejects any $ref in schema" do
schema_with_ref = {
properties: {
foo: { "$ref" => "#/definitions/bar" },
},
required: [],
definitions: {
bar: { type: "string" },
},
}
error = assert_raises(ArgumentError) do
Class.new(MCP::Tool) do
input_schema schema_with_ref
end
end
assert_match(/Invalid JSON Schema/, error.message)
end
end
end