How to Configure Pydantic AI MongoDB Least Privilege
To set up pydantic ai mongodb least privilege, create a dedicated database user with collection-scoped read roles. Then, pass that user to your agent tool functions so the model only runs allowed queries. Many teams start by giving their AI agents a shared admin login to move fast. Broad permissions let a bad prompt drop collections, change records, or read private user tables. How to set up pydantic ai mongodb least privilege MongoDB controls access through role-based rules. A role grants th
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.
To set up pydantic ai mongodb least privilege, create a dedicated database user with collection-scoped read roles. Then, pass that user to your agent tool functions so the model only runs allowed queries. Many teams start by giving their AI agents a shared admin login to move fast. Broad permissions let a bad prompt drop collections, change records, or read private user tables.
How to set up pydantic ai mongodb least privilege
MongoDB controls access through role-based rules. A role grants the right to run specific actions on defined database resources. Each role applies to the database where you define it. You can scope a role down to a single collection for tight control. Self-hosted servers and MongoDB Atlas offer different built-in role presets, but both use the same privilege actions.
For most AI agents, the built-in read role is the safest starting point. The read role lets the user read all non-system collections in that database. It allows actions like find, listCollections, listIndexes, dbStats, collStats, and changeStream. It blocks write actions like insert, update, remove, and dropCollection.
If your agent must save state, the readWrite role adds data modification rights. It includes insert, update, remove, createCollection, createIndex, dropIndex, and dropCollection. Because readWrite allows dropping collections, you should build a custom role instead. A custom role can allow insert and update on one collection while blocking remove and dropCollection.
On the app side, Pydantic AI lets you define typed tools that the model can call. You pass the database client into the agent run context as a dependency. Instead of letting the model write raw queries, give it narrow tool functions. Each tool validates the input arguments with a schema before running a fixed query.
What native roles solve and what stays exposed
Database roles and typed agent tools solve the first half of the security problem. They stop the agent from touching collections outside its assigned database scope. They also block write actions when the login only holds the read role.
Still, native database roles only check who logs in and which collection they touch. They do not inspect what individual queries return in flight. A read role on a customer collection returns every field in every matched document. If a document holds emails, phone numbers, card numbers, or national IDs, the query returns them.
Least Privilege Principle + MongoDB Authentication & Authorization: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
Static database credentials also create operational risk for engineering teams. The app must store a database username and password in its runtime settings. If an engineer copies those settings to test locally, that key leaves your secure server. Also, standard database logs often tie every agent query to the same service account. When something goes wrong, you cannot tell which user prompt triggered a heavy find action.
Scoping agent tools for safer database queries
You can reduce risk further by shaping how your agent tools query the database. Always set a hard document limit on every find action inside your tool functions. This stops a vague prompt from pulling thousands of documents into the model context window.
You should also define strict field projections inside your tool code. A projection tells the database to return only the specific fields the agent needs. Exclude fields that hold emails, phone numbers, card numbers, or national IDs by default. Finally, validate all filter inputs so the model cannot pass query operators that scan the whole collection.
Inspect MongoDB wire traffic in flight
Built-in MongoDB roles govern collection access at setup time. They cannot score command risk per call or mask sensitive document values on the fly. By design, hoop.dev is an open-source proxy that sits next to MongoDB as a sidecar for your MongoDB. It reads every command from mongosh, drivers, or agent tools.
First, hoop.dev guardrails block bad deletes, drops, and write pipelines. Next, hoop.dev data masking rewrites sensitive values in returned documents based on your rules and logs each decision. Explore the repo on GitHub.
Frequently asked questions
Should I give a Pydantic AI agent the MongoDB readWrite role?
No, the built-in readWrite role includes dropCollection and remove actions. Build a custom role that grants only insert or update on the specific collection the agent needs.
Can MongoDB roles restrict access to a single collection?
Yes, MongoDB roles can grant access down to the collection level. You can create a custom role that allows the find action on one collection and nothing else.
How do I keep sensitive fields out of Pydantic AI prompts?
Use field projections in your tool queries to select only safe fields. You can also use data masking to rewrite emails, phone numbers, and card numbers in query results.
Related guides
In this post