How to Configure Pydantic AI MongoDB Read Only Access

Published
Length
3 min read

Luis Felipe Dias

All posts

To set up pydantic ai mongodb read only access, assign the MongoDB read role to the database user your agent uses. Then pass that user account into the database client inside your agent tools. Many teams start by reusing a shared app account for their agents. That account often carries the readWrite role on the database. With readWrite, a bad prompt or agent bug can insert, update, or remove records. It can even drop collections or indexes. How to set up pydantic ai mongodb read only access M

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 read only access, assign the MongoDB read role to the database user your agent uses. Then pass that user account into the database client inside your agent tools. Many teams start by reusing a shared app account for their agents. That account often carries the readWrite role on the database. With readWrite, a bad prompt or agent bug can insert, update, or remove records. It can even drop collections or indexes.

How to set up pydantic ai mongodb read only access

MongoDB controls what a user can do through role-based access rules. Each role grants a set of actions on defined resources. A role applies to the database where you define it. You can also scope a role down to a single collection. Note that MongoDB Atlas and self-hosted setups have different built-in role lists. Both still build their roles from the same core privilege actions.

For an agent that only needs to fetch data, use the built-in read role. The read role grants access to all non-system collections in that database. It also grants access to the system.js collection. This role lets the user run actions like find, listCollections, and listIndexes. It also allows dbStats, collStats, and changeStream actions. It blocks insert, update, remove, and dropCollection actions at the database engine.

Next, wire this restricted user into your Pydantic AI setup. Pydantic AI lets you pass a database client through its dependency system. You define tool functions that the model can call during a run. Inside those tool functions, the client runs queries using the read role credentials. If the model tries to write or delete data, MongoDB rejects the command.

Why role checks alone leave gaps for AI agents

Assigning the read role stops an agent from changing or deleting your data. The database blocks write actions like createCollection, createIndex, and dropIndex right away. This is a strong first step for any AI tool setup. Still, database roles only check who logs in and which action they call. They do not inspect what the query returns in flight.

A read role gives access to every field in every allowed collection. When an agent runs a find action, it gets the full document back. Those documents often hold sensitive fields like emails, phone numbers, or card numbers. They might also hold national IDs. Once the tool reads those fields, they flow straight into the model prompt.

Auditor Read-Only Access + AI Model Access Control: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Native roles also lack fine controls over how an agent queries data. A valid read user can still run huge scans across a collection. Plus, shared app credentials make it hard to trace which agent ran a query. Teams need a way to inspect queries and mask sensitive fields in results.

Scoping agent tools for safer database queries

You can reduce risk further by how you build your agent tools. Do not give the model a tool that runs raw database queries. Instead, build small tool functions for specific tasks. Have the tool accept clear inputs validated by a strict schema. Inside the tool, select only the specific document fields the task needs.

Even with scoped tools, managing credentials across many agents takes work. Storing static database keys in app files creates rotation headaches for your team. Combining least-privilege MongoDB roles with runtime controls keeps data much safer.

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

What is the difference between the MongoDB read and readWrite roles?

The read role allows actions like find, listCollections, and listIndexes on non-system collections. The readWrite role adds write actions like insert, update, remove, and dropCollection.

Can I limit a MongoDB role to a single collection for my agent?

Yes, MongoDB roles apply to a database and can scope down to the collection level. This lets you limit your agent to only the collections its tools need.

Does the MongoDB read role hide sensitive fields from Pydantic AI?

No, the built-in read role grants access to full documents in allowed collections. To hide emails, phone numbers, or card numbers, you must rewrite matched fields in results.

The query runs. The emails stay hidden.