Generates encrypted storage and routes for connecting to MCP servers that use OAuth
AshAi.Mcp.OAuth keeps tokens, client registrations, and authorizations in
progress in an AshAi.Mcp.OAuth.Store that your application provides. This
generates one backed by ash_postgres, with the secrets encrypted by
ash_cloak, and the routes that send a user to authorize an MCP server and
bring them back. See the "MCP Client Authorization" guide to write a store of
your own instead.
Example
mix ash_ai.gen.mcp_oauth --user MyApp.Accounts.User
Options
--user- The user resource whose tokens are stored. Defaults toYourApp.Accounts.Userwhen it exists. Tokens then belong to a user and are deleted with them, and the routes are generated, reading the user fromconn.assigns.current_user. Without a user resource, tokens are stored under an opaque owner and the routes are left to you.--domain- The domain for the generated resources. Defaults toYourApp.McpAuth.--vault- TheCloak.Vaultthat encrypts the secrets. Defaults toYourApp.Vault, which is generated if it doesn't exist.--route- Where the routes are mounted. Defaults to/mcp-auth.--yes- Skip confirmation prompts.
What gets generated
- The
ash_cloakandcloakdependencies. - A vault, unless one exists, started by your application. Its key is configured in
config/dev.exsandconfig/test.exs, and read from theCLOAK_KEYenvironment variable in production. - Resources whose
datais encrypted:Token- the tokens a user holds for an MCP server, deleted with the user.Client- clients registered for each MCP server through Dynamic Client Registration.PendingAuthorization- authorizations in progress, keyed by a digest of theirstate. Abandoned ones are deleted after an hour.
YourApp.McpAuth.Store, theAshAi.Mcp.OAuth.Store. Token refreshes take a lease on the token row, so they're serialized across nodes without holding a database connection.YourApp.McpAuth.options/1, theAshAi.Mcp.OAuthoptions for a user,YourApp.McpAuth.allowed_private_hosts/0, which allowslocalhostin development, andYourApp.McpAuth.disconnect_all/1, which revokes a user's tokens. Call it before deleting a user, since deleting the user deletes their tokens without revoking them.- With a user resource and a Phoenix router,
YourAppWeb.McpAuthControllerand its routes:POST /mcp-auth/authorizesends the user to authorize an MCP server.GET /mcp-auth/callbackstores the token and sends the user back.DELETE /mcp-auth/connectiondisconnects the user from an MCP server, revoking their tokens there.GET /mcp-auth/client-metadata.jsonserves the Client ID Metadata Document, which authorization servers are offered when your endpoint's URL is HTTPS.