MCP server OAuth flow

Beta

This feature is in beta. For now it is available only to some users. We will release it gradually to more accounts once testing ends. If you want to use it, ask your JAAK contact for access.

Description

This page describes the OAuth flow that an MCP client uses to get temporary access to JAAK. A client other than Claude Code must follow these steps.

If you only want to connect Claude Code, use Connect agents with MCP.

The flow has these parts:

  1. The client reads the metadata of the authorization server.
  2. The client registers and gets a client_id.
  3. The client opens the authorization screen. The person signs in with Google and approves the access.
  4. The client exchanges the code for an access token and a refresh token.
  5. The client refreshes the access token when it expires.

On this page, https://<host-api> is the public address of the JAAK gateway. https://<host-mcp> is the address of the MCP server. Your JAAK contact gives you both addresses for each environment.

1. Authorization server metadata

The client reads the authorization server configuration:

curl https://<host-api>/.well-known/oauth-authorization-server

Response (shortened example):

{
  "issuer": "https://<host-api>",
  "authorization_endpoint": "https://<host-api>/oauth/authorize",
  "token_endpoint": "https://<host-api>/oauth/token",
  "registration_endpoint": "https://<host-api>/oauth/register",
  "scopes_supported": [
    "signa:templates:read",
    "signa:templates:write",
    "signa:send",
    "signa:read",
    "kyc:validate"
  ],
  "response_types_supported": ["code"],
  "grant_types_supported": ["authorization_code", "refresh_token"],
  "code_challenge_methods_supported": ["S256"],
  "token_endpoint_auth_methods_supported": ["none"],
  "authorization_response_iss_parameter_supported": true
}
  • issuer is the public origin of the gateway.
  • The client must use PKCE with S256. No other method is supported.
  • The client is public. It does not use a secret. The token endpoint auth method is none.

Protected resource metadata

Each toolset has its own path. The MCP server publishes its metadata at the toolset path, with the prefix /.well-known/oauth-protected-resource:

curl https://<host-mcp>/.well-known/oauth-protected-resource/mcp/signa-send

The response shows:

  • resource: the URL of the toolset.
  • authorization_servers: the JAAK authorization server.
  • scopes_supported: the scopes that the tools of that toolset use.

When a call arrives without a valid token, the server responds with the WWW-Authenticate header. That header contains the resource_metadata URL of that toolset. If the toolset needs fewer scopes than all of them, the header also includes scope.

2. Client registration

The client registers with POST /oauth/register. The body is JSON. The maximum size is 8 KiB.

FieldRequiredRules
client_nameNoName shown on the approval screen. Maximum 100 characters. If omitted, the name is MCP client.
redirect_urisYesFrom 1 to 10 URIs. Each must be https, or http only when the host is localhost, 127.0.0.1 or [::1].
grant_typesNoOnly authorization_code and refresh_token.
response_typesNoOnly code.
token_endpoint_auth_methodNoOnly none. A confidential client cannot register.

Example:

curl -X POST https://<host-api>/oauth/register \
  -H "Content-Type: application/json" \
  -d '{
    "client_name": "Agente de Ventas Demo",
    "redirect_uris": ["http://localhost:8765/callback"],
    "grant_types": ["authorization_code", "refresh_token"],
    "response_types": ["code"],
    "token_endpoint_auth_method": "none"
  }'

201 response:

{
  "client_id": "7d1f0c2e-0000-4000-8000-000000000001",
  "client_name": "Agente de Ventas Demo",
  "redirect_uris": ["http://localhost:8765/callback"],
  "grant_types": ["authorization_code", "refresh_token"],
  "response_types": ["code"],
  "token_endpoint_auth_method": "none",
  "client_id_issued_at": 1795363200
}

Save the client_id. The client uses it in the authorization and in the token request.

Registration errors:

OAuth code (error)Cause
invalid_redirect_uriNo URI, more than 10, or a URI that does not follow the rules.
invalid_client_metadataThe body is not JSON, the name is too long, the auth method is not none, or a grant_types or response_types value is not valid.
server_errorJAAK could not save the client. Try again later.

3. Authorization

The browser opens GET /oauth/authorize with these parameters:

ParameterValue
response_typecode
client_idThe client_id from the registration.
redirect_uriOne of the registered URIs. It must match exactly.
code_challengeThe PKCE challenge: BASE64URL(SHA256(code_verifier)).
code_challenge_methodS256
scopeScopes separated by spaces. If omitted, all scopes that the role allows are requested.
stateA random value that the client checks on return.

