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
Metadata
Release files for mcp-authkit 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| mcp_authkit-0.3.0.tar.gz | 36.5 kB | With provenance | Details for mcp_authkit-0.3.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| mcp_authkit-0.3.0-py3-none-any.whl | Python 3 | none | any | Details for mcp_authkit-0.3.0-py3-none-any.whl |
Total release size: 78.8 kB
Release files / mcp_authkit-0.3.0.tar.gz
| Download URL | mcp_authkit-0.3.0.tar.gz |
|---|---|
| Size | 36.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
925bad3184ca8226f06dd2a38668ba00beb32cd93e7bfe195608328b1c41be07
|
|
BLAKE2b-256 checksum How to use checksums |
ce16d82fda282b3227535023bce63a4eda0348f78e3be9974060f643af38cc6d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Aug 11, 2026.
Transparency logRelease files / mcp_authkit-0.3.0-py3-none-any.whl
| Download URL | mcp_authkit-0.3.0-py3-none-any.whl |
|---|---|
| Size | 42.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
271e15f04a63f448a1f800cc7925899e11cc12909fcdb499908aea41be2f3858
|
|
BLAKE2b-256 checksum How to use checksums |
cd0e4807d7b1ab2cf3038a111ed4b39727c4d5d80bc07a73673a88db6514e689
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Aug 11, 2026.
Transparency logSecurity for mcp-authkit 0.3.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for mcp_authkit-0.3.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Aug 11, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.