keycardai-fastmcp
FastMCP integration for Keycard OAuth: protect FastMCP servers with Keycard authentication and run delegated OAuth 2.0 token exchange (RFC 8693) for downstream APIs.
This is the canonical home for the integration; keycardai-mcp-fastmcp is
preserved as a deprecation bridge for callers still on the old name.
Installation
pip install keycardai-fastmcp
Quick Start
from fastmcp import FastMCP
from keycardai.fastmcp import AccessContext, AuthProvider
auth_provider = AuthProvider(
zone_id="abc1234",
mcp_server_name="My Server",
mcp_base_url="http://localhost:8000",
)
mcp = FastMCP("My Server", auth=auth_provider.get_remote_auth_provider())
@mcp.tool()
async def call_external_api(
query: str,
access: AccessContext = auth_provider.grant("https://api.example.com"),
):
token = access.access("https://api.example.com").access_token
return f"Results for {query} (token starts with {token[:8]})"
Declaring the grant as a typed parameter default injects the populated
AccessContext per request; the parameter never appears in the tool's input
schema. Exchange failures are recorded on the AccessContext (check
access.has_errors() / access.get_errors()), never raised. Granting
multiple resources is all-or-nothing: if any exchange fails, the context
carries that resource's error and no tokens.
If you lint with flake8-bugbear or Ruff's B008 rule (function call in
argument default), exempt your tool modules: the call-in-default is the
intended spelling here, the same pattern as FastAPI's Depends. In
pyproject.toml:
[tool.ruff.lint.per-file-ignores]
"src/my_server/tools/*.py" = ["B008"]
Migrating from the decorator form
The decorator form (@auth_provider.grant(...) above the tool) still works
from the same object. Reading the result via ctx.get_state("keycardai") is
deprecated and emits a DeprecationWarning; helpers that only hold the
FastMCP Context can use await AccessContext.from_context(ctx) instead.
The warning fires once per tool, at decoration time (module import). If your
test or CI setup escalates warnings to errors (-W error,
filterwarnings = ["error"] in pytest config), importing a server module
that still uses the old form will raise instead of warn. Either migrate the
tools to the injected-parameter form, or allow this warning explicitly:
filterwarnings = ["error", 'default:Tool .* uses the grant decorator:DeprecationWarning']
Testing
Fake delegated access without patching internals:
from keycardai.fastmcp.testing import mock_access_context
with mock_access_context(access_token="fake_token"):
... # grants resolve to an AccessContext serving fake_token
The bare access_token form serves the token for any resource, so it will
not catch a mistyped resource URL in an access(...) call. Pass
resource_tokens={...} when the test should enforce which resources the
tool reads; resources outside the dict raise ResourceAccessError, matching
production.
Migration from keycardai-mcp-fastmcp
The old package keeps working: from keycardai.mcp.integrations.fastmcp import AuthProvider
emits a DeprecationWarning pointing here and returns the same class. Migrate
when convenient.
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 keycardai_fastmcp-0.5.0.tar.gz.
File metadata
- Download URL: keycardai_fastmcp-0.5.0.tar.gz
- Upload date:
- Size: 40.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.11.30 {"installer":{"name":"uv","version":"0.11.30","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8af5fce5de2ed84cda3f635ab05fdaaffe8ba043d0400c074b3beb00660c546b
|
|
| MD5 |
8722db82e397c90ab105b9f42b75aa10
|
|
| BLAKE2b-256 |
6f02a553c10f2f0a3e5db79e5d2efcd3a26065cdbf1f4ade49e4e64de51dca27
|
File details
Details for the file keycardai_fastmcp-0.5.0-py3-none-any.whl.
File metadata
- Download URL: keycardai_fastmcp-0.5.0-py3-none-any.whl
- Upload date:
- Size: 19.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.11.30 {"installer":{"name":"uv","version":"0.11.30","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0ec1059987bd68ef6530c502b6e1b18aac9e96385b3d251281a27f43af4a6896
|
|
| MD5 |
fd0c9d8616c8f72c94a412a0e05a8d21
|
|
| BLAKE2b-256 |
d11847a864092f25dad7cb8f8c56feebd52593e51839fb7d19681a5ba0a5cdc3
|