Fix: Disallow $ref in tool input schemas
This commit is contained in:
parent
bae35facf6
commit
8dee60cfc2
3 changed files with 27 additions and 1 deletions
|
|
@ -37,6 +37,7 @@ module MCP
|
||||||
private
|
private
|
||||||
|
|
||||||
def validate_schema!
|
def validate_schema!
|
||||||
|
check_for_refs!
|
||||||
schema = to_h
|
schema = to_h
|
||||||
schema_reader = JSON::Schema::Reader.new(
|
schema_reader = JSON::Schema::Reader.new(
|
||||||
accept_uri: false,
|
accept_uri: false,
|
||||||
|
|
@ -48,6 +49,19 @@ module MCP
|
||||||
raise ArgumentError, "Invalid JSON Schema: #{errors.join(", ")}"
|
raise ArgumentError, "Invalid JSON Schema: #{errors.join(", ")}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def check_for_refs!(obj = properties)
|
||||||
|
case obj
|
||||||
|
when Hash
|
||||||
|
if obj.key?("$ref") || obj.key?(:$ref)
|
||||||
|
raise ArgumentError, "Invalid JSON Schema: $ref is not allowed in tool input schemas"
|
||||||
|
end
|
||||||
|
|
||||||
|
obj.each_value { |value| check_for_refs!(value) }
|
||||||
|
when Array
|
||||||
|
obj.each { |item| check_for_refs!(item) }
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -61,6 +61,18 @@ module MCP
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "rejects schemas with $ref references" do
|
||||||
|
assert_raises(ArgumentError) do
|
||||||
|
InputSchema.new(properties: { foo: { "$ref" => "#/definitions/bar" } }, required: [:foo])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
test "rejects schemas with symbol $ref references" do
|
||||||
|
assert_raises(ArgumentError) do
|
||||||
|
InputSchema.new(properties: { foo: { :$ref => "#/definitions/bar" } }, required: [:foo])
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -249,7 +249,7 @@ module MCP
|
||||||
properties: {
|
properties: {
|
||||||
foo: { "$ref" => "#/definitions/bar" },
|
foo: { "$ref" => "#/definitions/bar" },
|
||||||
},
|
},
|
||||||
required: [],
|
required: ["foo"],
|
||||||
definitions: {
|
definitions: {
|
||||||
bar: { type: "string" },
|
bar: { type: "string" },
|
||||||
},
|
},
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue