Pluggable OAuth 2.0 + credentials elicitation library for FastMCP servers
Project description
MCP Authentication Kit - mcp-authkit
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.
JwtAuthMiddlewarevalidates 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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mcp_authkit-0.2.0.tar.gz.
File metadata
- Download URL: mcp_authkit-0.2.0.tar.gz
- Upload date:
- Size: 35.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9df39465da1f04f54a69fa1194716c43a372eae5a7473a56de027e9f3f8f9afe
|
|
| MD5 |
f482d6b37e1ed08cb6208e262e96af47
|
|
| BLAKE2b-256 |
e843fbb77165873906215a067970f7b645c6bf8f6c42693172b0be377b8eaa8a
|
Provenance
The following attestation bundles were made for mcp_authkit-0.2.0.tar.gz:
Publisher:
release.yml on masterela/mcp-authkit
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
mcp_authkit-0.2.0.tar.gz -
Subject digest:
9df39465da1f04f54a69fa1194716c43a372eae5a7473a56de027e9f3f8f9afe - Sigstore transparency entry: 1588726422
- Sigstore integration time:
-
Permalink:
masterela/mcp-authkit@796599c2f04ef04961d668f287274060e92a9079 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/masterela
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@796599c2f04ef04961d668f287274060e92a9079 -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file mcp_authkit-0.2.0-py3-none-any.whl.
File metadata
- Download URL: mcp_authkit-0.2.0-py3-none-any.whl
- Upload date:
- Size: 41.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
da269eccad14086be7e8b0c40b941b1090617abb1321176e18df1b1beb0c9aea
|
|
| MD5 |
35ac561f9de3f2bf04ee3ad075b53ff6
|
|
| BLAKE2b-256 |
f4dfda90d6cc62a34b67c66fe11a9531bcfe36455966208dd390bad4881af981
|
Provenance
The following attestation bundles were made for mcp_authkit-0.2.0-py3-none-any.whl:
Publisher:
release.yml on masterela/mcp-authkit
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
mcp_authkit-0.2.0-py3-none-any.whl -
Subject digest:
da269eccad14086be7e8b0c40b941b1090617abb1321176e18df1b1beb0c9aea - Sigstore transparency entry: 1588726453
- Sigstore integration time:
-
Permalink:
masterela/mcp-authkit@796599c2f04ef04961d668f287274060e92a9079 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/masterela
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@796599c2f04ef04961d668f287274060e92a9079 -
Trigger Event:
workflow_dispatch
-
Statement type: