Merge pull request #144 from nilcolor/output_schema_arrays

Allow output schema to be array of objects
This commit is contained in:
Koichi ITO 2025-09-28 03:17:09 +09:00 • committed by GitHub
commit de1e833ae3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 72 additions and 38 deletions

View file

@ -5,15 +5,10 @@ require "test_helper"
module MCP
class Tool
class OutputSchemaTest < ActiveSupport::TestCase
test "required arguments are converted to symbols" do
output_schema = OutputSchema.new(properties: { result: { type: "string" } }, required: ["result"])
assert_equal [:result], output_schema.required
end
test "to_h returns a hash representation of the output schema" do
output_schema = OutputSchema.new(properties: { result: { type: "string" } }, required: [:result])
assert_equal(
{ type: "object", properties: { result: { type: "string" } }, required: [:result] },
{ type: "object", properties: { result: { type: "string" } }, required: ["result"] },
output_schema.to_h,
)
end
@ -41,7 +36,7 @@ module MCP
test "valid schema initialization" do
schema = OutputSchema.new(properties: { foo: { type: "string" } }, required: [:foo])
assert_equal({ type: "object", properties: { foo: { type: "string" } }, required: [:foo] }, schema.to_h)
assert_equal({ type: "object", properties: { foo: { type: "string" } }, required: ["foo"] }, schema.to_h)
end
test "invalid schema raises argument error" do
@ -135,6 +130,26 @@ module MCP
schema.validate_result(invalid_result)
end
end
test "allow to declare array schemas" do
schema = OutputSchema.new({
type: "array",
items: {
properties: { foo: { type: "string" } },
required: [:foo],
},
})
assert_equal(
{
type: "array",
items: {
properties: { foo: { type: "string" } },
required: ["foo"],
},
},
schema.to_h,
)
end
end
end
end

View file

@ -267,7 +267,7 @@ module MCP
title: "Mock Tool",
description: "a mock tool for testing",
inputSchema: { type: "object" },
outputSchema: { type: "object", properties: { result: { type: "string" } }, required: [:result] },
outputSchema: { type: "object", properties: { result: { type: "string" } }, required: ["result"] },
}
assert_equal expected, tool.to_h
end
@ -292,7 +292,7 @@ module MCP
end
tool = HashOutputSchemaTool
expected = { type: "object", properties: { result: { type: "string" } }, required: [:result] }
expected = { type: "object", properties: { result: { type: "string" } }, required: ["result"] }
assert_equal expected, tool.output_schema.to_h
end
@ -302,7 +302,7 @@ module MCP
end
tool = OutputSchemaObjectTool
expected = { type: "object", properties: { result: { type: "string" } }, required: [:result] }
expected = { type: "object", properties: { result: { type: "string" } }, required: ["result"] }
assert_equal expected, tool.output_schema.to_h
end
@ -354,7 +354,7 @@ module MCP
assert_equal "mock_tool", tool.name_value
assert_equal "a mock tool for testing", tool.description
assert_instance_of Tool::OutputSchema, tool.output_schema
expected_output_schema = { type: "object", properties: { result: { type: "string" } }, required: [:result] }
expected_output_schema = { type: "object", properties: { result: { type: "string" } }, required: ["result"] }
assert_equal expected_output_schema, tool.output_schema.to_h
end
@ -379,7 +379,7 @@ module MCP
expected_input = { type: "object", properties: { message: { type: "string" } }, required: [:message] }
assert_equal expected_input, tool.input_schema.to_h
expected_output = { type: "object", properties: { result: { type: "string" }, success: { type: "boolean" } }, required: [:result, :success] }
expected_output = { type: "object", properties: { result: { type: "string" }, success: { type: "boolean" } }, required: ["result", "success"] }
assert_equal expected_output, tool.output_schema.to_h
end
end