Skip to content
Open
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
15 changes: 15 additions & 0 deletions lib/mcp/client/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,21 @@ def close
end
end

# `inspect` is what error reporters and consoles print an object with, and the default one walks every instance variable:
# the static `headers:` (often an API key), the Faraday connection that holds them as well, and the session id.
# It shows the header names only, the URL without its userinfo and query, whether a session is open, and
# the class of the `oauth:` provider: what a provider or its storage prints is theirs to decide, and may hold a credential.
def inspect
safe_url = begin
MCP::Client::OAuth::Discovery.canonicalize_origin_and_path(@url)
rescue StandardError
"[unparseable]"
end

"#<#{self.class.name} url=#{safe_url.inspect} headers=#{(@headers || {}).keys.inspect} " \
"session_id=#{@session_id.nil? ? "nil" : "[present]"} oauth=#{@oauth.nil? ? "nil" : @oauth.class.name}>"
end

private

attr_reader :headers
Expand Down
7 changes: 7 additions & 0 deletions lib/mcp/client/oauth/client_credentials_provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,13 @@ def client_assertion(audience:)
)
end

# `inspect` is what error reporters and consoles print an object with, and the default one would print
# the `private_key` given as a PEM String in full, so it shows only whether a key is held, and the class of
# the storage: what a custom storage prints is its own to decide.
def inspect
"#<#{self.class.name} client_id=#{@client_id.inspect} private_key=#{@private_key.nil? ? "nil" : "[present]"} storage=#{@storage.class.name}>"
end

private

