Class: MCP::Server::OAuth::Authenticator

Inherits:
Object
  • Object
show all
Defined in:
lib/mcp/server/oauth/authenticator.rb

Overview

The verification core shared by Middleware and the streamable HTTP transport's built-in enforcement: extracts the bearer token from a Rack env, runs it through the verifier, checks scopes, and maps each failure to the RFC 6750 challenge response it deserves.

  • token_verifier - anything responding to verify(token); see TokenVerifier for the contract. Built-ins: JWTVerifier, IntrospectionVerifier.
  • required_scopes - scopes the token must include, all of them; a shortfall produces 403 insufficient_scope (step-up).
  • resource_metadata - a ProtectedResourceMetadata; its well-known URL and scopes_supported feed the challenges. Pass resource_metadata_url: instead to point at a document served elsewhere.
  • scope_matcher - optional callable (required_scope, granted_scopes) -> Boolean for deployments whose scopes form a hierarchy; the MCP authorization specification requires a broader scope to satisfy the narrower scopes it implies, which exact membership (the default) cannot express. The matcher also rides on the returned AccessToken, so require_scopes! and scope? inside handlers apply the same semantics.

Constant Summary collapse

MAX_TOKEN_BYTES =

Upper bound on the presented token, applied before any verification so oversized garbage neither reaches cryptographic parsing nor gets forwarded to an introspection endpoint. Real-world access tokens stay far below this; web servers cap the whole header section around 16 KiB.

8192

Instance Method Summary collapse

Constructor Details

#initialize(token_verifier:, required_scopes: [], resource_metadata: nil, resource_metadata_url: nil, scope_matcher: nil) ⇒ Authenticator

Returns a new instance of Authenticator.



36
37
38
39
40
41
42
# File 'lib/mcp/server/oauth/authenticator.rb', line 36

def initialize(token_verifier:, required_scopes: [], resource_metadata: nil, resource_metadata_url: nil, scope_matcher: nil)
  @verifier = token_verifier
  @required_scopes = Array(required_scopes)
  @resource_metadata = 
  @resource_metadata_url =  || &.well_known_url
  @scope_matcher = scope_matcher
end

Instance Method Details

#authenticate(env) ⇒ AccessToken

Parameters:

  • env (Hash) —

    the Rack env

Returns:

Raises:



47
48
49
50
51
52
53
54
55
56
# File 'lib/mcp/server/oauth/authenticator.rb', line 47

def authenticate(env)
  token = extract_bearer_token(env)

  access_token = @verifier.verify(token)
  raise InvalidTokenError if access_token.nil?

  access_token = attach_scope_matcher(access_token)
  check_scopes!(access_token)
  access_token
end

#challenge_response(error) ⇒ Object

Maps an OAuth::Error raised by authenticate (or by a handler's require_scopes!) to the Rack response carrying the matching challenge. An Error outside the four built-in classes, such as one a custom verifier defines, is a token rejection and answers 401 like an invalid token; anything else is a programming error.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/mcp/server/oauth/authenticator.rb', line 61

def challenge_response(error)
  case error
  when MissingTokenError
    Challenge.missing_token_response(scope: scope_hint, resource_metadata: @resource_metadata_url)
  when InvalidRequestError
    Challenge.invalid_request_response(error_description: error.message, resource_metadata: @resource_metadata_url)
  when InsufficientScopeError
    Challenge.insufficient_scope_response(
      error_description: error.message,
      scope: scope_hint(error.required_scopes),
      resource_metadata: @resource_metadata_url,
    )
  when Error
    Challenge.invalid_token_response(
      error_description: error.message,
      scope: scope_hint,
      resource_metadata: @resource_metadata_url,
    )
  else
    raise ArgumentError, "unexpected error class: #{error.class}"
  end
end