mig: add typed MCP auth clients and server associations

Merged
DA
1mo ago
+625-18

AIM-588

Stacked on #7653.

Summary

Schema only, in four tables. They hold typed auth clients: the ways an MCP server can authenticate to its upstream. Columns shared by several types live on mcp_auth_clients; fields only one type needs get that type's own table (today only EMA).

  • mcp_auth_clients: one row per auth client.
    • Columns: a stable id, the org, an optional project (NULL means shared across the organization), a generated attachment_scope (the project or organization owner, used by scope-checked foreign keys), client_type, source (backfill or admin), a name, an enabled flag and the author. It's soft deleted.
    • Type and source: validated in application code (remote_session, service_account, static_credential, pass_through, ema); the DB only checks they're non-empty, so a new type needs no migration.
    • OAuth registration: OAuth-backed types (remote_session, service_account, ema) point at their remote_session_clients row here, rather than copying it. The pointer is remote_session_client_id plus client_attachment_scope, with a composite FK.
    • Registration check: the registration id and its scope are set together, and the registration must sit in the client's project or organization, or be platform-global. Which types need one is validated in application code.
    • Uniqueness: partial unique indexes on (organization_id, remote_session_client_id) for live source = 'backfill' rows, per type, give each registration at most one backfilled remote_session and one service_account client per organization, so the backfill finds rather than duplicates. Admin-created clients may share a registration (RFC §7.3/7.4). A platform-global registration gets one backfilled client per organization.
  • mcp_auth_client_emas: extra detail for ema clients only.
    • Columns: the upstream authorization server (remote_session_issuer_id), the resource and the requested scopes.
    • Issuer must match the registration: the table copies the base row's organization_id and remote_session_client_id. One FK pins those copies, and client_type = 'ema', to the base row via (id, client_type, remote_session_client_id, organization_id). A second FK pins (remote_session_client_id, remote_session_issuer_id) to remote_session_clients.
    • Why it's built this way: this is the smallest shape that keeps "the issuer is the registration's issuer" enforced by the database without copying scope columns.
    • Placement: the table sits after remote_session_clients_id_issuer_key in schema.sql, because Atlas needs that key to exist first.
    • Uniqueness: one live row per (organization, issuer, registration, resource); the row is soft deleted with its base row, so a retired client frees the key. Two orgs can share a platform-global registration.
    • Not stored here: the vouching order stays in remote_session_ema_providers, and the user session issuer is no longer part of the key.
  • mcp_auth_client_headers: headers owned by a client. Same shape, CHECKs and access classes as remote_mcp_server_headers. Static credentials and pass-through keep their material here.
  • mcp_server_auth_clients: which clients each server offers, many-to-many, with a source (backfill or admin) so the backfill only ever retires links it created. It uses the same tenant and scope pattern as mcp_server_remote_session_clients, which this PR leaves untouched.
  • No upstream auth: not modeled as a client. It stays the existing no_auth selection (RFC §7.5); a server with no clients is setup incomplete. Pass-through only forwards explicitly named headers (value_from_request_header); it never forwards the caller's own credentials by default.
  • mcp_servers.requires_upstream_auth: nullable boolean. true means the upstream needs authentication, false means it explicitly needs none, NULL means undecided. The backfill only ever sets true (for a server it links a client to); the admin UI (AIM-592) sets false. Adding a nullable column with no default is metadata-only.

An earlier revision had a table per type. It's collapsed to these four because only EMA has fields of its own. The OAuth-backed types differ only in which registration they point at, and that now lives on the base row.

remote_session_issuers.passthrough isn't modeled. It's stored issuer config ("the MCP client registers and transacts directly with this issuer"), shown in admin views, not an outbound identity.

Nothing reads or writes these tables yet. The existing outbound configuration stays authoritative. The backfill and the admin runner phase follow in separate PRs.

Motivation

This is milestone 2 of RFC: Multiple Upstream Credentials for MCP Servers, With RBAC Control. Every existing upstream identity needs a stable, typed id before permissions (AIM-589/591) and the single resolver (AIM-594) can refer to it. EMA becomes an auth client a server offers, rather than something derived from its sign-in.

The regenerated sqlc files only add the new table models.

šŸ¤– Generated with Claude Code


Summary by cubic

Adds the schema for typed MCP auth clients — the ways an MCP server can authenticate to its upstream — so each existing upstream identity gets a stable, typed ID before permissions and the unified resolver can reference it (AIM-588). Also adds a nullable requires_upstream_auth flag on mcp_servers (true needs auth, false explicitly needs none, NULL undecided). Nothing reads or writes these yet; the existing outbound configuration stays authoritative. The diff also carries unrelated changes merged in from main: the typed MCP route base (AIM-601), the hosted MCP orphan cleanup (AIM-580), hosted MCP server-key writes and flag removal (AIM-579), the agent provisioning wizard and identity page, the OAuth metadata discovery recovery (AIM-515), the refusal of Authorization as a configured header source (AIM-630), the OAuth token exchange error naming (AIM-388), the CLI rebrand to speakeasy, the Assistants rollout flag retirement, and a Mise header fix.

Schema

  • mcp_auth_clients holds one row per client: stable id, org/project tenant, generated attachment_scope, client_type, source (backfill | admin), name, enabled flag, and soft delete.
  • OAuth-backed types (remote_session, service_account, ema) reference existing remote_session_clients rows rather than copying them; composite FKs prevent a type row from attaching to the wrong client type or tenant. Static credentials and pass-throughs keep their material in headers.
  • mcp_auth_client_emas pins the upstream issuer, registration, resource, and requested scopes, one row per issuer, registration, and resource.
  • mcp_auth_client_headers stores client-owned headers; mcp_server_auth_clients maps which clients each server offers, many-to-many, with source so the backfill only retires links it created.
  • client_type and source are validated in application code, so a new type needs no migration.