pymcp-kit ships optional authentication and authorization hooks. They are off by default and are activated through create_app(...) or MiddlewareConfig(...).
TokenMapAuthenticator maps static Bearer tokens to Principal objects:
from pymcp import create_app
from pymcp.security import TokenMapAuthenticator
app = create_app(
authn=TokenMapAuthenticator(
{
"secret-token": {
"subject": "alice",
"display_name": "Alice",
"roles": ["ops"],
"scopes": ["tools:*", "tasks:*"],
"claims": {"tenant": "acme"},
}
}
),
require_authn=True,
)With require_authn=True, unauthenticated requests receive 401.
RuleBasedAuthorizer evaluates ordered allow/deny rules:
from pymcp import create_app
from pymcp.security import RuleBasedAuthorizer, TokenMapAuthenticator
authn = TokenMapAuthenticator(
{
"secret-token": {
"subject": "alice",
"roles": ["ops"],
"scopes": ["tools:*", "tasks:*"],
}
}
)
authz = RuleBasedAuthorizer(
{
"default_effect": "deny",
"hide_unauthorized_capabilities": True,
"hide_unauthorized_tools": True,
"rules": [
{
"methods": ["initialize", "ping", "tools/list", "tasks/list", "tasks/get", "tasks/result"],
"allow_subjects": ["alice"],
"effect": "allow",
},
{
"methods": ["tools/call"],
"tool": "deploy_*",
"allow_roles": ["ops"],
"effect": "allow",
},
],
}
)
app = create_app(
authn=authn,
authz=authz,
require_authn=True,
)from pymcp.security import RuleBasedAuthorizer, load_json_config
authz = RuleBasedAuthorizer(load_json_config("authz_policy.json"))hide_unauthorized_capabilities: remove disallowed capability fragments frominitializehide_unauthorized_tools: filtertools/listoutput to only visible toolsauth_exempt_paths: skip auth middleware on selected HTTP routes
When authentication is enabled, task ownership follows the authenticated principal rather than the raw session ID. That matters for tasks/list, tasks/get, tasks/cancel, and tasks/result.
- keep token maps or authn setup in
config.py - keep JSON authz policies in a separate file
- pass
authn,authz, andrequire_authnintocreate_app() - use capability and tool filtering if clients should not even discover restricted functionality