Example URL (line breaks are for reading only):

https://<host-api>/oauth/authorize
  ?response_type=code
  &client_id=7d1f0c2e-0000-4000-8000-000000000001
  &redirect_uri=http%3A%2F%2Flocalhost%3A8765%2Fcallback
  &code_challenge=E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM
  &code_challenge_method=S256
  &scope=signa%3Asend%20signa%3Aread
  &state=abc123

How the server responds:

  • If the client_id does not exist, or the redirect_uri is not registered, JAAK shows an error page. It does not redirect to the client.
  • If response_type is not code, or code_challenge_method is not S256, or scope has an unknown scope, JAAK redirects to the redirect_uri with an OAuth error.
error in the redirectCause
unsupported_response_typeresponse_type is not code.
invalid_requestcode_challenge is missing, or code_challenge_method is not S256.
invalid_scopeA scope does not exist.

4. Google sign-in and consent

After authorization, JAAK sends the person to Google:

  1. The person signs in with their Google account.
  2. The Google account must match an existing JAAK user. JAAK does not create users.
  3. JAAK shows the approval screen with the client name and the scopes it requests.
  4. The person sends POST /oauth/consent with consent_id and decision.
    • decision=approve issues the code.
    • Any other value denies the access. The client receives error=access_denied.
  5. JAAK redirects to the redirect_uri with code and state.

Scopes each role can grant:

RoleScopes it can grant
owner, administrator, managerAll five scopes.
Other rolesOnly signa:templates:read and signa:read.

The token gets the intersection of what the client requested and what the role allows. A requested scope that the role does not have is not granted. There is no error in that case: the token has only the allowed scopes.

Time limits:

StepLifetime
From the request to /oauth/authorize until Google's answer10 minutes
From Google's answer until the person's decision10 minutes
Authorization code1 minute, single use

5. Exchange the code for tokens

The client calls POST /oauth/token with application/x-www-form-urlencoded:

curl -X POST https://<host-api>/oauth/token \
  -d grant_type=authorization_code \
  -d client_id=7d1f0c2e-0000-4000-8000-000000000001 \
  -d code=<code-received> \
  -d code_verifier=<original-code-verifier> \
  -d redirect_uri=http://localhost:8765/callback

200 response:

{
  "access_token": "<access_token>",
  "token_type": "Bearer",
  "expires_in": 3600,
  "refresh_token": "<refresh_token>",
  "scope": "signa:send signa:read"
}
  • The access_token lasts 1 hour (expires_in = 3600). It never lasts longer than the grant.
  • The response has Cache-Control: no-store.
  • The client sends the token in each call: Authorization: Bearer <access_token>.

6. Refresh the token

When the access_token expires, the client calls POST /oauth/token again:

curl -X POST https://<host-api>/oauth/token \
  -d grant_type=refresh_token \
  -d client_id=7d1f0c2e-0000-4000-8000-000000000001 \
  -d refresh_token=<refresh_token>

Refresh rules:

  • The scope field is optional. If sent, it must be a subset of the grant's scopes. If not, the server responds invalid_scope.
  • Each refresh rotates the token. The response contains a new refresh_token. Store it and discard the old one.
  • If the client uses a refresh_token that was already used, JAAK revokes the whole grant. All tokens in that chain stop working.
  • The grant lasts 30 days. After that, the person must sign in again.
  • If the user is suspended or deleted, or the company is suspended, the refresh responds invalid_grant and the grant is revoked.
  • If an administrator revokes the grant from the key list, the token stops working on the server that receives the revocation. Other servers apply it in less than one minute.

Token errors

HTTP statuserrorMeaning
401invalid_clientThe client_id does not exist.
400invalid_grantThe code expired, was already used, or the PKCE verifier does not match. A refresh token is invalid, revoked or reused. A user or company cannot refresh.
400invalid_requestA parameter of the flow is missing.
400invalid_scopeA scope requested in the refresh exceeds the grant.
400unsupported_grant_typeThe grant_type is not authorization_code or refresh_token.
503temporarily_unavailableJAAK cannot respond right now. Retry.

Error response:

{
  "error": "invalid_grant",
  "error_description": "the grant was revoked or expired"
}

Next steps