Validate protocol_version value

## Motivation and Context

Currently, MCP supports the following three protocol versions:
"2025-06-18", "2025-03-26", and "2024-11-05".
https://modelcontextprotocol.io/introduction

As shown in the changes to `test/mcp/configuration_test.rb`,
the `protocol_version` value is not currently being validated,
which allows unexpected `protocol_version` values to be accepted.

This PR ensures that only supported protocol versions are accepted.

## How Has This Been Tested?

Existing tests have been updated and new tests have been added.

## Breaking Changes

None. If it becomes an issue, it means a value that was not originally intended has been used.

## Additional context

While one possible approach might be to fall back to a default protocol version
when an invalid `protocol_version` value is provided,
this PR opts to reject invalid `protocol_version` values explicitly.
The reasoning is that encouraging users to provide intentional,
valid input is preferable to silently falling back to a default,
which could lead to unexpected behavior.
This commit is contained in:
Koichi ITO 2025-07-16 01:12:52 +09:00
commit f13522d416

View file

@ -3,6 +3,7 @@
module MCP
class Configuration
DEFAULT_PROTOCOL_VERSION = "2024-11-05"
SUPPORTED_PROTOCOL_VERSIONS = ["2025-06-18", "2025-03-26", DEFAULT_PROTOCOL_VERSION]
attr_writer :exception_reporter, :instrumentation_callback, :protocol_version, :validate_tool_call_arguments
@ -11,6 +12,10 @@ module MCP
@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"
end