Support Ruby 2.7 - Ruby 3.1

## Summary

Unlike applications, libraries provide value by remaining usable for a broad range of users,
even when that means supporting older Ruby versions beyond Ruby's own EOL timeline.

I maintain RuboCop, a Ruby linter. Because linters need to work across the ecosystem,
they often need to keep compatibility with older Ruby versions to some extent.

The trade-off is that the MCP Ruby SDK must remain compatible with Ruby 2.7.
However, given the ecosystem impact, supporting Ruby 2.7 is still worthwhile.

For example, bundled gems such as `csv` (Ruby >= 2.5), `bigdecimal` (Ruby >= 2.5), `json` (Ruby >= 2.7),
and `language_server-protocol` (Ruby >= 2.5) tend to keep compatibility with older Ruby versions.

Unlike dropping support, which imposes restrictions on users,
this change broadens compatibility and does not introduce breaking changes.

## Development Note

`rubocop-shopify` (2.18) supports Ruby 3.1+, and this PR does not change that.
Since RuboCop can configure the runtime Ruby version and the target Ruby version for analysis independently,
CI continues to run RuboCop on Ruby 4.0, while analyzing code with `TargetRubyVersion: 2.7`.

When setting `TargetRubyVersion: 2.7`, several RuboCop offenses were resolved.

Additionally, Sorbet-related libraries support Ruby 3.0+. Since these are not dependencies of the MCP Ruby SDK itself,
they are versioned and tested in the development Gemfile.
This commit is contained in:
Koichi ITO 2025-12-21 23:21:19 +09:00
commit aa8bbad491
13 changed files with 106 additions and 100 deletions

View file

@ -18,42 +18,42 @@ module MCP
rescue Faraday::BadRequestError => e
raise RequestHandlerError.new(
"The #{method} request is invalid",
{ method:, params: },
{ method: method, params: params },
error_type: :bad_request,
original_error: e,
)
rescue Faraday::UnauthorizedError => e
raise RequestHandlerError.new(
"You are unauthorized to make #{method} requests",
{ method:, params: },
{ method: method, params: params },
error_type: :unauthorized,
original_error: e,
)
rescue Faraday::ForbiddenError => e
raise RequestHandlerError.new(
"You are forbidden to make #{method} requests",
{ method:, params: },
{ method: method, params: params },
error_type: :forbidden,
original_error: e,
)
rescue Faraday::ResourceNotFound => e
raise RequestHandlerError.new(
"The #{method} request is not found",
{ method:, params: },
{ method: method, params: params },
error_type: :not_found,
original_error: e,
)
rescue Faraday::UnprocessableEntityError => e
raise RequestHandlerError.new(
"The #{method} request is unprocessable",
{ method:, params: },
{ method: method, params: params },
error_type: :unprocessable_entity,
original_error: e,
)
rescue Faraday::Error => e # Catch-all
raise RequestHandlerError.new(
"Internal error handling #{method} request",
{ method:, params: },
{ method: method, params: params },
error_type: :internal_error,
original_error: e,
)

View file

@ -85,10 +85,10 @@ module MCP
validate_tool_call_arguments = other.validate_tool_call_arguments
Configuration.new(
exception_reporter:,
instrumentation_callback:,
protocol_version:,
validate_tool_call_arguments:,
exception_reporter: exception_reporter,
instrumentation_callback: instrumentation_callback,
protocol_version: protocol_version,
validate_tool_call_arguments: validate_tool_call_arguments,
)
end

View file

@ -11,7 +11,7 @@ module MCP
end
def to_h
{ text:, annotations:, type: "text" }.compact
{ text: text, annotations: annotations, type: "text" }.compact
end
end
@ -25,7 +25,7 @@ module MCP
end
def to_h
{ data:, mime_type:, annotations:, type: "image" }.compact
{ data: data, mime_type: mime_type, annotations: annotations, type: "image" }.compact
end
end
end

View file

@ -6,7 +6,7 @@ module MCP
start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
begin
@instrumentation_data = {}
add_instrumentation_data(method:)
add_instrumentation_data(method: method)
result = yield block

View file

@ -96,7 +96,7 @@ module MCP
icons icons
arguments arguments
define_singleton_method(:template) do |args, server_context: nil|
instance_exec(args, server_context:, &block)
instance_exec(args, server_context: server_context, &block)
end
meta meta
end

View file

@ -11,7 +11,7 @@ module MCP
end
def to_h
{ role:, content: content.to_h }.compact
{ role: role, content: content.to_h }.compact
end
end
end

View file

@ -11,7 +11,7 @@ module MCP
end
def to_h
{ description:, messages: messages.map(&:to_h) }.compact
{ description: description, messages: messages.map(&:to_h) }.compact
end
end
end

View file

@ -11,7 +11,7 @@ module MCP
end
def to_h
{ uri:, mime_type: }.compact
{ uri: uri, mime_type: mime_type }.compact
end
end

View file

@ -10,7 +10,7 @@ module MCP
end
def to_h
{ resource: resource.to_h, annotations: }.compact
{ resource: resource.to_h, annotations: annotations }.compact
end
end
end

View file

@ -114,7 +114,7 @@ module MCP
end
def define_tool(name: nil, title: nil, description: nil, input_schema: nil, annotations: nil, meta: nil, &block)
tool = Tool.define(name:, title:, description:, input_schema:, annotations:, meta:, &block)
tool = Tool.define(name: name, title: title, description: description, input_schema: input_schema, annotations: annotations, meta: meta, &block)
tool_name = tool.name_value
@tool_names << tool_name
@ -124,7 +124,7 @@ module MCP
end
def define_prompt(name: nil, title: nil, description: nil, arguments: [], &block)
prompt = Prompt.define(name:, title:, description:, arguments:, &block)
prompt = Prompt.define(name: name, title: title, description: description, arguments: arguments, &block)
@prompts[prompt.name_value] = prompt
validate!
@ -289,11 +289,11 @@ module MCP
def server_info
@server_info ||= {
description:,
icons:,
name:,
title:,
version:,
description: description,
icons: icons,
name: name,
title: title,
version: version,
websiteUrl: website_url,
}.compact
end
@ -316,13 +316,13 @@ module MCP
tool = tools[tool_name]
unless tool
add_instrumentation_data(tool_name:, error: :tool_not_found)
add_instrumentation_data(tool_name: tool_name, error: :tool_not_found)
return error_tool_response("Tool not found: #{tool_name}")
end
arguments = request[:arguments] || {}
add_instrumentation_data(tool_name:)
add_instrumentation_data(tool_name: tool_name)
if tool.input_schema&.missing_required_arguments?(arguments)
add_instrumentation_data(error: :missing_required_arguments)
@ -360,7 +360,7 @@ module MCP
raise RequestHandlerError.new("Prompt not found #{prompt_name}", request, error_type: :prompt_not_found)
end
add_instrumentation_data(prompt_name:)
add_instrumentation_data(prompt_name: prompt_name)
prompt_args = request[:arguments]
prompt.validate_arguments!(prompt_args)

View file

@ -42,7 +42,7 @@ module MCP
notification = {
jsonrpc: "2.0",
method:,
method: method,
}
notification[:params] = params if params

View file

@ -19,7 +19,7 @@ module MCP
idempotentHint: idempotent_hint,
openWorldHint: open_world_hint,
readOnlyHint: read_only_hint,
title:,
title: title,
}.compact
end
end

View file

@ -23,7 +23,7 @@ module MCP
end
def to_h
{ content:, isError: error?, structuredContent: @structured_content }.compact
{ content: content, isError: error?, structuredContent: @structured_content }.compact
end
end
end