# bex as an OAuth 2.1 provider for agents (MCP)

Deferred from the reverted w4/m9 (dashboard-as-OAuth2-client — wrong direction; see `DO_NOT_DO.md`: first-party stays on Kratos sessions). Build the **provider side** when agent/MCP OAuth 2.1 lands (the MCP authorization spec makes connecting agents OAuth 2.1 clients of bex):

- **Kratos native bridge, not a custom provider**: set `oauth2_provider.url` → hydra-admin in `kratos.values.yaml`. Kratos accepts Hydra login challenges itself; the dashboard login page only passes `login_challenge` through (near-zero UI change).
- **Headless consent acceptor**: tiny endpoint that auto-accepts for `skip_consent`/trusted clients; a real consent UI only when true third-party apps arrive. (A working Go implementation existed in the reverted m9 — see git history of `lego/backend/internal/api/oauth_provider.go` — and can be cribbed, minus the login half Kratos now owns.)
- **Dynamic Client Registration (DCR)** for MCP clients per the MCP auth spec — not seeding scripts.
- **Resource-server side is already done**: bex-api validates `Authorization: Bearer` via Hydra introspection (w4/m2).

Why w2: this is agent-facing surface (pillar 3–5), sequenced with the MCP milestones — it's what lets a third-party agent (e.g. Claude Code over MCP) get a user-consented token instead of a hand-provisioned API key.

Refs: IETF `draft-ietf-oauth-browser-based-apps` (BFF > browser tokens); Ory guidance (first-party = Kratos sessions, Hydra = third-party/machine); Kratos `oauth2_provider` docs; MCP authorization spec (OAuth 2.1 + DCR).
