Agent code barely changes; what changes is where the key goes.
The env: prefix
Anywhere swarms takes a credential, "env:VAR_NAME" reads it from the environment when the connection is made, instead of embedding it in your source:
"${VAR_NAME}" works too. Prefer either over os.getenv(...) at construction time: the literal never enters the agent object, so it cannot leak through a serialized config or a printed repr.
No authentication
Bearer token
The most common shape. The token is sent asAuthorization: Bearer <key>.
mcp_api_key applies to every server that does not define its own credential, which makes it the right choice for a single server and the wrong one when servers need different keys.
Query parameter
Some hosted servers want the key in the URL’s query string. Build the URL from the environment:URL path segment
Firecrawl takes the key as part of the path:Optional authentication
For a server that serves anonymous traffic, a missing key should lower your rate limit, not crash your program:Custom header
When a server wants its key in something other thanAuthorization, use an MCPConnection and set the header and prefix explicitly:
MCPConnection is also where per-server timeouts and transports live:
OAuth 2.1
For servers that speak the MCP authorization spec. The browser flow runs once and the tokens are cached under~/.swarms/mcp_auth/, so later runs are silent:
access_token= and no flow is run.
Different credentials per server
Mix plain URLs and connection objects in the samemcp_urls list:
Troubleshooting
The key looks right but the URL is malformed
The key looks right but the URL is malformed
An unset environment variable interpolates as the string
None. Check for the variable and exit with a clear message before constructing the URL.OAuth opens a browser on a server with no display
OAuth opens a browser on a server with no display
Set
open_browser=False on MCPOAuthConfig — the authorization URL is logged instead — or use the client_credentials grant.See also
- Model Context Protocol (MCP) — the full connection reference.
- MCPManager API — auth when you are calling MCP without an agent.