ADR 0004doc/{zh,en}/adr/0004-mcp-oauth-per-principal.md

ADR 0004: OAuth per Principal for Remote MCP Servers

Status: accepted, 2026-11-14; amended by ADR 0012, decision 8, on 2026-11-19: the sign-in is a signin card in the requester's direct chat, refused in groups, and a person's token i

Status: accepted, 2026-11-14; amended by ADR 0012, decision 8, on 2026-11-19: the sign-in is a sign_in card in the requester's direct chat, refused in groups, and a person's token is their credential mcp:<server>.

Context

Remote MCP servers (issue trackers, docs, mail) act on a person's own account and require OAuth 2.1 sign-in (MCP authorization, 2025-06-18 and later). Today a URL server gets only static headers from the instance configuration, so one token would act for every person who talks to the Host. A sign-in needs things no plugin can provide: a redirect URI the browser returns to, flow state that survives the wait, and token storage bound to the person, not to the instance. The Host has no HTTP listener of its own; the httpapi channel is a plugin.

Decision

  1. Host listener. The Host configuration gains oauth: {listen, public_url}. With listen set the Host serves GET /oauth/callback and GET /oauth/client.json (a Client ID Metadata Document) on that address; public_url (default http://{listen}) is how browsers reach it. Without oauth, servers that need sign-in report mcp.oauth_unavailable and offer no tools.
  2. Which servers. A manifest server declares auth: oauth (optional scopes), or the Host learns it the first time the server answers 401 with WWW-Authenticate: Bearer. Static headers stay for servers with a shared key.
  3. Flow (internal/oauth). Discover the protected resource metadata (RFC 9728, from resource_metadata in WWW-Authenticate or the well-known path) and the authorization server metadata (RFC 8414, then OpenID discovery). The client is the operator's client_id option if set, else the metadata document when public_url is https and the server supports it, else Dynamic Client Registration (RFC 7591), cached per (instance, issuer). Authorize with PKCE S256, state and the resource parameter (RFC 8707); check iss on the redirect when the server announces it (RFC 9207). Flow state is a row in oauth_flows bound to (instance, principal, server) with the verifier, and expires after 10 minutes.
  4. Tokens. Stored in the existing encrypted secret store under (instance, principal) as oauth:{server}, the principal being the person when principals are linked. Refreshed when less than 60 s remain; a failed refresh deletes them, so the person signs in again.
  5. Sessions per principal. A server with OAuth gets one MCP session per (server, principal), carrying that principal's token, closed after 10 idle minutes; a 401 during a session refreshes once, then asks for sign-in.
  6. What the model sees. For a principal without a token the server's tools are not listed; instead {prefix}__sign_in (no arguments, description: sign in to the service so its tools become available; ask the user first). It asks the person a link question with Done and Cancel; the browser lands on the callback, which stores the tokens. The tools appear from the person's next message, because a run's tool list is pinned.

Consequences

  • ◆New Host configuration (oauth), a manifest field (auth, scopes), a store table (oauth_flows) and an HTTP listener. No plugin API changes: the manifest adapter (internal/bundle) reaches the flow through a Host-internal port.
  • ◆A Host reachable only on loopback can sign in only people whose browser runs on that machine; a team Host needs a public_url its users can reach, preferably https.
  • ◆In a group conversation the link question is shown in that conversation; sending it to the requester's direct chat is not part of this decision.
  • ◆Tokens never reach the model, the logs or the checkpoint.

Design: MCP section 9.