Merge pull request #242 from koic/support_ref_in_tool_schema

Allow `$ref` in `Tool::Schema` for protocol version 2025-11-25
This commit is contained in:
Koichi ITO 2026-03-01 17:16:47 +09:00 • committed by GitHub
commit 145dc1f2c5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 133 additions and 34 deletions

View file

@ -219,6 +219,14 @@ module MCP
message = "Error occurred in server_info. `description` is not supported in protocol version 2025-06-18 or earlier"
raise ArgumentError, message
end
tools_with_ref = @tools.each_with_object([]) do |(tool_name, tool), names|
names << tool_name if schema_contains_ref?(tool.input_schema_value.to_h)
end
unless tools_with_ref.empty?
message = "Error occurred in #{tools_with_ref.join(", ")}. `$ref` in input schemas is supported by protocol version 2025-11-25 or higher"
raise ArgumentError, message
end
end
if @configuration.protocol_version <= "2025-03-26"
@ -259,6 +267,17 @@ module MCP
raise ToolNotUnique, duplicated_tool_names unless duplicated_tool_names.empty?
end
def schema_contains_ref?(schema)
case schema
when Hash
schema.any? { |key, value| key.to_s == "$ref" || schema_contains_ref?(value) }
when Array
schema.any? { |element| schema_contains_ref?(element) }
else
false
end
end
def handle_request(request, method)
handler = @handlers[method]
unless handler

View file

@ -31,10 +31,6 @@ module MCP
case schema
when Hash
schema.each_with_object({}) do |(key, value), result|
if key.casecmp?("$ref")
raise ArgumentError, "Invalid JSON Schema: $ref is not allowed in tool schemas"
end
result[yield(key)] = deep_transform_keys(value, &block)
end
when Array