Tokens and connections

laravel-rust-mcp-oauth Alpha Laravel for Rust

Each approval grants an exact set of scopes, and the grant's tokens rotate on every refresh until the person or your app revokes it.

Tokens

An approval reuses a grant only when the account, client and scope set match. A different scope set creates a separate grant, so old tokens keep their scopes.

  • Codes live 5 minutes and work once. A second use revokes every token issued from that code, and any failed exchange (wrong verifier, wrong callback) spends the code too.
  • Access tokens live an hour (MCP_OAUTH_ACCESS_TTL).
  • Refresh tokens live 180 days from issue (MCP_OAUTH_REFRESH_DAYS) and are replaced on every use.

Tokens look like mcp_at_… (access), mcp_rt_… (refresh) and mcp_ac_… (codes), so secret scanners can spot them. Change the prefix with MCP_OAUTH_TOKEN_PREFIX. The database stores only their SHA-256 digests.

When an authorization request omits scope or sends an empty value, the approval includes the default scope and offline_access. A nonempty scope list grants offline access only when it explicitly includes offline_access. The token response includes a refresh token only when the approval grants that scope.

Refresh rotation

Every successful refresh returns a new pair and retires the old refresh token. Omit the scope parameter or send the full approved set in any order. Wider and narrower sets return invalid_scope. For a narrower set, the response explains that the server issues the grant's full scopes.

If an old refresh token comes back:

  • before it expires and within 2 minutes of rotation (MCP_OAUTH_REFRESH_GRACE), while its replacement is still unused, the crate assumes the client lost the answer and retries. It revokes the unused replacement and returns a fresh pair;
  • before it expires, in other cases someone may have stolen it, so the whole grant is revoked and RefreshTokenReplayed fires. The person connects again.
  • at or after expiry the request returns invalid_grant without revoking the current grant.

Revocation

POST /oauth/revoke requires the form fields client_id and token. A missing field returns 400 invalid_request. A live access or refresh token issued to that client revokes the whole grant and returns 200 {}. Unknown, expired and revoked tokens, and tokens issued to another client, return 200 {} and leave the grant unchanged. If the request supplies a client_id that does not own the token, it returns 200 {} and the grant stays active. The endpoint applies its own per-address MCP_OAUTH_TOKENS_PER_MINUTE limit.

A connected-apps page

use laravel_rust_mcp_oauth::McpOAuth;

pub async fn index(Authenticated(user): Authenticated<User>) -> laravel::Result<View> {
    let connections = McpOAuth::connections(&user).await?;
    Ok(view("settings.connections", data! { connections }))
}

pub async fn destroy(Authenticated(user): Authenticated<User>, Params(grant): Params<String>) -> laravel::Result<Redirect> {
    McpOAuth::revoke(&grant, &user).await?; // only the user's own grants; false when not found
    Ok(Redirect::route(index, ()))
}

Each Connection has the grant ID, the app's name and family, the approved scopes, whether it is verified, when it was created and when it was last used. A registration is verified there only when all its current callbacks use HTTPS and verify as the same known client. Registrations with local callbacks are unverified. Revoked grants stay in the list for MCP_OAUTH_RETENTION_DAYS (90).

When a person changes their password, is suspended or deletes their account, end every connection:

McpOAuth::revoke_all(&user).await?;

Events

EventWhen
GrantCreated { grant_id }A person approved a connection
GrantRevoked { grant_id, reason }A grant ended; reason is User, Replay, Prune or Application
RefreshTokenReplayed { grant_id, token_id }An old refresh token came back outside the grace window: worth a warning in your logs

They are framework events, so listen with #[listener]. They fire after the database transaction commits and carry IDs only, never tokens.

There's also a hook for approvals:

McpOAuthServiceProvider::<model::Users<User>>::new()
    .on_grant(|grant| async move { Ok(()) })

Pruning

mcp-oauth:prune runs daily. It removes:

  • expired codes and pending approvals;
  • tokens 30 days after they expire;
  • revoked grants after MCP_OAUTH_RETENTION_DAYS;
  • unused dynamic registrations at least 30 days old;
  • unused metadata-document registrations, including invalidation markers, when the last metadata fetch is older than MCP_OAUTH_METADATA_CACHE_HOURS plus MCP_OAUTH_METADATA_MAX_STALE_HOURS (8 days by default).

Active grants, retained revoked grants and unexpired pending approvals keep their client registrations. A successful fetch can register a client again after pruning.

Turn the schedule off with MCP_OAUTH_SCHEDULE_PRUNE=false when another application prunes the same tables.