The idea
A language model can request any tool call, including one it was talked into by text it read. The decision to run a tool therefore should not belong to the model. It belongs to ordinary code that sits between the request and the tool, and that code trusts only facts supplied by the application: who the current tenant is and which scopes the session holds.
tool_allowed(call, tenant, scopes, policy) is that check. The policy dict maps each tool name to the one scope it requires. A call passes only if the tool is in the policy, the tenant named in the call equals the session tenant, and the required scope is in scopes. Anything missing or unknown is denied.
How it works
- Read the tool name with
call.get('tool'), so a missing field becomes None instead of raising. - Check that the name is a key of
policy, because a tool the policy does not list is denied. - Compare
call.get('tenant') with the trusted tenant argument, which rejects calls aimed at another tenant's data. - Look up the scope that the tool requires and check that it is in
scopes. - Return
True only when all three checks pass and False in every other case.
What it costs
- Per call
- O(number of scopes)
- Memory
- one small set
The check is two dictionary lookups and one set membership test, so it adds almost nothing to the cost of a tool call.
Where it is used
- OAuth 2.0 APIs check that an access token carries the required scope before serving a request, such as
repo for GitHub repository endpoints. - AWS IAM evaluates every API request against policies and denies it unless a statement explicitly allows the action.
- Coding agents such as Claude Code check allow and deny rules from a settings file before running a tool the model asked for.
Watch out for
- Trusting a tenant or scope written inside the model's
call: only the tenant and scopes arguments come from the application. - Allowing tools that are missing from
policy, as if an unlisted tool needed no scope. - Indexing
call['tool'] directly, so a malformed call raises an exception instead of returning False.