Rustem Feyzkhanov · Writing
Own the Tool, Govern the Result
Part 2 of Distributed Agents, Centralized Authorization. How to bring stateful agent policy to Claude Managed Agents without building a stateful MCP gateway.
A support agent picks up ticket T-4821. It reads the ticket: the customer’s name, their contract tier, a description of the outage. It pulls that customer’s service logs, which happen to contain a live API key. Then it files a bug.
Three things should be true about that run:
- The API key never reaches the model. The logs are masked before the agent reads them.
- The internal tracker just works. Filing an internal issue with the customer’s details needs no approval.
- The public tracker needs a person. After the agent has read a customer ticket, posting to the public issue tracker waits for a maintainer, even if the issue text looks harmless.
The third rule is the interesting one. create_issue(tracker="public") is fine at the start of a session and risky ten minutes later. The call didn’t change; what the agent read in between did. The same shape shows up everywhere: a coding agent opening a pull request on an open-source repo after reading private code, or a sales agent emailing a partner after opening the pricing sheet.
Policy engines like OpenAPPA are built for exactly this. Writing the policy is the easy part. The hard part is getting the engine into an agent harness you don’t run.
Where OpenAPPA sits in an agent harness
OpenAPPA needs two hooks in the agent loop and memory between them:
- Before a call, it checks the tool and its arguments against the policy and the session’s current label. The answer is allow, deny or ask a person.
- After a call, it checks the result before the model reads it: admit it, mask it, or withhold it. Then it updates the session’s label (what the session has read) and effects (what it has done).
The memory is what makes rule 3 possible. Without it, every call is judged as if it were the first one in the session.
It’s just as important to know what it does not check. OpenAPPA doesn’t read the model’s reasoning. It trusts each tool’s declared contract and doesn’t watch what the tool actually does. It isn’t a data-loss scanner: masking is a declared sanitizer, not a search for anything sensitive. It doesn’t decide whose credentials run a tool. And it governs only the calls that pass through its hooks.
The rule, in policy
Here are the three rules as an OpenAPPA policy, trimmed to the parts that matter (the full file adds the parameter schema and built-in bindings):
[policy.deployment]
starting_label = { audience = ["public"] }
confined_results = ["host/cma-custom/fetch_logs"]
# Reading a ticket makes the session internal.
[[policy.tool]]
name = "host/cma-custom/read_ticket"
delta = { audience = ["internal"] }
# Raw logs are the customer's own data. They reach the model only masked.
[[policy.tool]]
name = "host/cma-custom/fetch_logs"
delta = { audience = ["self"] }
[[policy.sanitizer]]
name = "mask-secrets"
on = ["tool_output"]
permits.audience = { from = ["self"], to = ["internal"] }
# An issue may only carry data that the tracker's readers may see.
[[policy.tool]]
name = "host/cma-custom/create_issue"
requires = { audience = { contains = ["$tracker"] } }
delta = {}
# A maintainer may approve one public issue at a time.
[[policy.authority]]
name = "maintainer"
permits.audience_missing = ["public"]
delta is what a tool’s result does to the session’s label, and requires is what a call needs from it. The session starts public. Reading the ticket narrows it to internal. Raw logs would narrow it further, to self, so they pass through the masker, which redacts secrets and lets the result in at internal.
OpenAPPA’s appa replay runs a sequence of calls against a policy without executing any tool. For the triage run, trimmed:
create_issue tracker=public allow
read_ticket T-4821 allow (after accepting the narrowing)
fetch_logs T-4821 sanitizer mask-secrets
create_issue tracker=internal allow
create_issue tracker=public authority maintainer
create_issue tracker=public authority maintainer
The same public issue is allowed before the ticket and needs a maintainer after it. One approval releases one call; the next public issue asks again, because the label belongs to the session. Remove the maintainer and both public issues are denied. Remove the masker and even the internal issue is denied: with raw keys in context, the session is self, and no tracker qualifies.
Getting the hooks into Claude Managed Agents
Claude Managed Agents (CMA) runs the agent loop for you. That’s the point of it, and it also means the two hooks aren’t yours. In Part 1 I used CMA’s always_ask permission policy to check calls before they ran. That gave me the first hook, without memory and without a result check: CMA runs the tool and hands the output straight to the model.
To get everything in the policy above, every call and every result has to pass through my code, along with the session and user it belongs to. Both routes below give me that control, and both need persistent policy state. I chose application-owned custom tools because my application already knows the session and its user. A stateful MCP gateway adds another job: reconstructing that context from incoming requests and maintaining the mapping.
A stateful MCP gateway. I could put every tool behind my own MCP server, so it sees each call and each result. In CMA an MCP server is declared with a name and a URL, and its credential comes from a vault attached when the session is created. In this design, the gateway needs a unique token for each agent execution, a vault entry carrying it, and a mapping from token to session to user to policy state. That token mapping is how this gateway recovers context; CMA does not require this particular scheme.
The gateway then has to keep that state, maintain the token mapping and clean up its credentials and mapping when the execution ends. In this design, a maintainer’s review also happens while the MCP request waits. The two policy hooks work, but I have added credential provisioning and a second place to manage session identity.
Application-owned custom tools. CMA never executes a custom tool. It emits agent.custom_tool_use, and my application answers with user.custom_tool_result. The call arrives through the event stream for a session my application created, with an event ID I can use to answer it. My application already maps that session to the user. The service can go straight to the same policy and execution work:
- Checks the call, asking a person when the policy requires it.
- Executes the tool.
- Approves the result: admits it, masks it or withholds it, and updates the session’s label.
For a public issue that needs a maintainer, I can save the pending call, wait for approval, then execute and check the result before replying. CMA already provides the mechanism for answering the call later. The outstanding call can wait without holding an MCP request open.
I still own policy persistence and reliable tool execution. Custom tools let me do that without building a second system for session identity.
What it costs
You now own execution, which CMA used to do for you. A crash between running a tool and returning its result is your problem. The pattern I use: commit a marker before the tool runs, never rerun an execution whose outcome is unknown, and retry only the result check, never the tool. A withheld result never leaves the service. Policy state is stored per conversation and restored before each check, so a fresh process sees the whole history.
The reference app implements this for its own custom tools: persistent per-conversation OpenAPPA state, result masking and single-person review in Web and Slack. A sandbox run of its demo tools passed on November 8 (details). The triage policy above is an illustration, not the app’s configuration. The app has no ticketing integration.
Next: whose credentials?
Once one service executes every tool, it can decide one more thing: which credentials to run it with. The same create_issue means something different under the user’s own account than under a dedicated agent account. A broker like Composio keeps per-user connected accounts and injects the credential at execution time, so the policy can choose the identity from trusted context. The model never names it. That’s the next post.
The takeaway
Stateful agent policy needs two hooks and memory between them. A hosted agent runtime gives you neither by default. A stateful MCP gateway can win them back, at the cost of reinventing session identity. Custom tools give you all three in one service, because the session already waits for you.
How are you bringing your own policy to hosted agent runtimes?
Example policy and replays: examples/. Code: public commit a73787f. A reference implementation, not a production-readiness claim.
#AIAgents #AgentSecurity #Authorization #Anthropic #AWS