def validate_private_key_jwt_arguments!(client_secret:, private_key:, signing_algorithm:)
Expand Down
8 changes: 8 additions & 0 deletions lib/mcp/client/stdio.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ def initialize(command:, args: [], env: nil, inherit_env: false, read_timeout: n
@write_mutex = Mutex.new
end

# The default `inspect` would print the `env:` given to the server process, which is where its API keys usually go,
# and the arguments, which can hold one as well. It shows the command, the number of arguments and the names
# of the variables only.
def inspect
"#<#{self.class.name} command=#{@command.inspect} args=#{Array(@args).size} env=#{(@env || {}).keys.inspect} " \
"started=#{@started}>"
end

# Performs the MCP `initialize` handshake: sends an `initialize` request
# followed by the required `notifications/initialized` notification. The
# server's `InitializeResult` (protocol version, capabilities, server
Expand Down
6 changes: 6 additions & 0 deletions lib/mcp/server/oauth/introspection_verifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ def verify(token)
access_token
end

# The default `inspect` would print the `client_secret` this verifier authenticates to the introspection endpoint with.
def inspect
"#<#{self.class.name} introspection_endpoint=#{@introspection_endpoint.inspect} client_id=#{@client_id.inspect} " \
"client_secret=#{@client_secret.nil? ? "nil" : "[present]"} client_auth_method=#{@client_auth_method.inspect}>"
end

private

def introspect(token)
Expand Down
6 changes: 6 additions & 0 deletions lib/mcp/server/oauth/jwt_verifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@ def verify(token)
raise InvalidTokenError, "Invalid token"
end

# The default `inspect` would print the `key:`, which for HMAC algorithms is the shared secret itself.
def inspect
"#<#{self.class.name} issuer=#{@issuer.inspect} audience=#{@audience.inspect} jwks_uri=#{@jwks_uri.inspect} " \
"key=#{@key.nil? ? "nil" : "[present]"} algorithms=#{@algorithms.inspect}>"
end

private

def decode(token)
Expand Down
5 changes: 5 additions & 0 deletions lib/mcp/server/request_state_security.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ def initialize(key:, ttl: DEFAULT_TTL, audience: nil)
@audience = audience
end

# The default `inspect` would print the `key:`, with which anyone can seal a state this server accepts.
def inspect
"#<#{self.class.name} ttl=#{@ttl.inspect} audience=#{@audience.inspect}>"
end

# Seals a plaintext state into an opaque token bound to the originating request.
def seal(state, method:, target:, arguments_digest:)
claims = {
Expand Down
25 changes: 25 additions & 0 deletions test/mcp/client/http_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,31 @@ def test_raises_load_error_when_event_stream_parser_not_available
assert_includes(error.message, "Add it to your Gemfile: gem 'event_stream_parser', '>= 1.0'")
end

def test_inspect_shows_no_header_value_userinfo_query_or_session_id
transport = HTTP.new(url: "https://user:pass@example.com/mcp?key=query-secret", headers: { "X-Api-Key" => "header-secret" })
transport.instance_variable_set(:@session_id, "session-secret")

# The connection holds the headers too, and the default `inspect` would walk into it.
transport.send(:client)

[transport.inspect, MCP::Client.new(transport: transport).inspect].each do |text|
["header-secret", "user:pass", "query-secret", "session-secret"].each { |secret| refute_includes(text, secret) }
end
assert_equal('#<MCP::Client::HTTP url="https://example.com/mcp" headers=["X-Api-Key"] session_id=[present] oauth=nil>', transport.inspect)
end

def test_inspect_shows_the_class_of_the_oauth_provider_and_nothing_it_holds
storage = Object.new
storage.define_singleton_method(:inspect) { "storage-secret" }
[:tokens, :client_information].each { |name| storage.define_singleton_method(name) { nil } }
[:save_tokens, :save_client_information].each { |name| storage.define_singleton_method(name) { |_value| nil } }
provider = MCP::Client::OAuth::ClientCredentialsProvider.new(client_id: "cc-client", client_secret: "s3cret", storage: storage)
transport = HTTP.new(url: "https://example.com/mcp", oauth: provider)

assert_includes(transport.inspect, "oauth=MCP::Client::OAuth::ClientCredentialsProvider>")
[transport.inspect, provider.inspect].each { |text| refute_includes(text, "storage-secret") }
end

def test_headers_are_added_to_the_request
headers = { "Authorization" => "Bearer token" }
client = HTTP.new(url: url, headers: headers)
Expand Down
13 changes: 13 additions & 0 deletions test/mcp/client/oauth/client_credentials_provider_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,19 @@ def private_key_jwt_provider(key: generate_es256_key)
)
end

def test_inspect_does_not_print_the_private_key
pem = generate_es256_key.to_pem
provider = ClientCredentialsProvider.new(
client_id: "cc-client",
token_endpoint_auth_method: "private_key_jwt",
private_key: pem,
signing_algorithm: "ES256",
)

refute_includes(provider.inspect, "PRIVATE KEY")
assert_includes(provider.inspect, 'client_id="cc-client" private_key=[present]')
end

def test_initialize_accepts_private_key_jwt
provider = private_key_jwt_provider

Expand Down
9 changes: 9 additions & 0 deletions test/mcp/client/stdio_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ def test_send_request_raises_when_connect_not_called
assert_equal("MCP::Client#connect must be called before sending requests.", error.message)
end

def test_inspect_shows_no_argument_or_environment_value
transport = Stdio.new(command: "ruby", args: ["server.rb", "--token=arg-secret"], env: { "API_KEY" => "env-secret" })

[transport.inspect, MCP::Client.new(transport: transport).inspect].each do |text|
["arg-secret", "env-secret"].each { |secret| refute_includes(text, secret) }
end
assert_equal('#<MCP::Client::Stdio command="ruby" args=2 env=["API_KEY"] started=false>', transport.inspect)
end

def test_send_request_starts_process_and_returns_response
stdin_read, stdin_write = IO.pipe
stdout_read, stdout_write = IO.pipe
Expand Down
5 changes: 5 additions & 0 deletions test/mcp/server/oauth/introspection_verifier_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,11 @@ def test_requires_client_id_unless_none
assert_includes(error.message, "client_id is required")
end

def test_inspect_does_not_print_the_client_secret
refute_includes(verifier.inspect, "rs-secret")
assert_includes(verifier.inspect, 'client_id="rs-client" client_secret=[present]')
end

private

def verifier(client_auth_method: :client_secret_basic, client_id: "rs-client", client_secret: "rs-secret", resource_metadata: METADATA)
Expand Down
8 changes: 8 additions & 0 deletions test/mcp/server/oauth/jwt_verifier_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,14 @@ def test_stale_jwks_is_not_served_beyond_the_stale_bound
assert_equal("JWKS endpoint responded with status 500", error.message)
end

def test_inspect_does_not_print_an_hmac_key
secret = "hmac-shared-secret-of-at-least-32-bytes"
verifier = JWTVerifier.new(resource_metadata: METADATA, key: secret, algorithms: ["HS256"])

refute_includes(verifier.inspect, secret)
assert_includes(verifier.inspect, 'key=[present] algorithms=["HS256"]')
end

private

def claims
Expand Down
4 changes: 4 additions & 0 deletions test/mcp/server/request_state_security_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ class RequestStateSecurityTest < ActiveSupport::TestCase
assert_raises(ArgumentError) { RequestStateSecurity.new(key: KEY, ttl: 0) }
end

test "does not print its key in inspect" do
refute_includes(RequestStateSecurity.new(key: KEY).inspect, KEY)
end

test "rejects a ttl that is not finite" do
assert_raises(ArgumentError) { RequestStateSecurity.new(key: KEY, ttl: Float::INFINITY) }
assert_raises(ArgumentError) { RequestStateSecurity.new(key: KEY, ttl: Float::NAN) }
Expand Down