Skip to content

Commit e4d5f19

Browse files
authored
Merge pull request #80 from koic/validate_protocol_version_value
Validate `protocol_version` value
2 parents 54c1f96 + f13522d commit e4d5f19

File tree

4 files changed

+22
-7
lines changed

4 files changed

+22
-7
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,8 @@ This will make all new server instances use the specified protocol version inste
338338
MCP::Server.protocol_version = nil
339339
```
340340

341+
If an invalid `protocol_version` value is set, an `ArgumentError` is raised.
342+
341343
Be sure to check the [MCP spec](https://modelcontextprotocol.io/specification/2025-03-26) for the protocol version to understand the supported features for the version being set.
342344

343345
### Exception Reporting

lib/mcp/configuration.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
module MCP
44
class Configuration
55
DEFAULT_PROTOCOL_VERSION = "2024-11-05"
6+
SUPPORTED_PROTOCOL_VERSIONS = ["2025-06-18", "2025-03-26", DEFAULT_PROTOCOL_VERSION]
67

78
attr_writer :exception_reporter, :instrumentation_callback, :protocol_version, :validate_tool_call_arguments
89

@@ -11,6 +12,10 @@ def initialize(exception_reporter: nil, instrumentation_callback: nil, protocol_
1112
@exception_reporter = exception_reporter
1213
@instrumentation_callback = instrumentation_callback
1314
@protocol_version = protocol_version
15+
if protocol_version && !SUPPORTED_PROTOCOL_VERSIONS.include?(protocol_version)
16+
message = "protocol_version must be #{SUPPORTED_PROTOCOL_VERSIONS[0...-1].join(", ")}, or #{SUPPORTED_PROTOCOL_VERSIONS[-1]}"
17+
raise ArgumentError, message
18+
end
1419
unless validate_tool_call_arguments.is_a?(TrueClass) || validate_tool_call_arguments.is_a?(FalseClass)
1520
raise ArgumentError, "validate_tool_call_arguments must be a boolean"
1621
end

test/mcp/configuration_test.rb

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,18 @@ class ConfigurationTest < ActiveSupport::TestCase
4848
end
4949

5050
test "merges protocol version from other configuration" do
51-
config1 = Configuration.new(protocol_version: "2025-03-27")
52-
config2 = Configuration.new(protocol_version: "2025-03-28")
51+
config1 = Configuration.new(protocol_version: "2025-03-26")
52+
config2 = Configuration.new(protocol_version: "2025-06-18")
5353
config3 = Configuration.new
5454

5555
merged = config1.merge(config2)
56-
assert_equal "2025-03-28", merged.protocol_version
56+
assert_equal "2025-06-18", merged.protocol_version
5757

5858
merged = config1.merge(config3)
59-
assert_equal "2025-03-27", merged.protocol_version
59+
assert_equal "2025-03-26", merged.protocol_version
6060

6161
merged = config3.merge(config1)
62-
assert_equal "2025-03-27", merged.protocol_version
62+
assert_equal "2025-03-26", merged.protocol_version
6363
end
6464

6565
test "defaults validate_tool_call_arguments to true" do
@@ -96,10 +96,18 @@ class ConfigurationTest < ActiveSupport::TestCase
9696
refute merged.validate_tool_call_arguments
9797
end
9898

99+
test "raises ArgumentError when protocol_version is not a supported value" do
100+
exception = assert_raises(ArgumentError) do
101+
Configuration.new(protocol_version: "1999-12-31")
102+
end
103+
assert_match(/\Aprotocol_version must be/, exception.message)
104+
end
105+
99106
test "raises ArgumentError when validate_tool_call_arguments is not a boolean" do
100-
assert_raises(ArgumentError) do
107+
exception = assert_raises(ArgumentError) do
101108
Configuration.new(validate_tool_call_arguments: "true")
102109
end
110+
assert_equal("validate_tool_call_arguments must be a boolean", exception.message)
103111
end
104112
end
105113
end

test/mcp/server_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -804,7 +804,7 @@ def call(message:, server_context: nil)
804804
end
805805

806806
test "server protocol version can be overridden via configuration" do
807-
custom_version = "2025-03-27"
807+
custom_version = "2025-03-26"
808808
configuration = Configuration.new(protocol_version: custom_version)
809809
server = Server.new(name: "test_server", configuration: configuration)
810810

0 commit comments

Comments
 (0)