McpHouse.Client.OAuth (mcp_house v0.1.0)

Copy Markdown View Source

OAuth discovery and authorization-code flow for an HTTP MCP resource.

Call discover/2 with the MCP endpoint and a trusted issuer. Use begin_authorization/2 to build the browser URL, keep the returned transaction for the callback, and pass the callback parameters to exchange_code/2. The host supplies the registered client ID and redirect URI, presents the consent flow, and owns token storage. Pass the resulting access token to McpHouse.Client.connect/2 as :bearer_token.

Summary

Functions

Builds a browser authorization URL and a transaction for the callback.

Discovers metadata for a protected MCP URL and a caller-selected issuer.

Exchanges an authorization code after validating the browser callback.

Requests a new access token from the discovered token endpoint.

Functions

begin_authorization(oauth, opts)

Builds a browser authorization URL and a transaction for the callback.

Pass a nonempty :client_id and the registered :redirect_uri. If :scope is omitted, the resource's discovered scope is used. Returns {:ok, url, transaction}. Redirect the user to the URL and keep the transaction on the host.

The example supplies metadata directly so it can run locally.

iex> oauth = %McpHouse.Client.OAuth{
...>   resource: "https://mcp.example/mcp",
...>   authorize: URI.new!("https://auth.example/authorize")
...> }
iex> {:ok, url, transaction} = McpHouse.Client.OAuth.begin_authorization(oauth,
...>   client_id: "example", redirect_uri: "https://app.example/callback")
iex> params = url |> URI.parse() |> Map.fetch!(:query) |> URI.decode_query()
iex> {params["client_id"], params["code_challenge_method"], params["resource"]}
{"example", "S256", "https://mcp.example/mcp"}
iex> McpHouse.Client.OAuth.exchange_code(transaction, %{"code" => "example"})
{:error, :invalid_oauth_callback}

discover(resource, opts)

Discovers metadata for a protected MCP URL and a caller-selected issuer.

Supply the trusted issuer URL in :issuer. Discovery checks that the protected resource lists that issuer and that its metadata supports PKCE with S256. Returns {:ok, oauth} or {:error, reason}.

Pass the same :network_policy used by McpHouse.Client.connect/2 to validate destinations for discovery, token exchange, and refresh. Supply :limits to bound setup and request and response sizes.

iex> McpHouse.Client.OAuth.discover("https://mcp.example/mcp", issuer: "http://auth.example")
{:error, :invalid_oauth_url}

exchange_code(transaction, callback)

Exchanges an authorization code after validating the browser callback.

Pass the transaction returned by begin_authorization/2 and a map with string keys "state" and "code". Include the actual "iss" callback parameter when supplied. An issuer that advertises issuer identification requires this parameter. Returns {:ok, tokens} with an "access_token" or {:error, reason}.

refresh(oauth, client_id, refresh_token)

Requests a new access token from the discovered token endpoint.

Pass the same OAuth client ID and the refresh token stored by the host. Returns {:ok, tokens} on success. Use a replacement refresh token when the issuer includes one in the returned map.