# Tokens and connections (laravel-rust-mcp-oauth)

> 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.

- Package: laravel-rust-mcp-oauth (Laravel for Rust, alpha), crates: `laravel-rust-mcp-oauth`
- Maintenance: active; Latest release: 0.1.0 (2026-12-05); Framework: laravel-rust 0.2.x; MSRV: Rust 1.99 or later; Licence: MIT
- Links: [Repository](https://github.com/bowerbird-si/laravel-rust-mcp-oauth), [crates.io](https://crates.io/crates/laravel-rust-mcp-oauth/0.1.0), [docs.rs](https://docs.rs/laravel-rust-mcp-oauth/0.1.0), [Changelog](https://oss.bowerbird.si/mcp-oauth/docs/changelog/), [Contributing](https://github.com/bowerbird-si/laravel-rust-mcp-oauth/blob/8b1d066397c9f1a1cf02d444a910a1c65585c159/CONTRIBUTING.md), [Security policy](https://github.com/bowerbird-si/laravel-rust-mcp-oauth/blob/8b1d066397c9f1a1cf02d444a910a1c65585c159/SECURITY.md)
- Based on: [Laravel MCP](https://github.com/laravel/mcp): The public flow of Mcp::oauthRoutes(): discovery, client registration, an approval screen in your app and bearer tokens for the MCP server.; [Laravel Passport](https://github.com/laravel/passport): The authorization server Laravel MCP builds on. This crate needs no Passport, supports Client ID Metadata Documents and uses opaque, hashed tokens instead of JWTs.; [MCP authorization specification](https://modelcontextprotocol.io/specification/2026-07-28/basic/authorization): The sign-in flow MCP clients expect, with Client ID Metadata Documents preferred over dynamic registration.
- Community package, not affiliated with Laravel.
- HTML version: https://oss.bowerbird.si/mcp-oauth/docs/tokens-and-connections/
- Source: https://github.com/bowerbird-si/laravel-rust-mcp-oauth/blob/8b1d066397c9f1a1cf02d444a910a1c65585c159/docs/tokens-and-connections.md

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

```rust
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:

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

## Events

| Event | When |
| --- | --- |
| `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:

```rust
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.
