Merge pull request #410 from yuki3738/add_priority_validation_to_annotations

Add range validation for `MCP::Annotations#priority` per MCP specification
This commit is contained in:
Koichi ITO 2026-06-16 00:55:55 +09:00 • committed by GitHub
commit acec71df74
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 28 additions and 0 deletions

View file

@ -5,6 +5,8 @@ module MCP
attr_reader :audience, :priority, :last_modified
def initialize(audience: nil, priority: nil, last_modified: nil)
raise ArgumentError, "The value of priority must be between 0 and 1." if priority && !priority.between?(0, 1)
@audience = audience
@priority = priority
@last_modified = last_modified

View file

@ -55,5 +55,31 @@ module MCP
assert_equal({ lastModified: timestamp }, annotations.to_h)
end
def test_valid_priority_at_lower_bound
assert_nothing_raised do
Annotations.new(priority: 0)
end
end
def test_valid_priority_at_upper_bound
assert_nothing_raised do
Annotations.new(priority: 1)
end
end
def test_invalid_priority_above_upper_bound
exception = assert_raises(ArgumentError) do
Annotations.new(priority: 1.5)
end
assert_equal("The value of priority must be between 0 and 1.", exception.message)
end
def test_invalid_priority_below_lower_bound
exception = assert_raises(ArgumentError) do
Annotations.new(priority: -0.1)
end
assert_equal("The value of priority must be between 0 and 1.", exception.message)
end
end
end