AI Connector (MCP)

OAuth Reference

OAuth 2.1 endpoints for MCP connector authentication.

Recurex implements a custom OAuth 2.1 authorization server for MCP clients.

Discovery endpoints

EndpointStandard
GET /.well-known/oauth-authorization-serverRFC 8414
GET /.well-known/oauth-protected-resourceRFC 9728
GET /api/mcp/.well-known/oauth-protected-resourceRFC 9728 (MCP path)

Protected resource metadata points to authorization servers and supported scope expenses:rw.

Authorization

GET /api/oauth/authorize
ParamRequiredNotes
client_idYesDCR client ID or CIMD URL
redirect_uriYesMust match registration
code_challengeYesS256 PKCE
code_challenge_methodNoMust be S256
stateNoCSRF protection
resourceNoDefaults to MCP resource URL

Unauthenticated users redirect to /login?next=.... Authenticated users see an HTML consent page.

POST with action=approve|deny completes the flow. Approval issues an HMAC-signed authorization code (10 min TTL) bound to user, org, PKCE challenge, and resource.

Token exchange

POST /api/oauth/token

Authorization code grant

ParamRequired
grant_typeauthorization_code
codeYes
code_verifierYes
redirect_uriYes
client_idYes
resourceValidated if present

Response:

{
  "access_token": "...",
  "refresh_token": "...",
  "token_type": "Bearer",
  "expires_in": 3600,
  "scope": "expenses:rw"
}

Refresh token grant

Rotating refresh — old token revoked, new pair issued. Refresh tokens last 30 days.

Dynamic client registration

POST /api/oauth/register

RFC 7591. Body: { "redirect_uris": ["..."], "client_name": "..." }. Returns client_id.

Token storage

Access and refresh tokens are hashed in mcp_tokens. Authorization codes are single-use with replay prevention via oauth_used_codes.

Environment variables

VariablePurpose
OAUTH_CODE_SECRETHMAC signing for auth codes
NEXT_PUBLIC_SITE_URLResource URL and redirects
SUPABASE_SERVICE_ROLE_KEYToken persistence

MCP authentication

Authorization: Bearer <access_token>

Validated by lib/mcp-auth.ts: non-revoked, non-expired token; user still a member of token's org; plan includes ai:connector.