mcp_agent.config.yaml, mcp_agent.secrets.yaml, or environment variables. This page shows the common patterns and points to runnable examples.
Quick reference
- API keys / custom headers – set
headerson the server definition and load secrets frommcp_agent.secrets.yamlor environment variables. - OAuth (interactive loopback) – use the same configuration as
examples/basic/oauth_basic_agent; mcp-agent opens a browser, captures the callback, and stores tokens for reuse. - OAuth (authorization-code with server interaction) – follow
examples/oauth/interactive_tool; the MCP server issuesauth/requestmessages when a token is missing. - Pre-authorised tokens – seed tokens ahead of time as in
examples/oauth/pre_authorize; useful for background workflows or Temporal deployments. - Token storage – configure memory (default) or Redis in
settings.oauth.token_store.
MCP_APP_SETTINGS_PRELOAD).
Header-based authentication
For servers that require static API keys or custom headers, add them directly to the server configuration and load the secret from your secrets file or environment:mcp_agent.config.yaml
mcp_agent.secrets.yaml
${DOCS_API_TOKEN} from the secrets file or environment and injects it into every request.
OAuth basics
OAuth configuration is split into two pieces:- Global OAuth settings (
settings.oauth) – token storage, loopback ports, and general behavior. - Per-server OAuth settings (
mcp.servers[].auth.oauth) – provider-specific details such as client ID, scopes, or whether theresourceparameter is supported.
Global configuration
mcp_agent.config.yaml
Per-server configuration
mcp_agent.config.yaml
mcp_agent.secrets.yaml
examples/basic/oauth_basic_agent.
OAuth flows in practice
Loopback (client-only) flow
Whenauth.oauth.enabled is true and no token is cached, mcp-agent:
- Launches a loopback HTTP listener on one of the
loopback_ports. - Opens the provider login page in the user’s browser.
- Receives the authorization code at the loopback URL and exchanges it for an access token.
- Stores the token in the configured token store (memory or Redis).
oauth_basic_agent example to see the flow end-to-end.
Interactive tool flow
Servers can also request authorization during tool execution by emittingauth/request messages. The examples/oauth/interactive_tool project demonstrates this pattern:
- The MCP server (backed by mcp-agent) exposes a tool that talks to the GitHub MCP server.
- If no token is cached, the server requests authorization; the client opens the browser and resumes once the user approves.
- Tokens are stored via the same
settings.oauthconfiguration.
Pre-authorised tokens
Sometimes workflows run in the background (for example on Temporal workers) and cannot open a browser. Pre-seed tokens using theworkflows-store-credentials tool before the workflow runs. The examples/oauth/pre_authorize folder shows how:
- Obtain a token out-of-band (using a previous flow or provider tooling).
- Call
workflows-store-credentialswith the token and desired server identity. - Run the workflow; it reads the cached token without additional prompts.
Secrets and environment variables
For local development, keep OAuth credentials inmcp_agent.secrets.yaml (gitignored by default). In production or CI/CD, prefer environment variables or MCP_APP_SETTINGS_PRELOAD to avoid writing plaintext files:
MCP_APP_SETTINGS_PRELOAD_STRICT=true causes the app to fail fast if the preload cannot be parsed.
Debugging tips
- Set
logger.level: debuginmcp_agent.config.yamlto inspect OAuth requests and token caching. - Cached tokens live under
context.token_store/context.token_manager; inspect them when writing custom automation. - For Redis-backed storage, ensure
OAUTH_REDIS_URLis reachable from both client and worker processes.
