mandat (Python)
Authorization, delegation and audit for AI agent actions: the Python guard for Mandat.
Protect an MCP server written with the official Python SDK (mcp ≥ 2.0). Every tool call is then:
- authorised by a policy on the call's arguments, delegation chain and session taint (
refund ≤ €50 → auto · €50–500 → ask a human · > €500 → deny); - attributed to the human, the agent and any sub-agents, from a short-lived, single-task Mandat token;
- audited in Mandat's hash-chained ledger.
It uses the same policy language, the same decisions and the same control plane as the TypeScript SDK. A shared conformance suite (spec/conformance/policy.json) checks that both SDKs decide every case identically.
pip install "mandat[mcp]"
Requires Python ≥ 3.10. The core depends only on cryptography; the mcp extra adds the MCP SDK integration. The control plane (STS, approvals, revocation, ledger, console) is the mandat npm package: npm i -g mandat && mandat init && mandat serve.
Protect an MCPServer
from mcp.server.mcpserver import MCPServer
from mandat import create_guard
from mandat.mcp import auth_settings, protect, token_verifier
guard = await create_guard(
resource="https://mcp.example.com/mcp", # token audience: this server
control_plane="http://127.0.0.1:7400", # `mandat serve`
api_key=SERVICE_KEY, # API key with the "service" role (.mandat/credentials.json)
)
server = MCPServer("billing", token_verifier=token_verifier(guard), auth=auth_settings(guard))
protect(server, guard) # register tools after this line
@server.tool()
async def refund(order_id: str, amount: float, currency: str) -> str:
"""Refund (part of) an order."""
...
app = server.streamable_http_app() # serve with uvicorn
token_verifier(guard)makes the SDK's bearer middleware accept only valid, unrevoked Mandat task tokens for this server. Anything else gets401with aresource_metadatachallenge.auth_settings(guard)publishes the RFC 9728 metadata.protect(server, guard)wraps each tool registered afterwards with policy, approvals, session taint, tool pinning and a ledger record. It keeps the function signature, so the SDK derives the same input schema. Sync tools still run on a worker thread.- Denied calls return
isError: truewith Mandat's decision in_meta["mandat/decision"], the same shape as the TypeScript SDK. Calls that need approval wait up toapproval_waitseconds for a human, then ask the agent to retry.
A complete server is in examples/server.py.
Without MCP
auth = await guard.verify(bearer_token) # raises AuthError (-> HTTP 401)
result = await guard.execute(auth, "refund", args, lambda: do_refund(**args)) # raises GuardDenied
compile_policy(doc).evaluate(input) gives you the decision engine on its own, with no I/O.
Behaviour
- Fail-safe: the guard checks its API key at startup and refuses to run with a rejected key. If the control plane is unreachable, it starts from the last policy snapshot, and
financial/destructivetools fail closed after 15 s without revocation updates. - Revocations are polled every 2 s, so a revoked agent is locked out within seconds.
- Ledger records are shipped in the background, batched and delivered at least once, so the request path never waits for them.
- Rejected tokens are recorded (
auth.rejected), at most one record per reason every 10 s.
Development
cd python
uv venv && uv pip install -r requirements-dev.txt -e .
python -m unittest discover -s tests -v # end-to-end tests need Node >= 23.6 (they run the real control plane)
If a system proxy is configured (common on Windows), set NO_PROXY=127.0.0.1,localhost. The httpx client used by the MCP SDK otherwise sends local requests through the proxy.
License: Apache-2.0.
Metadata
Release files for mandat 0.1.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 |
|---|---|---|---|---|
| mandat-0.1.0.tar.gz | 38.5 kB | With provenance | Details for mandat-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| mandat-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for mandat-0.1.0-py3-none-any.whl |
Total release size: 73.3 kB
Release files / mandat-0.1.0.tar.gz
| Download URL | mandat-0.1.0.tar.gz |
|---|---|
| Size | 38.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9796b941f3f00a12e7c9f7169777e05435d63616ec0092be883853f56ec2ad2a
|
|
BLAKE2b-256 checksum How to use checksums |
d9cf6b1fdf0b2831c66df003e1d3600e6149b2a97e437fffeb56f637dffe8225
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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 Nov 4, 2026.
Transparency logRelease files / mandat-0.1.0-py3-none-any.whl
| Download URL | mandat-0.1.0-py3-none-any.whl |
|---|---|
| Size | 34.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e8e2989a8d603d72a1fe5cdbf4530ae5109b5257cd0dabe9cab933cb6b29652c
|
|
BLAKE2b-256 checksum How to use checksums |
e58938b80379a050b7344b937691f3de9a2640a5205a14ede1f7635e1ee60d70
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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 Nov 4, 2026.
Transparency logSecurity for mandat 0.1.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
All 2 files in this release have been uploaded with provenance metadata.
Attestation for mandat-0.1.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 Nov 4, 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.