Skip to content

Commit ab4075c

Browse files
committed
Fix RuboCop offenses detected with rubocop-shopify 3.0.0
## Motivation and Context This repository does not track Gemfile.lock, so `bundle install` resolves to the latest RuboCop gems. Upgrading rubocop-shopify from 2.x to 3.0.0 surfaces 8 new offenses because the shared config no longer disables `Style/MutableConstant`, newly disables `Lint/RescueException` and `Naming/AccessorMethodName`, and drops `AllowedPatterns` for `Naming/MethodName`. Upgrading rubocop itself to 1.88 surfaces one more offense from the new `Style/ReduceToHash` cop. This change fixes them as follows: - Freeze mutable constants. Regexp constants are also flagged because `TargetRubyVersion` is 2.7, where Regexp literals are not frozen. - Remove inline disable directives that became redundant. - Restore `AllowedPatterns: ['\Atest_']` for `Naming/MethodName` so `def test_` style test names can reference class names such as `NoMethodError`. - Swap out the frozen `SUPPORTED_STABLE_PROTOCOL_VERSIONS` constant in the protocol version fallback test since Mocha cannot stub methods on frozen objects. - Use `to_h` instead of `each_with_object` in `Server#index_resources_by_uri`. Ref: https://rubygems.org/gems/rubocop-shopify/versions/3.0.0 ## How Has This Been Tested? `bundle exec rake` passes: 1402 runs, 0 failures, 0 errors, and RuboCop reports no offenses. The conformance suite also passes against its baseline. ## Breaking Changes None. Freezing these constants only affects code that mutates them in place, which has never been supported.
1 parent e85f6d0 commit ab4075c

10 files changed

Lines changed: 18 additions & 12 deletions

File tree

.rubocop.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,7 @@ Lint/IncompatibleIoSelectWithFiberScheduler:
1616

1717
Minitest/LiteralAsActualArgument:
1818
Enabled: true
19+
20+
Naming/MethodName:
21+
AllowedPatterns:
22+
- '\Atest_'

lib/json_rpc_handler.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class ErrorCode
1616
PARSE_ERROR = -32700
1717
end
1818

19-
DEFAULT_ALLOWED_ID_CHARACTERS = /\A[a-zA-Z0-9_-]+\z/
19+
DEFAULT_ALLOWED_ID_CHARACTERS = /\A[a-zA-Z0-9_-]+\z/.freeze
2020

2121
# Sentinel return value from a handler. When a handler returns this,
2222
# `process_request` emits no JSON-RPC response for the request,

lib/mcp/client/oauth/discovery.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ module Discovery
3737
# Matches a single `key=value` pair inside an HTTP auth-scheme challenge.
3838
# `value` is either a quoted string (which can contain commas and spaces)
3939
# or a bare token, per RFC 7235.
40-
WWW_AUTH_PARAM_PATTERN = /\A([A-Za-z0-9_-]+)\s*=\s*(?:"((?:[^"\\]|\\.)*)"|([^\s,]+))/
40+
WWW_AUTH_PARAM_PATTERN = /\A([A-Za-z0-9_-]+)\s*=\s*(?:"((?:[^"\\]|\\.)*)"|([^\s,]+))/.freeze
4141

4242
class << self
4343
# Parses a `WWW-Authenticate` header and returns the parameters of

lib/mcp/configuration.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class Configuration
55
LATEST_STABLE_PROTOCOL_VERSION = "2025-11-25"
66
SUPPORTED_STABLE_PROTOCOL_VERSIONS = [
77
LATEST_STABLE_PROTOCOL_VERSION, "2025-06-18", "2025-03-26", "2024-11-05",
8-
]
8+
].freeze
99
DEFAULT_NEGOTIATED_PROTOCOL_VERSION = "2025-03-26"
1010

1111
attr_writer :exception_reporter, :around_request

lib/mcp/icon.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
module MCP
44
class Icon
5-
SUPPORTED_THEMES = ["light", "dark"]
5+
SUPPORTED_THEMES = ["light", "dark"].freeze
66

77
attr_reader :mime_type, :sizes, :src, :theme
88

lib/mcp/instrumentation.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ def instrument_call(method, server_context: {}, exception_already_reported: nil,
1414
rescue => e
1515
already_reported = begin
1616
!!exception_already_reported&.call(e)
17-
# rubocop:disable Lint/RescueException
1817
rescue Exception
19-
# rubocop:enable Lint/RescueException
2018
# The predicate is expected to be side-effect-free and return a boolean.
2119
# Any exception raised from it (including non-StandardError such as SystemExit)
2220
# must not shadow the original exception.

lib/mcp/resource_template.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class << self
88
# Applied after `Regexp.escape`, which turns `{` and `}` into `\{` and `\}`.
99
# Variable names are restricted to valid Regexp named-group names,
1010
# so RFC 6570 operator expressions (e.g. `{?query}`) stay literal and never match.
11-
VARIABLE_PATTERN = /\\\{([A-Za-z_]\w*)\\\}/
11+
VARIABLE_PATTERN = /\\\{([A-Za-z_]\w*)\\\}/.freeze
1212

1313
attr_reader :uri_template_value
1414
attr_reader :title_value

lib/mcp/server.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -963,9 +963,7 @@ def report_exception(exception, server_context = {})
963963
end
964964

965965
def index_resources_by_uri(resources)
966-
resources.each_with_object({}) do |resource, hash|
967-
hash[resource.uri] = resource
968-
end
966+
resources.to_h { |resource| [resource.uri, resource] }
969967
end
970968

971969
def error_tool_response(text)

test/mcp/server/transports/stdio_notification_integration_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def gets
2727
nil # Simulate end of input
2828
end
2929

30-
def set_encoding(encoding) # rubocop:disable Naming/AccessorMethodName
30+
def set_encoding(encoding)
3131
# Mock implementation
3232
end
3333

test/mcp/server/transports/streamable_http_transport_test.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1925,7 +1925,10 @@ def string
19251925
end
19261926

19271927
test "missing MCP-Protocol-Version header falls back to default for validation" do
1928-
MCP::Configuration::SUPPORTED_STABLE_PROTOCOL_VERSIONS.stubs(:include?).returns(false)
1928+
# The constant is frozen, so swap it out instead of stubbing `include?` on it.
1929+
original_versions = MCP::Configuration::SUPPORTED_STABLE_PROTOCOL_VERSIONS
1930+
MCP::Configuration.send(:remove_const, :SUPPORTED_STABLE_PROTOCOL_VERSIONS)
1931+
MCP::Configuration.const_set(:SUPPORTED_STABLE_PROTOCOL_VERSIONS, [].freeze)
19291932

19301933
request = Rack::Request.new(
19311934
"REQUEST_METHOD" => "POST",
@@ -1938,6 +1941,9 @@ def string
19381941

19391942
body = JSON.parse(response[2][0])
19401943
assert_includes body["error"]["message"], MCP::Configuration::DEFAULT_NEGOTIATED_PROTOCOL_VERSION
1944+
ensure
1945+
MCP::Configuration.send(:remove_const, :SUPPORTED_STABLE_PROTOCOL_VERSIONS)
1946+
MCP::Configuration.const_set(:SUPPORTED_STABLE_PROTOCOL_VERSIONS, original_versions)
19411947
end
19421948

19431949
test "POST request with empty MCP-Protocol-Version header returns 400" do

0 commit comments

Comments
 (0)