Class: MCP::Server::OAuth::ProtectedResourceMetadata

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

Overview

OAuth 2.0 Protected Resource Metadata (RFC 9728) for an MCP server. The MCP authorization spec requires servers to publish this document so clients can discover which authorization servers can issue tokens for the resource.

  • resource - the canonical resource identifier: the URL clients connect to, without a fragment. Clients send this back to the authorization server as the RFC 8707 resource parameter.
  • authorization_servers - issuer URLs of the authorization servers that protect this resource (at least one).
  • scopes_supported - an Array of the scope strings clients may request (also the default scope hint in WWW-Authenticate challenges); offline_access is dropped, and a result with nothing left omits the member.
  • bearer_methods_supported - defaults to ["header"]: the SDK accepts bearer tokens only in the Authorization header, never in a query string.
  • extra - additional RFC 9728 fields (e.g. jwks_uri, resource_signing_alg_values_supported, resource_policy_uri) merged into the document as given; the members above cannot be overridden through it.

https://www.rfc-editor.org/rfc/rfc9728 https://modelcontextprotocol.io/specification/2026-07-28/basic/authorization/authorization-server-discovery

Constant Summary collapse

WELL_KNOWN_PATH_PREFIX =
"/.well-known/oauth-protected-resource"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource:, authorization_servers:, scopes_supported: nil, resource_name: nil, resource_documentation: nil, bearer_methods_supported: DEFAULT_BEARER_METHODS_SUPPORTED, extra: {}) ⇒ ProtectedResourceMetadata

Returns a new instance of ProtectedResourceMetadata.

Raises:

  • (ArgumentError)


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/mcp/server/oauth/protected_resource_metadata.rb', line 42

def initialize(resource:, authorization_servers:, scopes_supported: nil, resource_name: nil, resource_documentation: nil, bearer_methods_supported: DEFAULT_BEARER_METHODS_SUPPORTED, extra: {})
  @resource_uri = parse_resource_uri(resource)
  @resource = resource

  servers = Array(authorization_servers)
  raise ArgumentError, "authorization_servers must contain at least one issuer URL" if servers.empty?

  servers.each do |server|
    next if secure_identifier?(server)

    raise ArgumentError, "authorization_servers must use https (http is allowed only on loopback or a name under localhost): #{server.inspect}"
  end

  overridden = extra.keys.map(&:to_s) & VALIDATED_MEMBERS
  unless overridden.empty?
    raise ArgumentError, "extra must not override #{overridden.join(", ")}; pass them as keyword arguments so they are validated"
  end

  @authorization_servers = servers
  @scopes_supported = advertisable_scopes(scopes_supported)
  @resource_name = resource_name
  @resource_documentation = resource_documentation
  @bearer_methods_supported = bearer_methods_supported
  @extra = extra
end

Instance Attribute Details

#authorization_servers ⇒ Object (readonly)

Returns the value of attribute authorization_servers.



40
41
42
# File 'lib/mcp/server/oauth/protected_resource_metadata.rb', line 40

def authorization_servers
  @authorization_servers
end

#bearer_methods_supported ⇒ Object (readonly)

Returns the value of attribute bearer_methods_supported.



40
41
42
# File 'lib/mcp/server/oauth/protected_resource_metadata.rb', line 40

def bearer_methods_supported
  @bearer_methods_supported
end

#extra ⇒ Object (readonly)

Returns the value of attribute extra.



40
41
42
# File 'lib/mcp/server/oauth/protected_resource_metadata.rb', line 40

def extra
  @extra
end

#resource ⇒ Object (readonly)

Returns the value of attribute resource.



40
41
42
# File 'lib/mcp/server/oauth/protected_resource_metadata.rb', line 40

def resource
  @resource
end

#resource_documentation ⇒ Object (readonly)

Returns the value of attribute resource_documentation.



40
41
42
# File 'lib/mcp/server/oauth/protected_resource_metadata.rb', line 40

def resource_documentation
  @resource_documentation
end

#resource_name ⇒ Object (readonly)

Returns the value of attribute resource_name.



40
41
42
# File 'lib/mcp/server/oauth/protected_resource_metadata.rb', line 40

def resource_name
  @resource_name
end

#scopes_supported ⇒ Object (readonly)

Returns the value of attribute scopes_supported.



40
41
42
# File 'lib/mcp/server/oauth/protected_resource_metadata.rb', line 40

def scopes_supported
  @scopes_supported
end

Instance Method Details

#to_h ⇒ Object



68
69
70
71
72
73
74
75
76
77
# File 'lib/mcp/server/oauth/protected_resource_metadata.rb', line 68

def to_h
  {
    resource: resource,
    authorization_servers: authorization_servers,
    scopes_supported: scopes_supported,
    resource_name: resource_name,
    resource_documentation: resource_documentation,
    bearer_methods_supported: bearer_methods_supported,
  }.compact.merge(extra)
end

#to_json(*args) ⇒ Object



79
80
81
# File 'lib/mcp/server/oauth/protected_resource_metadata.rb', line 79

def to_json(*args)
  to_h.to_json(*args)
end

#well_known_path ⇒ Object

The path-inserted well-known path per RFC 9728 Section 3: a resource of https://example.com/mcp is described at /.well-known/oauth-protected-resource/mcp, and a resource at the origin root is described at /.well-known/oauth-protected-resource. The path math intentionally matches what MCP::Client::OAuth::Discovery.protected_resource_metadata_urls probes.



86
87
88
89
90
91
# File 'lib/mcp/server/oauth/protected_resource_metadata.rb', line 86

def well_known_path
  path = @resource_uri.path
  path = "" if path == "/"

  "#{WELL_KNOWN_PATH_PREFIX}#{path}"
end

#well_known_url ⇒ Object

The absolute URL of the metadata document. Pass this to Middleware (or Challenge) as resource_metadata_url so 401/403 challenges point clients at the document.



95
96
97
98
99
# File 'lib/mcp/server/oauth/protected_resource_metadata.rb', line 95

def well_known_url
  port = @resource_uri.port && @resource_uri.port != @resource_uri.default_port ? ":#{@resource_uri.port}" : ""

  "#{@resource_uri.scheme}://#{@resource_uri.host}#{port}#{well_known_path}"
end