## Motivation and Context An MCP server chooses the authorization server its users sign in to. It names one in `authorization_servers` in its Protected Resource Metadata, and states the scopes it wants in `scopes_supported` or in the `WWW-Authenticate` challenge. Nothing binds either choice to the server's own identity. No origin rule can settle this, because an authorization server is legitimately a different origin from the resource it protects. Validating that a token was issued for the intended audience is a responsibility the specification places on MCP servers rather than on clients, and there is no portable way for a client to perform it either: access tokens are frequently opaque, and an authorization server is not required to honor the `resource` parameter or to report an audience in the token response. Two things follow from that, and they are separate. The first is a judgement, and it belongs to whoever embeds this SDK, because an application often knows which providers its users deal with. `authorization_request_validator` is handed an `AuthorizationRequest` describing the authorization server, the scopes, the MCP server, and the RFC 8707 resource, and can refuse. It is one object rather than keyword arguments so that later revisions of the specification can add to it without changing the shape implementers wrote. It runs on all three grants, and on the authorization-code grant it runs before dynamic client registration, which is why scope resolution moved above `ensure_client_registered`: a refusal must not leave this client registered at an authorization server the host just rejected. The second is not a judgement and is enforced here rather than asked about. `refresh!` rediscovers the authorization server on every call, so the server named for a session can differ from the one the stored tokens came from. SEP-2352 already binds client credentials to their issuer, but that check passes when the issuer was never recorded and is deliberately skipped for a Client ID Metadata Document URL, which is portable across authorization servers. The issuer that minted the tokens is now recorded with them, and `refresh!` refuses to present them to a different one. The transport answers that refusal by running a full authorization, which brings the new authorization server to the validator above. Tokens stored before this carry no issuer and keep refreshing. The hook decides whether to proceed, not what to ask for. The scopes reach the authorization server unchanged either way, because the specification requires a client to treat the scopes in the challenge as authoritative for the operation, and narrowing them silently would break that. ## How Has This Been Tested? New tests in `test/mcp/client/oauth/flow_test.rb` assert what the validator receives, that a refusal raises before any registration request is sent and before the redirect handler runs, and that the `client_credentials` and `jwt-bearer` grants consult it too. Others cover the issuer recorded on authorization, the refusal to refresh against a different one, and that tokens stored without an issuer still refresh. A test in `test/mcp/client/oauth/http_oauth_test.rb` drives the two halves together: a stale issuer makes the refresh fail, the transport reauthorizes, and the validator sees the newly named authorization server while no refresh token is ever posted to it. Each of them fails with its half of this change removed. `bundle exec rake` (tests, RuboCop, and conformance baseline) passes. ## Breaking Changes Refreshing now fails when the authorization server named for a session differs from the one that issued the stored tokens, and the caller reauthorizes instead. Tokens stored before this change carry no recorded issuer and are unaffected until their next authorization. The validator itself is additive: it defaults to `nil`, and a provider that does not offer it authorizes as before.
81 lines
3.7 KiB
Ruby
81 lines
3.7 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
module MCP
|
|
class Client
|
|
module OAuth
|
|
# OAuth client configuration for the MCP Enterprise Managed Authorization extension (SEP-990, "Cross-App Access"):
|
|
# the client obtains an Identity Assertion Authorization Grant (ID-JAG) from an enterprise identity provider and
|
|
# presents it to the MCP authorization server with the RFC 7523 `jwt-bearer` grant, authenticating with
|
|
# `client_secret_basic`. Handed to `MCP::Client::HTTP` via the `oauth:` keyword, the same as `Provider`.
|
|
#
|
|
# Mirrors `CrossAppAccessProvider` in the TypeScript SDK: the assertion is supplied by a callable so it can come
|
|
# from `IDJAGTokenExchange` (the common case), an enterprise secret store, or a test double.
|
|
#
|
|
# Required keyword arguments:
|
|
#
|
|
# - `client_id` - String identifying the pre-registered confidential client at the MCP authorization server.
|
|
# - `client_secret` - String shared secret for `client_secret_basic`.
|
|
# - `assertion_provider` - Callable invoked as `call(audience:, resource:)`, returning the ID-JAG assertion to
|
|
# present. `audience` is the authorization server's issuer identifier and `resource` is the canonical MCP server URL;
|
|
# pass both through to `IDJAGTokenExchange.request` when exchanging an IdP ID token.
|
|
#
|
|
# Optional keyword arguments:
|
|
#
|
|
# - `scope` - String of space-separated scopes to request when the server's `WWW-Authenticate` and
|
|
# the Protected Resource Metadata do not specify one.
|
|
# - `storage` - Object responding to `tokens`, `save_tokens(tokens)`, `client_information`, and `save_client_information(info)`.
|
|
# Defaults to an `InMemoryStorage`.
|
|
#
|
|
# https://github.com/modelcontextprotocol/modelcontextprotocol/issues/990
|
|
class CrossAppAccessProvider
|
|
include StorageBackedProvider
|
|
|
|
# Raised when the provider is constructed without the pieces the `jwt-bearer` grant needs.
|
|
class InvalidConfigurationError < ArgumentError; end
|
|
|
|
attr_reader :scope, :storage
|
|
|
|
def initialize(client_id:, client_secret:, assertion_provider:, scope: nil, storage: nil, authorization_request_validator: nil)
|
|
if blank?(client_id)
|
|
raise InvalidConfigurationError, "client_id is required for the jwt-bearer grant."
|
|
end
|
|
|
|
if blank?(client_secret)
|
|
raise InvalidConfigurationError, "client_secret is required: SEP-990 authenticates the jwt-bearer grant with client_secret_basic."
|
|
end
|
|
|
|
unless assertion_provider.respond_to?(:call)
|
|
raise InvalidConfigurationError, "assertion_provider must be callable as `call(audience:, resource:)` and return the ID-JAG assertion."
|
|
end
|
|
|
|
@assertion_provider = assertion_provider
|
|
@scope = scope
|
|
@storage = storage || InMemoryStorage.new
|
|
@authorization_request_validator = authorization_request_validator
|
|
@storage.save_client_information(
|
|
"client_id" => client_id,
|
|
"client_secret" => client_secret,
|
|
"token_endpoint_auth_method" => "client_secret_basic",
|
|
)
|
|
end
|
|
|
|
# See `Provider#authorization_flow`.
|
|
def authorization_flow
|
|
:jwt_bearer
|
|
end
|
|
|
|
# Returns the ID-JAG assertion to present at the MCP authorization server. Called by `Flow#run_jwt_bearer!` with the audience
|
|
# and resource resolved during discovery.
|
|
def jwt_bearer_assertion(audience:, resource:)
|
|
@assertion_provider.call(audience: audience, resource: resource)
|
|
end
|
|
|
|
private
|
|
|
|
def blank?(value)
|
|
value.nil? || (value.is_a?(String) && value.strip.empty?)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|