Skip to content

Commit cb5e875

Browse files
committed
Add validation to MCP::Configuration setters
I noticed through the existing tests that `MCP::Configuration#protocol_version=` allows setting an arbitrary protocol version. Since the official MCP Ruby SDK is an implementation of the MCP specification, it should not support arbitrary protocol versions. This change adds validation to the setter, consistent with `Configuration#initialize`. The same validation has also been added to `MCP::Configuration#validate_tool_call_arguments=`.
1 parent e168292 commit cb5e875

File tree

2 files changed

+43
-11
lines changed

2 files changed

+43
-11
lines changed

lib/mcp/configuration.rb

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,29 @@ class Configuration
55
DEFAULT_PROTOCOL_VERSION = "2025-06-18"
66
SUPPORTED_PROTOCOL_VERSIONS = [DEFAULT_PROTOCOL_VERSION, "2025-03-26", "2024-11-05"]
77

8-
attr_writer :exception_reporter, :instrumentation_callback, :protocol_version, :validate_tool_call_arguments
8+
attr_writer :exception_reporter, :instrumentation_callback
99

1010
def initialize(exception_reporter: nil, instrumentation_callback: nil, protocol_version: nil,
1111
validate_tool_call_arguments: true)
1212
@exception_reporter = exception_reporter
1313
@instrumentation_callback = instrumentation_callback
1414
@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
19-
unless validate_tool_call_arguments.is_a?(TrueClass) || validate_tool_call_arguments.is_a?(FalseClass)
20-
raise ArgumentError, "validate_tool_call_arguments must be a boolean"
15+
if protocol_version
16+
validate_protocol_version!(protocol_version)
2117
end
18+
validate_value_of_validate_tool_call_arguments!(validate_tool_call_arguments)
19+
20+
@validate_tool_call_arguments = validate_tool_call_arguments
21+
end
22+
23+
def protocol_version=(protocol_version)
24+
validate_protocol_version!(protocol_version)
25+
26+
@protocol_version = protocol_version
27+
end
28+
29+
def validate_tool_call_arguments=(validate_tool_call_arguments)
30+
validate_value_of_validate_tool_call_arguments!(validate_tool_call_arguments)
2231

2332
@validate_tool_call_arguments = validate_tool_call_arguments
2433
end
@@ -83,6 +92,19 @@ def merge(other)
8392

8493
private
8594

95+
def validate_protocol_version!(protocol_version)
96+
unless SUPPORTED_PROTOCOL_VERSIONS.include?(protocol_version)
97+
message = "protocol_version must be #{SUPPORTED_PROTOCOL_VERSIONS[0...-1].join(", ")}, or #{SUPPORTED_PROTOCOL_VERSIONS[-1]}"
98+
raise ArgumentError, message
99+
end
100+
end
101+
102+
def validate_value_of_validate_tool_call_arguments!(validate_tool_call_arguments)
103+
unless validate_tool_call_arguments.is_a?(TrueClass) || validate_tool_call_arguments.is_a?(FalseClass)
104+
raise ArgumentError, "validate_tool_call_arguments must be a boolean"
105+
end
106+
end
107+
86108
def default_exception_reporter
87109
@default_exception_reporter ||= ->(exception, server_context) {}
88110
end

test/mcp/configuration_test.rb

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,21 @@ class ConfigurationTest < ActiveSupport::TestCase
4040
assert_equal Configuration::DEFAULT_PROTOCOL_VERSION, config.protocol_version
4141
end
4242

43-
test "allows setting a custom protocol version" do
43+
test "raises ArgumentError when protocol_version is not a supported protocol version" do
4444
config = Configuration.new
45-
custom_version = "2025-03-27"
46-
config.protocol_version = custom_version
47-
assert_equal custom_version, config.protocol_version
45+
exception = assert_raises(ArgumentError) do
46+
custom_version = "2025-03-27"
47+
config.protocol_version = custom_version
48+
end
49+
assert_equal("protocol_version must be 2025-06-18, 2025-03-26, or 2024-11-05", exception.message)
50+
end
51+
52+
test "raises ArgumentError when protocol_version is not a boolean value" do
53+
config = Configuration.new
54+
exception = assert_raises(ArgumentError) do
55+
config.validate_tool_call_arguments = "true"
56+
end
57+
assert_equal("validate_tool_call_arguments must be a boolean", exception.message)
4858
end
4959

5060
test "merges protocol version from other configuration" do

0 commit comments

Comments
 (0)