#231 Enterprise architecture and AI agents 5.5 min
MCP Gateway: Per-Tool Policy for Agent Tools (Python)
Seven tool calls worked directly. Through a small gateway, four were blocked.
Test the edges of the refund range
Copy the code folder first. The policy.py file needs no packages. From the copy, run python -c "import policy; print(policy.check('refund', {'order_id': 7, 'amount': 50.01}))" and then again with 50, 0.01 and 0.009. Predict each answer first. When I ran them, 50 and 0.01 gave (True, 'ok'), and 50.01 and 0.009 were refused with a message such as amount 50.01 is outside 0.01 to 50.
A hint
The rule uses low <= value <= high, so both ends are allowed.
Allow a second email domain
In policy.py, add "example.org" to the domains list of send_email. Then run python -c "import policy; print(policy.check('send_email', {'to': 'x@example.org', 'body': 'hi'}))". Predict the answer before and after your change. When I ran it, it was refused before the change with recipient is outside the allowed domains, and gave (True, 'ok') after.
A hint
The domain is the part of the address after the at sign.
Try to trick the type check
Run python -c "import policy; print(policy.check('refund', {'order_id': 7, 'amount': '20'}))" and then the same call with True as the amount. Predict both answers before running. When I ran the text version, it was refused with amount must be a number. Explain why the check looks at the type before it compares the number with the range.
A hint
Read the isinstance lines at the start of the range check in policy.py. The video says a true or false is refused before any comparison.
Allow the delete tool and see what changes
In policy.py, change the decision for delete_customer from deny to allow. Without running the gateway, run python -c "import policy; print(policy.check('delete_customer', {'customer_id': 3}), policy.allowed('export_all_customers'))". Predict the two answers. When I ran this, the first was (True, 'ok') and the second was False. Write one sentence on what this shows about a policy with no limit on the arguments of a risky tool.
A hint
The delete rule has no range or domains to check, so a decision of allow lets every call through.
These tasks are for you to try on your own machine. Nothing is checked or saved.
What this does not show
A made-up shop and no model. No caller identity, rate limit, output cleaning or human approval, a crude email check, and an audit log that keeps arguments. Nothing here forces calls through the gateway.
Up next in Enterprise architecture and AI agents
More in Enterprise architecture and AI agents
- #229 A Reference Architecture for Enterprise AI Agents, as Code
- #230 Agent Gateway: One Front Door for Models, Tools, Agents
- #233 Delegated Authority: Let an AI Agent Act for a User, Safely
- #234 Authorisation for AI Agents With Policy as Code (Cedar)
- #235 An End-to-End Audit Trail for AI Agent Actions in Python
