ExMCP.Authorization.Interceptor (ex_mcp v0.10.0)

View Source

Request interceptor that automatically adds authorization headers.

This module provides middleware functionality to ensure all MCP requests include proper authorization headers when tokens are available.

Summary

Functions

Adds authorization headers to a request if a token is available.

Processes a response and handles authorization errors.

Creates an authorization-aware request function.

Functions

add_auth_headers(request, opts)

@spec add_auth_headers(
  map() | keyword(),
  keyword()
) :: {:ok, map() | keyword()} | {:error, any()}

Adds authorization headers to a request if a token is available.

Options:

  • :token_manager - PID or name of TokenManager process
  • :token - Static token to use (alternative to token_manager)
  • :auth_type - Authorization type (default: "Bearer")

handle_response(response, state)

@spec handle_response({:ok, any()} | {:error, any()}, map()) ::
  {:ok, any()} | {:retry, map()} | {:error, any()}

Processes a response and handles authorization errors.

Returns:

  • {:ok, response} - Response is fine, continue processing
  • {:retry, auth_params} - Need to retry with authentication
  • {:error, reason} - Unrecoverable error

wrap_request_fn(request_fn, opts)

@spec wrap_request_fn(
  (map() -> {:ok, any()} | {:error, any()}),
  keyword()
) :: (map() -> {:ok, any()} | {:error, any()})

Creates an authorization-aware request function.

This wraps a request function to automatically add auth headers and handle authorization errors with retry logic.