Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 29 additions & 7 deletions lib/mcp/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,29 @@ class Configuration
DEFAULT_PROTOCOL_VERSION = "2025-06-18"
SUPPORTED_PROTOCOL_VERSIONS = [DEFAULT_PROTOCOL_VERSION, "2025-03-26", "2024-11-05"]

attr_writer :exception_reporter, :instrumentation_callback, :protocol_version, :validate_tool_call_arguments
attr_writer :exception_reporter, :instrumentation_callback

def initialize(exception_reporter: nil, instrumentation_callback: nil, protocol_version: nil,
validate_tool_call_arguments: true)
@exception_reporter = exception_reporter
@instrumentation_callback = instrumentation_callback
@protocol_version = protocol_version
if protocol_version && !SUPPORTED_PROTOCOL_VERSIONS.include?(protocol_version)
message = "protocol_version must be #{SUPPORTED_PROTOCOL_VERSIONS[0...-1].join(", ")}, or #{SUPPORTED_PROTOCOL_VERSIONS[-1]}"
raise ArgumentError, message
end
unless validate_tool_call_arguments.is_a?(TrueClass) || validate_tool_call_arguments.is_a?(FalseClass)
raise ArgumentError, "validate_tool_call_arguments must be a boolean"
if protocol_version
validate_protocol_version!(protocol_version)
end
validate_value_of_validate_tool_call_arguments!(validate_tool_call_arguments)

@validate_tool_call_arguments = validate_tool_call_arguments
end

def protocol_version=(protocol_version)
validate_protocol_version!(protocol_version)

@protocol_version = protocol_version
end

def validate_tool_call_arguments=(validate_tool_call_arguments)
validate_value_of_validate_tool_call_arguments!(validate_tool_call_arguments)

@validate_tool_call_arguments = validate_tool_call_arguments
end
Expand Down Expand Up @@ -83,6 +92,19 @@ def merge(other)

private

def validate_protocol_version!(protocol_version)
unless SUPPORTED_PROTOCOL_VERSIONS.include?(protocol_version)
message = "protocol_version must be #{SUPPORTED_PROTOCOL_VERSIONS[0...-1].join(", ")}, or #{SUPPORTED_PROTOCOL_VERSIONS[-1]}"
raise ArgumentError, message
end
end

def validate_value_of_validate_tool_call_arguments!(validate_tool_call_arguments)
unless validate_tool_call_arguments.is_a?(TrueClass) || validate_tool_call_arguments.is_a?(FalseClass)
raise ArgumentError, "validate_tool_call_arguments must be a boolean"
end
end

def default_exception_reporter
@default_exception_reporter ||= ->(exception, server_context) {}
end
Expand Down
18 changes: 14 additions & 4 deletions test/mcp/configuration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,21 @@ class ConfigurationTest < ActiveSupport::TestCase
assert_equal Configuration::DEFAULT_PROTOCOL_VERSION, config.protocol_version
end

test "allows setting a custom protocol version" do
test "raises ArgumentError when protocol_version is not a supported protocol version" do
config = Configuration.new
custom_version = "2025-03-27"
config.protocol_version = custom_version
assert_equal custom_version, config.protocol_version
exception = assert_raises(ArgumentError) do
custom_version = "2025-03-27"
config.protocol_version = custom_version
end
assert_equal("protocol_version must be 2025-06-18, 2025-03-26, or 2024-11-05", exception.message)
end

test "raises ArgumentError when protocol_version is not a boolean value" do
config = Configuration.new
exception = assert_raises(ArgumentError) do
config.validate_tool_call_arguments = "true"
end
assert_equal("validate_tool_call_arguments must be a boolean", exception.message)
end

test "merges protocol version from other configuration" do
Expand Down