ExMCP.Authorization.ProtectedResourceMetadata (ex_mcp v0.9.2)

View Source

OAuth 2.0 Protected Resource Metadata Discovery (RFC 9728 - Draft).

This module implements the discovery mechanism for protected resources to advertise their authorization server relationships. This allows MCP servers to indicate which authorization servers protect their resources.

Example

# Discover authorization servers for a protected resource
{:ok, metadata} = ProtectedResourceMetadata.discover("https://api.example.com/mcp")

# Use discovered authorization server
[auth_server | _] = metadata.authorization_servers
{:ok, auth_metadata} = Authorization.discover_server_metadata(auth_server.issuer)

Summary

Functions

Discovers protected resource metadata from the resource URL.

Parses WWW-Authenticate header for authorization information.

Types

authorization_server()

@type authorization_server() :: %{
  issuer: String.t(),
  metadata_endpoint: String.t() | nil,
  scopes_supported: [String.t()] | nil,
  audience: String.t() | [String.t()] | nil
}

metadata()

@type metadata() :: %{authorization_servers: [authorization_server()]}

www_authenticate_info()

@type www_authenticate_info() :: %{
  realm: String.t() | nil,
  as_uri: String.t() | nil,
  resource_uri: String.t() | nil,
  error: String.t() | nil,
  error_description: String.t() | nil
}

Functions

discover(resource_url)

@spec discover(String.t()) :: {:ok, metadata()} | {:error, term()}

Discovers protected resource metadata from the resource URL.

Makes a request to /.well-known/oauth-protected-resource to discover which authorization servers protect this resource.

parse_www_authenticate(header)

@spec parse_www_authenticate(String.t()) ::
  {:ok, www_authenticate_info()} | {:error, term()}

Parses WWW-Authenticate header for authorization information.

Extracts Bearer authentication parameters including realm, as_uri, resource_uri, and error information.