MaiAgent as an OAuth Authorization Server
Connect MCP clients such as Claude to MaiAgent MCP with OAuth 2.1 (authorization code + PKCE): endpoints, application registration, Redirect URIs, token lifetime and rotation, and how scopes actually
MaiAgent has a built-in OAuth 2.1 authorization server. MCP clients such as Claude can connect to MaiAgent MCP using OAuth without pasting an API Key into a configuration file: after a member signs in through a browser and grants authorization, the client receives a short-lived access token to operate MaiAgent as that member. This page is for two audiences: customer IT teams who want employees to connect to MaiAgent with their own accounts in MCP clients, and developers who want their services to call MaiAgent on behalf of members.
This page describes MaiAgent issuing tokens. For the reverse direction, where MaiAgent connects to a third-party OAuth service, see OAuth 2.0 Integration and Automatic Token Refresh.
Choosing Between OAuth and API Key
Identity represented
The member who grants authorization
The member associated with the key
Setup
The member signs in and consents through a browser, without handling any secrets
Copy the key into the client settings
Lifetime
Access tokens last 1 hour; the client refreshes them automatically
Based on the key's expiration setting; can be set to never expire
Permissions
The member's role permissions; scopes do not impose additional restrictions (see How Scopes Actually Work)
The member's role permissions
Members who sign in only through SSO and have no MaiAgent password can also authorize OAuth through their organization accounts. See Sign-in and Consent.
Endpoints
The SaaS authorization server is https://api.maiagent.ai. For on-premises and private cloud deployments, replace it with your company's platform domain. Metadata URLs are generated using the domain of the request.
Authorization server metadata (RFC 8414)
GET /.well-known/oauth-authorization-server
Authorization
GET /o/authorize/
Token exchange
POST /o/token/
Token revocation (RFC 7009)
POST /o/revoke_token/
The capabilities declared in the metadata match the actual behavior:
response_types_supported
code
grant_types_supported
authorization_code, refresh_token
code_challenge_methods_supported
S256
token_endpoint_auth_methods_supported
none, client_secret_basic, client_secret_post
scopes_supported
read, write
Registering an OAuth Application in Your Organization
In the administration interface, go to Organization Settings → Organization Overview → Management Actions → OAuth Applications to create an application and obtain its Client ID. The Connection Information card lists the Discovery, Authorize, and Token endpoints. For creation steps, who can manage applications, and how to store and regenerate the Client Secret, see OAuth Applications in the user manual. Keep these integration requirements in mind:
The application type cannot be changed after creation: Public applications are for MCP clients (such as Claude), use PKCE for authorization, and receive no client secret. Confidential applications are for server-side integrations and receive a client secret.
Regenerating the Client Secret immediately invalidates the old secret but does not affect tokens already issued. Deleting the application immediately invalidates all tokens for every member connected through it.
Each organization can have up to 20 OAuth applications.
The same functionality is available through the API:
/api/v1/oauth/applications/(GET, POST, PATCH, DELETE, plusPOST …/{id}/regenerate-secret/). Permission rules are the same as in the administration interface.
Redirect URI Rules
Exact matching: The scheme, host, port, and path must all match. Wildcards are not supported. If the registered URI includes query parameters, the authorization request must include them too.
Only
https://is accepted.http://is allowed only for local callbacks:localhost,127.0.0.1, and::1.Local callback ports: When you register
http://127.0.0.1/…orhttp://[::1]/…, the authorization request can use any port (RFC 8252). When you registerhttp://localhost:<port>/…, the port must match exactly. If your local MCP client chooses a random port each time, register the127.0.0.1form.For web-based MCP clients, follow the client's documentation for the callback URL. The example in the administration form is
https://claude.ai/api/mcp/auth_callback.
Authorization Flow
When an MCP client connects to MaiAgent MCP (https://mcp.maiagent.ai/mcp on SaaS), the flow is as follows:
The client sends no credentials. The MCP server returns
401, with theWWW-Authenticateheader pointing tohttps://mcp.maiagent.ai/.well-known/oauth-protected-resource/mcp.The client reads this resource metadata (RFC 9728). Its
authorization_serverspoints tohttps://api.maiagent.ai, and the client then reads the authorization server metadata.The client opens a browser at
/o/authorize/, where the member signs in and consents.MaiAgent redirects the authorization code to the Redirect URI. The client sends
code_verifierto/o/token/to exchange the code for tokens.Every subsequent call includes
Authorization: Bearer <access_token>. When the access token expires or is revoked, the MCP server returns401, and the client uses the refresh token to obtain a new one.
If you implement your own client, use the following formats for authorization and token exchange requests:
PKCE is always required, for both Public and Confidential applications. Use
S256forcode_challenge_method.Confidential applications must also provide the secret using HTTP Basic (
client_secret_basic) or theclient_secretform field (client_secret_post).Authorization codes are valid for 300 seconds.
The token exchange response includes
access_token,token_type(Bearer),expires_in(3600),refresh_token, andscope.
Sign-in and Consent
The authorization page retains Email and password sign-in. When continuing authorization, it also offers Sign in with an organization account (SSO). This entry point completes sign-in through the administration interface and then returns to the authorization page, so members who use only SSO and have no MaiAgent password can also authorize access.
The consent page shows the application name, its organization, and the Email of the member currently signed in.
If the member already has a valid token for this application, reconnecting skips the consent page.
SSO Sign-in Handoff and Security Boundaries
For example, an employee normally signs in to MaiAgent using SAML and has not set a separate MaiAgent password. When an MCP client such as Claude opens the authorization page, the employee chooses organization account sign-in, completes their usual sign-in to the administration interface, returns to the authorization page, and then consents to the client connecting as them.
The SSO handoff is a browser sign-in flow. It does not replace the client's authorization code and PKCE exchange: the client still uses the authorization and token endpoints above and does not need to handle the administration sign-in handoff code itself. Integrators should be aware of the following:
The signed sign-in request received by the administration interface is valid for 10 minutes. The one-time handoff code used after sign-in is valid for 300 seconds and can be redeemed only once. This handoff code serves a different purpose from the OAuth authorization code received by the MCP client.
The handoff code is bound to the browser that initiated sign-in and only continues this site's
/o/authorize/flow. It cannot be used in another browser, and an external site cannot be specified as the redirect destination after sign-in.The handoff cannot complete if the account is disabled or its SSO authentication source has sign-in disabled. When a member explicitly signs out, sign-in attempts that were still incomplete before sign-out also become invalid.
API Keys and member embedding tokens cannot be exchanged for sign-in handoff codes. They cannot replace a member completing sign-in and authorization in the browser.
Token Identity and Organization
The token represents the member who grants authorization. Its organization is fixed to the organization that owns the application. Calls do not need or rely on the
X-Organization-Idheader.At the time of the call, this person must be an active member of that organization. If the member is disabled or removed from the organization, or the organization is deleted, the token immediately becomes unusable and the API returns
401.Access tokens can also call the platform REST API directly (
Authorization: Bearer <access_token>), with the same permission checks as API Keys. Exceptions: AI Gateway's OpenAI-compatible endpoints accept only API Keys, and high-risk operations such as deleting your own account accept only interactive sign-in.
Token Lifetime and Rotation
Authorization code
300 seconds
Access token
3600 seconds (1 hour)
Refresh token
Rotated on every use: the response contains a new refresh token, and the old one is invalidated
Resubmission after rotation
When the same request is resubmitted due to network issues or concurrency, the old refresh token that was just rotated remains usable for 120 seconds
Explicit revocation
Takes effect immediately, with no 120-second grace period
Refresh token lifetime
Each refresh token is valid for 30 days from issuance. Every successful refresh rotates it to a new token, and the 30-day lifetime starts again from the new token's issuance time. If no refresh occurs before expiration, reauthorization is required
When your integration receives invalid_grant, stop using that refresh token and prompt the member to authorize again. Do not retry in the background. For details on revocation and rotation, see When a Refresh Token Is Revoked or Becomes Invalid.
There are three ways to revoke tokens:
Integrator
POST /o/revoke_token/, with token (optionally token_type_hint) and client authentication
A single token
The member
GET /api/v1/oauth/authorized-applications/ lists their authorized applications; DELETE /api/v1/oauth/authorized-applications/{id}/ revokes authorization
All tokens this member granted to the application
Organization administrator
Delete the OAuth application
All tokens all members granted to the application
When a Refresh Token Is Revoked or Becomes Invalid
Once a refresh token is explicitly revoked, it immediately stops being usable to obtain access tokens. If your integration continues sending it to POST /o/token/, it receives HTTP 400 with the error code invalid_grant. Handle this as follows:
When you receive
invalid_grant, stop using that refresh token and prompt the member to authorize again. Do not retry in the background.When a member revokes an entire authorized application, all tokens they granted to that application are revoked, and all old refresh tokens become unusable.
After normal rotation, when the same request is resubmitted due to network issues or concurrency, the old refresh token that was just rotated has a 120-second retry grace period. This grace period does not apply to explicit revocation.
How Scopes Actually Work
The authorization server accepts two scopes: read and write. If the authorization request omits scope, both are granted by default. Scopes currently do not restrict any API: what a member can do is determined entirely by their role permissions, just as when they sign in to the administration interface or use an API Key. To restrict what an MCP client can do, create a dedicated role for its user. See Role and Permission Management in the user manual.
Frequently Asked Questions
Last updated
Was this helpful?