How to Configure Pydantic AI MongoDB Approval
You set up pydantic ai mongodb approval by pausing agent tool runs before write calls execute. You pair this pause with strict database roles that limit what the agent can touch. Many teams start by giving their agent a broad database login to speed up testing. When an agent holds wide write rights, one bad prompt can wipe out production records. A strong setup combines strict database roles with a human check on risky actions. How to set up pydantic ai mongodb approval First, you split your
Your coding agents run with your access.
Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.
You set up pydantic ai mongodb approval by pausing agent tool runs before write calls execute. You pair this pause with strict database roles that limit what the agent can touch. Many teams start by giving their agent a broad database login to speed up testing. When an agent holds wide write rights, one bad prompt can wipe out production records. A strong setup combines strict database roles with a human check on risky actions.
How to set up pydantic ai mongodb approval
First, you split your agent functions into safe reads and risky writes. In your agent code, you build distinct tools for reading data and changing data. The agent validates tool inputs against a strict schema before any database call runs. If the model picks a write tool, your app pauses the run right away. The app then sends the checked tool payload to a human reviewer for sign-off.
Next, you lock down the database user that the agent uses to log in. MongoDB grants access through role-based rules that apply to a specific database. A role lets a user run set actions on defined resources down to the collection level. Note that Atlas users and self-hosted users have different built-in roles. Still, both sets of roles are built from the same basic privilege actions.
For agent tools that only fetch data, assign the built-in read role. The read role lets the agent run find, listCollections, listIndexes, dbStats, collStats, and changeStream actions. It covers all non-system collections and the system.js collection in that database. It does not permit insert, update, remove, or dropCollection actions at all. Keep your read tools tied to this role so they can never change records.
What native roles solve and what they leave open
When your agent needs to change data, you might look at the readWrite role. The readWrite role includes all read privileges plus the power to change data. Its allowed actions include insert, update, remove, createCollection, createIndex, dropIndex, and dropCollection. Giving an agent this role lets it do its job, but it creates a big risk. A role that allows updates also allows the agent to drop a whole collection.
Database roles only check who logs in and which collection they can touch. They do not inspect what an individual command does while it is in flight. If an approved tool holds readWrite rights, the database cannot stop a bad remove call. App-level checks also rely on developers writing custom pause code for every new tool. If one developer forgets a check in code, the agent bypasses human review.
Approval Chains & Escalation + MongoDB Authentication & Authorization: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
Native roles also do not rewrite sensitive fields inside the documents that read queries return. A find action on a user collection returns full emails, phone numbers, and card numbers. Those raw values go straight into the model prompt history and external model logs. Teams need a way to enforce query approvals and rewrite sensitive fields outside app code.
Separating read and write paths in agent workflows
A clean agent design uses two separate database logins for its tool set. The default path uses a read role login for routine lookups and summary tasks. These read calls run right away without waiting for a person to click approve. This keeps the agent fast when it only needs to look up facts.
The second path handles updates and deletes through a tight review queue. When the model wants to run an update, the workflow pauses the request. A reviewer checks the exact filter and update fields before the query runs. Even here, you should build a custom role instead of using the full readWrite role. Grant only the insert or update action on the single collection the agent needs. Leave out dropCollection and dropIndex so the agent can never delete whole structures.
Check MongoDB commands and documents on the wire
MongoDB roles and SCRAM login decide who opens a session. They do not check single commands or redact sensitive fields inside returned documents. With hoop.dev, you run an open-source proxy, a sidecar for your MongoDB. It reads the MongoDB wire protocol and checks commands and bulk writes as JSON while passing login through untouched.
Before a command runs, hoop.dev guardrails check its effect and refuse banned drops or writes with MongoDB error code 13. On the way back, hoop.dev data masking rewrites sensitive fields in returned documents that match your rules. Every command and verdict is logged. See the open-source code on GitHub.
Frequently asked questions
Can the MongoDB read role stop an agent from deleting data?
Yes, the built-in read role blocks insert, update, remove, and dropCollection actions. An agent using only the read role can run find queries without risking data loss.
Why is the readWrite role risky for AI agents?
The readWrite role grants insert and update actions alongside remove and dropCollection actions. If a prompt tricks the agent, those broad privileges allow it to delete whole collections.
How granular can MongoDB roles get for agent tools?
MongoDB roles apply to a defined database and can limit actions down to the collection level. You can grant an agent permission to read or update one single collection only.
Related guides
In this post