OAuth Provider & MCP

Let other apps, like MCP clients, ask your users to sign in and approve access to their account instead of pasting an API token.

Jumpstart Pro is an OAuth 2.1 authorization server. It supports the authorization code flow with PKCE, refresh token rotation, dynamic client registration, token revocation and token introspection, plus the discovery documents MCP clients look for. Claude, Cursor, VS Code and other MCP clients can connect to your API or MCP server and sign users in through the browser.

This is different from OmniAuth, which lets users sign in to your app with another provider. Here your app is the provider.

How it works

  1. A client calls your API without a token and gets a 401 with a WWW-Authenticate header pointing at /.well-known/oauth-protected-resource.
  2. That document points at /.well-known/oauth-authorization-server, which lists every endpoint below.
  3. The client registers itself at POST /oauth/register and gets a client_id.
  4. The client opens /oauth/authorize in the browser. The user signs in, including two-factor authentication, and approves or denies access.
  5. The client exchanges the code at POST /oauth/token for an access token and a refresh token.

Access tokens are ApiToken records that expire, so every API endpoint accepts them, current_user works as usual, and API token scopes apply. They don't appear on the API Tokens page. Users see the applications they've approved under Authorized Apps in their settings, and revoking one stops its tokens immediately.

Endpoints

GET  /.well-known/oauth-authorization-server   RFC 8414
GET  /.well-known/oauth-protected-resource     RFC 9728
GET  /oauth/authorize                          Consent screen
POST /oauth/token                              Authorization code and refresh token grants
POST /oauth/register                           RFC 7591 dynamic client registration
POST /oauth/revoke                             RFC 7009
POST /oauth/introspect                         RFC 7662, for confidential clients created in the admin
GET/POST /oauth/userinfo                       User identity claims for Sign in with X

Configuration

Everything works without configuration. config/initializers/oauth_provider.rb changes token lifetimes, turns off dynamic client registration, and lists other resources tokens can be issued for:

Rails.application.config.oauth_provider.issuer = "https://example.com"
Rails.application.config.oauth_provider.access_token_ttl = 1.hour
Rails.application.config.oauth_provider.refresh_token_ttl = 90.days
Rails.application.config.oauth_provider.dynamic_client_registration = true
Rails.application.config.oauth_provider.resources = ["https://mcp.example.com/mcp"]

Pin issuer in production on single-domain apps so discovery metadata can be publicly cached and doesn't depend on the request's Host header. Leave it unset if accounts use subdomains or custom domains, and configure config.hosts instead. Clients can always request a token for a URL on your app's own host. Add a resource to resources when it's served from another host, like an MCP server running separately.

Scopes

OAuth uses your API token scopes. Without any configured, the consent screen tells users the application can do anything they can, which is what an API token allows. With scopes configured in config/initializers/api_tokens.rb, clients can ask for some of them with the scope parameter, the consent screen lists them, and your controllers enforce them with require_api_scope "accounts:read" as they do for API tokens. The built-in API endpoints already do.

Two built-in identity scopes, profile and email, are always available for "Sign in with X". Tokens with only identity scopes can read GET /oauth/userinfo and are rejected by API endpoints even when API token scopes aren't configured.

Sign in with your app

To let another app sign users in with their account on your app, create a confidential application in the admin area and limit its scopes to profile and email. Once a user approves an admin-created application, repeat authorizations for the same or narrower scopes skip the consent screen unless the client passes prompt=consent.

Exchange the authorization code for a token and call GET /oauth/userinfo (or POST /oauth/userinfo) with Authorization: Bearer <token> to fetch sub, name, given_name, family_name, picture, email and email_verified. Client apps should identify users by sub rather than auto-linking by email, since Jumpstart doesn't confirm sign-up email addresses by default (email_verified: false). Because PKCE with S256 is required for every client, enable option :pkce, true in omniauth-oauth2 strategies.

Registering applications

Most MCP clients register themselves, so there's nothing to set up. To create an application yourself, such as your own mobile app or an MCP server that introspects tokens, add one in the admin area under OAuth Applications. Check Confidential to generate a client secret.

Redirect URIs must match exactly. HTTP is only allowed for localhost, 127.0.0.1 and [::1], which can use any port, and native apps can use their own scheme like myapp://callback.

Serving an MCP server

An MCP server needs to answer unauthenticated requests with a 401 and a WWW-Authenticate: Bearer resource_metadata="..." header. Api::BaseController already does this for controllers inside your app.

For an MCP server on another host, create a confidential application in the admin area, add the server's URL to resources, and have the server check each bearer token at POST /oauth/introspect with that application's credentials. The response says whether the token is active and includes its scope, sub (the user's ID), exp and aud, the resource the token was issued for. The server should reject a token whose aud is set to another resource. It answers for API tokens too, so the server accepts both.

Tokens issued for a resource on another host only work there. Your app's API rejects them, so a server that receives one can't replay it against your API.

Cleaning up

Applications that register but are never approved, and authorization codes that expire before the client exchanges them, stay in the database until pruned. Grants that were revoked or have expired are kept, so a client the user reconnects with is still registered. Pruning runs daily from config/recurring.yml and config/schedule.rb:

OauthGrant.prune
OauthApplication.prune

Security notes

  • PKCE with S256 is required for every client, and authorization codes expire after 10 minutes and work once. Using a code twice revokes the tokens it issued.
  • Refresh tokens rotate every time they're used. Using an old refresh token revokes the grant, since it may have leaked. The token a client just replaced keeps working for 30 seconds, so clients that refresh in parallel or retry a lost response aren't signed out.
  • Staff can't approve applications while impersonating a user.
  • Changing or resetting a password revokes every OAuth grant for that user.
  • The approval form redirects to other hosts and custom schemes. If you add a content security policy with form-action, it has to allow your clients' redirect URIs.