Skip to main content

Pluggable OAuth 2.0 + credentials elicitation library for FastMCP servers

Project description

mcp-authkit

CI Coverage Docs PyPI version Python License: MIT

Pluggable authentication library for FastMCP servers built on FastAPI / Starlette.

It handles two independent authentication legs:

  • Leg 1 — session auth — every MCP session is gated behind a standard OIDC provider (Keycloak, Okta, Entra ID, Auth0, …) using JWT bearer tokens. JwtAuthMiddleware validates tokens and publishes the RFC 8414 / MCP-spec well-known endpoints so the MCP client drives the PKCE flow automatically.
  • Leg 2 — tool-level credentials — individual tools can additionally require a third-party OAuth token (OAuthProvider) or a PAT / API key (CredentialsProvider), collected on demand via MCP elicitation.

Installation

pip install mcp-authkit

# Optional Redis storage backend
pip install "mcp-authkit[redis]"

Quick start

Step 1 — Declare current_user

The library is wired together through a single ContextVar that you create and own. Declare it once at module level in your server file and pass it to everything:

from contextvars import ContextVar

# You create this. The middleware writes it; providers read it.
current_user: ContextVar[dict | None] = ContextVar("current_user", default=None)

Python scopes ContextVar per async task automatically, so concurrent requests never interfere.

Step 2 — Add the JWT middleware (Leg 1)

from mcpauthkit.auth_middleware import JwtAuthMiddleware
from mcpauthkit.auth_routes import oauth_meta_router

app.include_router(oauth_meta_router(
    server_base_url=SERVER_URL,
    issuer_url=ISSUER_URL,
    client_id=CLIENT_ID,
))

app.add_middleware(
    JwtAuthMiddleware,
    issuer_url=ISSUER_URL,
    current_user=current_user,
    server_base_url=SERVER_URL,
    open_paths=("/.well-known", "/health", "/register"),
)

Step 3 — Gate a tool behind a third-party OAuth token (Leg 2a)

from mcpauthkit import OAuthProvider

provider = OAuthProvider.from_standard_oauth2(
    name="github",
    authorization_url="https://github.com/login/oauth/authorize",
    token_url="https://github.com/login/oauth/access_token",
    client_id=os.environ["GITHUB_CLIENT_ID"],
    client_secret=os.environ["GITHUB_CLIENT_SECRET"],
    scope="read:user repo",
    redirect_uri=f"{SERVER_URL}/github/callback",
    user_context=current_user,
)
provider.register(app)

@mcp.tool()
@provider.require_token()
async def list_prs(ctx: Context, repo: str) -> str:
    token = provider.get_token()
    ...

Step 4 — Gate a tool behind a PAT / API key form (Leg 2b)

from mcpauthkit import CredentialsProvider

creds = CredentialsProvider(
    name="confluence",
    variables={"pat": {"label": "Personal Access Token", "type": "password"}},
    user_context=current_user,
    server_base_url=SERVER_URL,
)
creds.register(app)

@mcp.tool()
@creds.require_credentials()
async def list_pages(ctx: Context, space: str) -> str:
    pat = creds.get_credentials()["pat"]
    ...

Storage backends

Mode Notes
memory (default) In-process. Tokens lost on restart. Good for development.
file Fernet-encrypted JSON files. Single-instance deployments.
redis Async Redis. Requires mcp-authkit[redis]. Multi-replica deployments.

Select via the TOKEN_STORAGE_MODE env var (memory / file / redis).


Documentation

Full API reference, architecture diagrams, and configuration details: https://masterela.github.io/mcp-authkit/

A complete working example (Docker Compose + Keycloak + Redis, GitHub OAuth tool, Confluence credentials tool): mcp-authkit-quickstart


Contributing

uv sync --group dev
uv run ruff check mcpauthkit/ tests/
uv run mypy mcpauthkit/
uv run pytest --cov=mcpauthkit --cov-report=term-missing -q

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mcp_authkit-0.1.3.tar.gz (35.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mcp_authkit-0.1.3-py3-none-any.whl (41.2 kB view details)

Uploaded Python 3

File details

Details for the file mcp_authkit-0.1.3.tar.gz.

File metadata

  • Download URL: mcp_authkit-0.1.3.tar.gz
  • Upload date:
  • Size: 35.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for mcp_authkit-0.1.3.tar.gz
Algorithm Hash digest
SHA256 b624c69f5fff522ba3c7d733a34d786ff75dc55aedaecf9cd102c3d329dfa6e8
MD5 48cf61f8e17f67ccd77b6971bdc4ab3c
BLAKE2b-256 694e756c6fbfd75e30152d69648a9525157db92e477813b6225318e2fdd8183d

See more details on using hashes here.

Provenance

The following attestation bundles were made for mcp_authkit-0.1.3.tar.gz:

Publisher: release.yml on masterela/mcp-authkit

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mcp_authkit-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: mcp_authkit-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 41.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for mcp_authkit-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 2c019f4720018c48c8ef6c37f5f3314f5d1c35149733e406fca7e2cf990e9780
MD5 f86751d74399978ba518c433517b2a48
BLAKE2b-256 655ed772cea0fde6bfa4be8665ac0228f4ffe644c38cb26a77dc17266a157642

See more details on using hashes here.

Provenance

The following attestation bundles were made for mcp_authkit-0.1.3-py3-none-any.whl:

Publisher: release.yml on masterela/mcp-authkit

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page