Agent authentication
Status: implemented (Phase A of the Agent Bridge, 25b7c50). The Bridge API (Phase B, 590b515) authenticates the same tokens on every request (api/v1/bridge/deps.py, Agent Bridge protocol); cloud providers and Remote Access (Phase D and later) are still design and reuse what is described here.
Before and after
Until Phase A, MCP, REST and the realtime WebSocket were protected only by binding to loopback, a Host allow-list (api/trusted_hosts.py, against DNS rebinding) and an Origin check (api/security.py). Whether a call came from an agent was one boolean context variable.
Now:
- MCP requires a token.
/mcpand/api/v1/mcpanswer401unless the request carriesAuthorization: Bearer <token>for a token that exists, is not revoked and has not expired. Every request is authenticated on its own (the endpoint is stateless), so a revocation or a grant change applies to the very next request. - A development mode keeps the old behaviour for experiments:
PHANTOM_MCP_AUTH=dev(orMCP_AUTH=dev). It refuses to start unlessAPI_HOSTis a loopback address (127.0.0.1,::1,localhost;0.0.0.0is refused; the launcher passes its--hostasAPI_HOST), logs a warning on every start, is reported asdev_mode: truebyGET /api/v1/connections/status(the interface shows a banner), and acts with every grant on every vault while every hard user-only rule still applies. The default istoken. - The agent flag is a principal.
app.core.agents.contextholds aPrincipal(kinduser/agent/dev, agent id, token id, permitted vaults, grants, transport) in one context variable.in_agent_call()keeps its meaning for the existing hard rules (true foragentanddev); grants are checked as described in Agent permissions.
Personal access tokens
These are local personal access tokens, not OAuth, and Phantom never calls them OAuth (app/core/agents/tokens.py).
| Property | Behaviour |
|---|---|
| Format | phm_pat_<lookup>_<secret>: lookup is 12 random characters from the lowercase base32 alphabet (a-z2-7), secret is 32 bytes from secrets in unpadded base64url (43 characters). The phm_ prefix lets secret scanners spot a leak. |
| Stored | lookup (unique index) and HMAC-SHA256(pepper, secret) as hex. The pepper is HKDF-SHA256(config key, info="phantom-pat"), derived once per process from PHANTOM_CONFIG_KEY or ~/.phantom/config.key (0600). A database copy alone cannot test guesses. |
| Verified | Row found by lookup, digests compared with hmac.compare_digest. On a lookup miss a digest is still computed and compared against a dummy, so a miss costs the same as a wrong secret. |
| Shown | The plaintext is returned once, by POST /connections/tokens and POST /connections/tokens/{id}/rotate, both Cache-Control: no-store. Lists, reads, audit rows and logs show only phm_pat_<lookup>…. The interface keeps it in component state only, never in storage. |
| Fields | name, description, grants, permitted vaults or all vaults (off by default), expiry (or never), created, last used (time and address, recorded at most every 15 s per address), revoked (time and reason user / rotated), the token it was rotated from. |
| Rotate | A new secret with the same name, grants, vaults and expiry; the old token is revoked (rotated) in the same transaction, its agents move to the new token, its open streams close. |
| Revoke | Immediate and idempotent; open subscriptions/listen streams of the token close with a stream_closed error, Bridge streams with stream.closed, and agent.token.revoked is announced. |
| Edit | Name, description, grants, vaults, expiry. Grant or vault changes close the token's open streams (authorization_changed). A revoked token cannot be edited (409). |
| Bridge work | Rotate, revoke, a grant or vault change, and disconnecting an agent cancel the affected agents' open Bridge tasks (authorization_changed), expire their pending approvals and fail messages still queued for them, in the same transaction (Agent Bridge protocol). |
Errors of a refused MCP request (401, standard error body):
| Code | When |
|---|---|
unauthenticated | No Authorization: Bearer header |
invalid_token | Malformed, unknown, or wrong secret |
token_expired | Past expires_at |
token_revoked | Revoked or rotated |
The WWW-Authenticate header is Bearer resource_metadata="<origin>/.well-known/oauth-protected-resource/<path>", with error="invalid_token" for every code but unauthenticated. Expired and revoked tokens are audited (authenticate, denied).
Connections API (user only)
All under /api/v1/connections. Every route refuses a request that carries an Authorization header or MCP transport headers (403 permission_denied, details {setting: "connections"}), and every management use case refuses inside an agent call, so an agent can never mint, widen, extend or revoke a token. There are no MCP tools for any of it.
| Route | Does |
|---|---|
GET /status | auth mode, dev_mode, MCP paths, active / total tokens, agents total / online, open streams, audited agent operations in 24 h |
GET /permissions | grantable actions per resource, the presets, the hard rules |
GET /tokens, POST /tokens | list; create (returns {token, plaintext}) |
GET /tokens/{id}, PATCH /tokens/{id} | read; change name, description, grants, vaults, expiry |
POST /tokens/{id}/rotate, POST /tokens/{id}/revoke | rotate (returns {token, plaintext}); revoke |
GET /agents, GET /agents/{agent_id} | registry with effective status |
PATCH /agents/{agent_id}, POST /agents/{agent_id}/disconnect | rename; disconnect |
GET /activity | audit rows newest first; filters agent_id, token_id, vault_id, outcome; paging before, limit (max 200), next_before |
Events (ids only): agent.token.updated (created, updated, rotated), agent.token.revoked, agent.updated.
Agent identity
Every client gets a backend-assigned identity (agents): public id agt_<16 base32>, display name, implementation and version, connection type (mcp today), protocol version, declared capabilities, token, status and timestamps. An MCP agent is keyed by token and the clientInfo.name of initialize; a client that never sends initialize is keyed by the token alone. Whatever the client says about itself is stored as declared (capabilities are kept whole up to 4 KB, otherwise only their top-level keys); what it may do always comes from the token's grants. An actor field in arguments, or a client calling itself "user", changes nothing: attribution comes from the authenticated principal only.
Status (app/services/agents/status.py), computed when read:
| Status | Rule |
|---|---|
revoked, expired | the token is, whatever else holds |
connecting | initialize seen, no other request yet |
connected | any other request seen in the last AGENT_INACTIVE_SECONDS (300 s) |
degraded | connected, but the last 3 or more operations failed or were refused |
disconnected | no request for 300 s, or disconnected by the user until its next request |
Disconnect closes the agent's open streams and does not revoke anything; to keep an agent out, revoke its token.
Limitation: the endpoint is stateless, so a request after initialize carries no client name. When several clients share one token, such requests are attributed to the client of that token seen most recently. Use one token per client; grants are per token either way.
How the principal reaches a tool
Measured against MCP Python SDK 2.3.0: stateless requests are served from the session manager's own task group, so a context variable set around the HTTP request is not relied on. Instead:
app/api/agent_auth.py(McpAuthentication, an ASGI wrapper inside the origin guard) authenticates the request and stores the principal in the ASGI scope (phantom.principal).app/mcp/principal.py(PrincipalMiddleware, an SDK context-tier middleware) runs in the task of each handler, reads the principal from the request the transport attached to the message, records the agent (initializeor activity), and sets the principal for exactly that handler.subscriptions/listenruns inside a cancel scope registered per token and agent (app/runtime/agents.py), which revoke and disconnect cancel.- The toolkit fails closed: a tool reached with no principal answers
unauthenticated.
Tests prove it: test_tools_see_their_own_token_under_concurrency (24 concurrent calls with two tokens on different vaults never mix), test_closing_from_the_user_side_ends_the_listen_stream (real uvicorn, revoke and disconnect end the stream).
Protected resource metadata
GET /.well-known/oauth-protected-resource and the path-suffixed forms /.well-known/oauth-protected-resource/mcp and …/api/v1/mcp (RFC 9728) return the resource URL, bearer_methods_supported: ["header"] and phantom_token_type stating these are locally issued tokens, not OAuth. No authorization server is named. Standard OAuth (authorization server metadata, audience-bound tokens) is required for remote MCP deployments and is planned with Remote Access (Agent Bridge, mode C).
Connecting a client
Create a token in Settings → AI & Agents → Add connection → Bridge; the result view shows ready-to-copy configurations, for example:
claude mcp add --transport http phantom http://127.0.0.1:8000/mcp --header "Authorization: Bearer phm_pat_…"The stdio bridge (backend/mcp_server.py) sends $PHANTOM_MCP_TOKEN as a bearer token. See Phantom MCP server for every client and the migration of existing setups.
Remaining trust boundary
The REST API used by Phantom's own interface stays unauthenticated on loopback: a request without a token is the user. A local process running as the same user can therefore still act as the user (including creating a token); see docs/roadmap/status.md. Requests that carry any Authorization header are refused on the user-only settings and connections routes. Browsers cannot attach Authorization cross-origin, since CORS allows only Content-Type.