Write policies with Cedar
Cedar policy examples for AI agents calling MCP tools
Copy a policy, put in your own IDs, and replay it over last week's calls before it refuses anything.
New to MCP? What is an MCP server? covers the basics first.
These are the Statio policies I'd reach for first when agents call MCP tools. Each one is a Cedar forbid statement you can paste into the policy editor, swap your IDs into, and test before it refuses anything.
Statio saves only forbid policies. Grants on the Permissions page decide which tools an agent may call, and a policy can only narrow that. It runs after the grant check, on every call through the gateway, from people and agents alike.
Prerequisites
- A Statio organization with an MCP server installed and per-tool grants set for the agents that use it. Least privilege for AI agents covers the grants.
- The Admin role to save policies. The Developer role can read them and try drafts.
- For Step 5, the
mcphubCLI and a Statio API key with thepolicies:readscope. Pushing also needspolicies:write, on a key an organization admin created.
Step 1: Find the IDs your policy needs
Teams and servers are Cedar entities, and their IDs are Statio's internal IDs. Team::"support" means a team whose ID is the string support, and yours won't be. Tools are named by attribute, so resource.name == "refund" works as written.
Two ways to get the IDs:
- In the policy editor, choose a template under Start from and pick the server and the exempt team in its form. The Cedar it writes has
Server::"..."andTeam::"..."with the real IDs in them. Copy them out. - Open Try it before saving, choose Logged call, paste the ID of a real call and run it. Show what the policy saw prints the request in Cedar's JSON. The principal shows its
kindandagent_id, plus its teams if the call came from a person, and the tool shows itsserver_id.
The second way is also how you get an agent's agent_id, which you need for Example 8. The client ID on the Agents page is a different value.
Step 2: Pick an example and fill it in
Every example uses angle-bracket placeholders such as "<billing server id>". Replace the whole string, brackets included.
1. Cap refunds, with a team exemption
The rule I'd write first for any agent that can move money. A per-tool grant can give an agent refund, but it can't say how big a refund.
forbid (principal, action == Action::"call", resource in Server::"<billing server id>")
when { resource.name == "refund" }
unless { context.args has amount && context.args.amount <= 50000 }
unless { principal in Team::"<support team id>" };A refund of 40000 goes through and 60000 is refused. A refund with no amount at all is refused too, because the unless can't be satisfied. Members of the support team are exempt. Whole numbers in arguments arrive as Cedar Long, so compare in the unit the tool takes. Here that's cents.
2. Allow only the tools you list on a server
Use this on any server with a long tool list. Servers add tools between versions, and this keeps a new one refused until you add it.
forbid (principal, action == Action::"call", resource in Server::"<github server id>")
unless {
["list_issues", "issue_read", "search_issues", "add_issue_comment"].contains(resource.name)
};merge_pull_request is refused, and so is any tool the server adds later. Calls to other servers are untouched. Grants already do default deny per caller. This puts the same list on everyone, people included, so it also catches a grant that was set too wide.
3. Keep write tools for people
An agent and the person who owns it often hold the same grants. This lets the person merge while the agent can't.
forbid (principal, action == Action::"call", resource in Server::"<github server id>")
when { ["merge_pull_request", "create_pull_request", "issue_write"].contains(resource.name) }
unless { principal.kind == "person" };principal.kind is "person", "agent" or "service". Writing unless kind == "person" refuses service accounts as well as agents, which is usually what you want. Use when { principal.kind == "agent" } if service accounts should keep the tools.
4. Keep agents off a server outside business hours
For a production server nobody watches at night. People can still use it.
forbid (principal, action == Action::"call", resource in Server::"<production database id>")
when { principal.kind == "agent" }
when { context.time.weekday > 5 || context.time.hour < 13 || context.time.hour >= 22 };context.time is UTC, and weekday runs from 1 for Monday to 7 for Sunday. 13:00 to 22:00 UTC is 9 to 6 in New York in summer and 8 to 5 in winter, since UTC has no daylight saving. The weekday is UTC too: 6pm on a Friday in California is already Saturday.
5. Protect tables from writes
On a database server, the gateway parses the SQL before policies run, and context.sql says what it touches. This refuses any write to two tables.
forbid (principal, action == Action::"call", resource)
when { context has sql && (!context.sql.parsed ||
context.sql.write_tables.containsAny(["payments", "ledger_entries"])) }
unless { principal in Team::"<dba team id>" };context has sql keeps the rule off calls that carry no SQL, such as a refund. !context.sql.parsed refuses SQL the parser couldn't read, so a malformed query can't slip past. Table names are lowercase, and each is recorded bare as well as schema-qualified, so "payments" catches public.payments. It also catches a payments table in any other schema. Write "public.payments" if you mean that one only.
To keep agents away from a table entirely, reads included, swap write_tables for tables:
forbid (principal, action == Action::"call", resource)
when { context has sql && (!context.sql.parsed ||
context.sql.tables.containsAny(["users", "api_keys"])) };6. Refuse unfiltered writes and cap read size
The mistakes an agent makes with SQL tend to be big ones: an UPDATE with no WHERE, or a SELECT that pulls a whole table into its context.
forbid (principal, action == Action::"call", resource)
when { context has sql && (!context.sql.parsed ||
context.sql.unfiltered_writes ||
context.sql.statements > 1 ||
(context.sql.kinds.contains("select") &&
(!context.sql.has_limit || context.sql.limit > 1000))) };DELETE FROM orders is refused. UPDATE orders SET status = 'shipped' WHERE id = 7 goes through. A read needs a LIMIT of 1000 or less, even a SELECT count(*), and two statements in one call are refused. The SQL guardrails template writes the same thing, plus a list of functions such as pg_sleep to refuse.
7. Hold SQL writes for approval
Some writes should happen, just not without a person seeing them first. @approval turns a refusal into a hold.
@approval("<dba team id>")
forbid (principal, action == Action::"call", resource in Server::"<production database id>")
when { context has sql && (!context.sql.parsed ||
!["select", "explain", "show"].containsAll(context.sql.kinds)) };Reads go through. An UPDATE waits for someone on the DBA team, or use @approval("admins") for your organization's admins. A SELECT with a DELETE inside a CTE counts as a delete, so it waits too.
The agent gets a 403 that says the call is held, gives the request ID, and tells it to retry the same call with the same arguments once it's approved. After approval that exact call goes through once, within 15 minutes. The approver can't be the person who made the call or the person accountable for the agent. If a policy without @approval also refuses the call, that refusal wins, and so does a policy that errors.
8. Block one tool for one agent
When a single agent misbehaves and you want to stop one tool for it without touching its grants or anyone else's.
forbid (principal, action == Action::"call", resource in Server::"<billing server id>")
when { principal.agent_id == "<agent id>" && resource.name == "refund" };Use principal.agent_id here. An agent's principal ID is its membership ID in the organization, which is a different value, so principal == Principal::"<agent id>" would never match.
9. Keep an argument inside an allowed pattern
String arguments can be matched with like, where * matches anything. Say your mail server's tool is send_email and takes one to address:
forbid (principal, action == Action::"call", resource in Server::"<mail server id>")
when { resource.name == "send_email" }
unless { context.args has to && context.args.to like "*@example.com" };[email protected] goes through. [email protected] and [email protected] are refused, because the pattern has to match the whole string. If the tool takes a list of recipients, this rule doesn't fit as written: to would be a set, and like only works on a string.
Step 3: Try the policy before saving it
The editor's Try it before saving panel runs your draft as if it were enforced, whatever mode you set. It has three tabs.
- Test a call: pick a server and tool and type the arguments as JSON. It runs as you, a person, with your teams looked up. A rule with
principal.kind == "agent"won't match here. - Logged call: run the draft against a real call from the log, by its ID. Use this for agent rules. Show what the policy saw prints the request, which is the quickest way to debug a rule that doesn't match.
- Replay history: run the draft over the last 24 hours, 7 days or 30 days of logged calls, the newest 5,000, on one server or all of them.
Replay is the check I wouldn't skip. It says how many calls the draft would refuse, how many it would hold for approval, and how many of those went through at the time. That last number is what breaks when you enforce. If it's more than you expected, open the examples it lists before saving.
Replay can be approximate. Agent calls are replayed with the agent as the principal and no teams, so a team exemption won't apply to them. Calls whose arguments were cut or redacted when they were logged are flagged too.
Step 4: Save in dry run, then enforce
A policy you save from the editor or push from the CLI starts in dry run. Applying a template from the Templates list saves it enforced, so customize it first if you'd rather watch it.
In dry run a matching call goes through and is logged with the decision would_deny and the policy's name. In the call log it reads "Dry run: refund-cap would have refused this call". Leave it a few days and switch to enforce when every match is one you meant. Changes reach the gateway within 15 seconds.
Once enforced, a refused call gets a 403 that names the policy:
Statio policy "refund-cap" refused this call to 'refund'Every save writes a version. History on a policy shows the diff, and Restore writes the old version back as a new one, so you can undo a rollback too.
Step 5: Keep policies in Git
mcphub policy keeps each policy as a <name>.cedar file. Settings go in // statio: lines at the very top of the file, and everything after them is the policy source, kept byte for byte.
// statio:description Refunds over $500 need the support team
// statio:mode dry_run
forbid (principal, action == Action::"call", resource in Server::"<billing server id>")
when { resource.name == "refund" }
unless { context.args has amount && context.args.amount <= 50000 }
unless { principal in Team::"<support team id>" };The settings are mode (enforce or dry_run), enabled (true or false), description and name. The name defaults to the file name.
export STATIO_API_KEY=<your key>
mcphub policy pull policies/
mcphub policy validate policies/
mcphub policy push policies/ --dry-run
mcphub policy push policies/pull writes your organization's policies into the directory, which is the easiest way to start. validate sends the files to Statio to check, prints file:line:col problems and exits 1 on any. push puts each policy by name and reports it as created, updated or unchanged. Pushing a file that hasn't changed creates no new version.
push --dry-run only prints the plan. It has nothing to do with a policy's dry-run mode. A file without a mode line leaves the server's mode alone, and a new policy starts in dry run. So when a policy is ready, write // statio:mode enforce in the file and push again.
--prune deletes policies that exist in Statio but not in the directory. Leave it off until the directory holds everything, or a pull request will delete the template someone applied in the web app last week.
In CI, run validate on pull requests with a policies:read key from a secret, and push on merge with a policies:write key. validate calls Statio's API, so the job needs network access. Schema warnings, such as a missing has guard, are printed but don't fail the run, so read the output.
Common mistakes
A missing has guard. Tools take different arguments, so every read of context.args needs a has check first. This looks fine:
forbid (principal, action == Action::"call", resource in Server::"<billing server id>")
when { context.args.amount > 50000 };It refuses big refunds. It also refuses every other tool on the server, because reading amount on a call that has none is an error, and an enforced policy that errors refuses the call. The editor warns with "attribute amount in context ... not found". Write context.args has amount && context.args.amount > 50000. Nested fields need a guard at each level: context.args has customer && context.args.customer has id.
A name where an ID belongs. resource in Server::"billing" parses and passes the schema check, then never matches, because no server's ID is billing. The policy does nothing and nothing tells you. If you want to match by name, compare the attribute: resource.server_name == "billing". Teams have no name attribute, so principal in Team::"..." always needs the ID. Step 1 covers getting them. Replay catches this one: a policy that should refuse something and shows 0 is probably pointing at an ID that doesn't exist.
Writing a permit. Statio refuses to save one: "Statio policies only narrow access, so permit has no effect." To let a team past a rule, write the exemption into the forbid as unless { principal in Team::"<team id>" }, as Example 1 does. To give someone a tool they don't have, change their grant.
Questions
principal.kind if a rule should apply to agents only, as Examples 3 and 4 do.Statio policy "refund-cap" refused this call to 'refund'. The call log records the refusal and every policy that refused it.context.sql.parsed is false and the other SQL fields are absent. Rules that start with context has sql && (!context.sql.parsed || ...) refuse it, and a rule that reads another field errors, which refuses it too.forbid statements, and the refusal names the policy. Separate policies are easier to replay and to switch between dry run and enforce one at a time.Conclusion
You have a set of policies to start from, a way to get the IDs they need, and a routine for each new one: try it on a logged call, replay it over last week, save it in dry run, and enforce it once the would_deny matches are all ones you meant. Keep the files in Git once there are more than a few.
Next tutorials: