Authorization for AI Agents and MCP Tools
An AI agent decides at run time which tools to call and with which arguments. The model can be wrong, and it can be talked into things by text it reads in a ticket, a web page, or a document. Authorization has to sit outside the model, at the point where a tool call is executed, and answer two questions:
- May this agent call this tool with these arguments? A support assistant may read tickets and issue small refunds, but should never run shell commands.
- May the user it acts for do this? An agent acting for someone must not get more access than that person has.
Casbin handles both with one model and one policy, in the same way it handles users. This page shows the rules first, then wires them into a plain tool-calling loop and into an MCP server. The code is Python. The model and policy also work in the other Casbin implementations; in Go, pass the arguments as a struct and use its exported field names in conditions, such as r.args.Amount.
1. Install
pip install pycasbin
2. Write the model
Save this as model.conf:
[request_definition]
r = sub, tool, args
[policy_definition]
p = sub, tool, cond, eft
[role_definition]
g = _, _
[policy_effect]
e = some(where (p.eft == allow)) && !some(where (p.eft == deny))
[matchers]
m = g(r.sub, p.sub) && globMatch(r.tool, p.tool) && (p.cond == '' || eval(p.cond))
r = sub, tool, args: the caller (an agent or a user), the tool name, and the tool's arguments as an object.p = sub, tool, cond, eft: each rule names a role, a tool pattern, an optional condition on the arguments, and whether it allows or denies.g = _, _: agents and users have roles, and roles can inherit from other roles.globMatchmatches tool names with wildcards such astickets.*or*.delete.- The policy effect allows a call if some rule allows it and no rule denies it, so a deny rule always wins.