## Motivation and Context `MCP::Client::OAuth::Flow` takes an `http_client_factory:`, but `MCP::Client::HTTP` builds the flow itself and passes none, so an application using `oauth:` could not add middleware to the requests the flow makes: Protected Resource Metadata and authorization server metadata discovery, dynamic client registration, and every token request. The block that customizes the transport's Faraday connection does not help, because that connection is bound to the MCP server URL and carries the `headers:` meant for that server, and the authorization server is a different origin. The TypeScript SDK hands the transport's `fetch` to `auth()` and the Python SDK sends the provider's requests through the transport's httpx client, so both let an application instrument these requests; this SDK did not. (#558) Providers now take `http_client_customizer:`, a callable invoked with the Faraday connection the flow builds for its own requests, next to the other callables a provider already holds. It runs after the SDK's defaults (`Accept: application/json`, no redirect following) and between two middlewares of the SDK's own: `Flow::RequestedURLStamp`, registered before the customizer is invoked, records the URL the flow asked for on the request environment, and `Flow::RequestedOriginGuard`, registered last, sees what the customizer's middleware did to the request after that record. A request that would leave that origin, by following a redirect or rewriting the URL, or that arrives without the record, is refused with `Flow::DestinationMismatchError` before it reaches the adapter. The record lives on the environment rather than in `env.request.context`, which stays the application's to fill or replace. The error is an `ArgumentError` rather than an `AuthorizationError` so neither discovery nor the transport's refresh fallback can absorb a configuration mistake. The boundary resembles the one the Python SDK keeps for its own requests, which follows a redirect only within the origin; this flow follows none itself. `Flow.build_http_client` assembles that connection and `Flow.new(http_client_factory:)` keeps replacing it whole. Following done inside an adapter cannot be seen by any guard, so the documentation keeps that, `Accept-Encoding`, and `raise_error` off the list of things a customizer may add, and notes that a recording middleware sees client credentials and tokens. Fixes #558. ## How Has This Been Tested? New tests in `test/mcp/client/oauth/flow_test.rb`, `test/mcp/client/oauth/http_oauth_test.rb`, and the three provider test files. Against the library before this change, every one of them fails, on the unknown keyword or on the missing reader. ## Breaking Changes None. The customizer is opt-in, and the guard cannot fire on the SDK's own middleware-free connection.
100 lines
4.6 KiB
Ruby
100 lines
4.6 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`.
|
|
# - `token_request_params` - Hash of String keys and values added to the `jwt-bearer` token request, for parameters
|
|
# the authorization server requires beyond the grant itself. A key in `Flow::RESERVED_TOKEN_REQUEST_PARAMS` raises
|
|
# `Flow::InvalidTokenRequestParamsError`. The Hash is copied and frozen.
|
|
# See `StorageBackedProvider#token_request_params`.
|
|
# - `http_client_customizer` - Callable invoked with the `Faraday::Connection` the flow builds for its own requests;
|
|
# see `StorageBackedProvider#http_client_customizer`.
|
|
#
|
|
# 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,
|
|
token_request_params: nil,
|
|
http_client_customizer: 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
|
|
|
|
http_client_customizer = validated_http_client_customizer(http_client_customizer)
|
|
|
|
@assertion_provider = assertion_provider
|
|
@scope = scope
|
|
@storage = storage || InMemoryStorage.new
|
|
@authorization_request_validator = authorization_request_validator
|
|
@token_request_params = frozen_token_request_params(token_request_params)
|
|
@http_client_customizer = http_client_customizer
|
|
@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
|