DEV Community

Cover image for Building Production MCP Servers & Tool-Calling AI Agents: Sandboxing, Schemas & Approval Gates

Building Production MCP Servers & Tool-Calling AI Agents: Sandboxing, Schemas & Approval Gates

Muhammad Ali Mughal on October 30, 2026

Most "AI agent" demos stop at the happy path: the model picks a tool, the tool returns JSON, everyone claps. Production is different. The moment an...
Collapse
 
robertpaschal profile image
Odinaka Robert Nnamani •

The argument-hash binding for tier-3 approvals is the part to copy. A changed refund amount after approval should kill the token, not slide through.

Collapse
 
muhammadalimughal profile image
Muhammad Ali Mughal •

Agreed, any drift in the arguments should invalidate the token. I also bind the hash to the tool name, tenant and an idempotency key, so a legitimate retry reuses the same approved token while a changed amount or target forces a fresh approval.

Collapse
 
robertpaschal profile image
Odinaka Robert Nnamani •

Binding the idempotency key in there is the useful bit. A retry should not need a second human to say yes again.

Thread Thread
 
muhammadalimughal profile image
Muhammad Ali Mughal •

Exactly. The approval token is scoped to the idempotency key with a short TTL, so a retry inside that window reuses the same approval instead of paging a human again. The gateway also records every execution against that key, so a duplicate side effect gets rejected outright rather than re-approved.

Thread Thread
 
robertpaschal profile image
Odinaka Robert Nnamani • • Edited

That gateway recording detail is the part that sells it. A duplicate getting rejected outright beats a second approval prompt every time.

Thread Thread
 
muhammadalimughal profile image
Muhammad Ali Mughal •

Agreed. A rejected duplicate also leaves a clean audit trail, so when something does retry you can see it was caught instead of wondering whether it ran twice. Thanks for pushing on this, Odinaka.

Collapse
 
lycore profile image
Lycore Development •

Separating read, write and destructive tiers in the gateway is the right shape, since the tool worker never decides its own permissions. One thing that has helped in practice is binding the approval to a hash of the exact arguments, so an approver can't approve one refund and have a different amount executed after a retry. I'd also log the retrieved context ids alongside each tool call, because that makes it much easier to trace which document carried an injected instruction. How do you handle approval timeouts: fail closed, or let the agent re-propose?

Collapse
 
muhammadalimughal profile image
Muhammad Ali Mughal •

Thanks, this is a great addition. Binding the approval to an argument hash is exactly the kind of guarantee a gateway should enforce, and logging the retrieved context ids per call is something I'd now call essential for tracing injected instructions back to their source document. On timeouts, I fail closed with a short TTL (around 15 minutes). An expired approval cancels the pending call outright. The agent can re-propose, but only as a brand-new request with a fresh argument hash and the original context ids attached, so the approver sees it as a new decision. Nothing ever auto-retries past an expired approval.

Collapse
 
paul_irolla_6b5ae261d0224 profile image
Paul Irolla •

@muhammadalimughal The tool schema list rides along on every turn, so it is easy to overlook. Filter it: expose only the tools for the current role, so a read-only session never sees the write schemas. That cuts tokens and shrinks what a prompt injection can reach. Log schema tokens per request before optimizing anything else.

Collapse
 
alexshev profile image
Alex Shev •

Sandboxing, schemas, and approval gates work best as one chain: schema validation limits the request, the sandbox limits the environment, and approval limits the authorized effect. The verification step after execution is the missing fourth link that proves the chain produced the intended state.

Collapse
 
muhammadalimughal profile image
Muhammad Ali Mughal •

Agreed, and verification is the link teams most often skip. In practice I have the gateway re-read the target state after execution (the refund record, the file diff, the affected row count) and compare it with what the approved arguments said should happen. A mismatch gets logged as an incident instead of a silent success, which closes the loop the approval opened.

Collapse
 
paul_irolla_6b5ae261d0224 profile image
Paul Irolla •

A server sends its whole schema list with each request unless it filters it, so measure that before anything else. Does yours filter it?

Collapse
 
muhammadalimughal profile image
Muhammad Ali Mughal •

Good call, the tool schema list is easy to overlook because it rides along on every turn. Filtering is the approach I recommend: expose only the tools scoped to the current tenant, role and approval tier, so a read-only session never even sees the write or destructive schemas. That cuts tokens and also shrinks what a prompt injection can reach for. Keep descriptions tight and log schema tokens per request so you can see the cost before optimizing anything else.