Skip to content

access_token

The access token of the request being served, exposed via a contextvar.

This module is deliberately transport- and auth-stack-agnostic: it imports no HTTP framework and no OAuth model, so mcp.server.request_state (and any tool handler) can read the caller's token without loading either. On HTTP transports the contextvar is populated by mcp.server.auth.middleware.auth_context.AuthContextMiddleware, which also re-exports both names under their long-standing import path.

get_access_token

get_access_token() -> AccessToken | None

Get the access token from the current context.

Returns:

Type Description
AccessToken | None

The access token if an authenticated user is available, None otherwise.

Source code in src/mcp/server/auth/access_token.py
31
32
33
34
35
36
37
38
def get_access_token() -> mcp.server.auth.provider.AccessToken | None:
    """Get the access token from the current context.

    Returns:
        The access token if an authenticated user is available, None otherwise.
    """
    auth_user = auth_context_var.get()
    return auth_user.access_token if auth_user else None