How to Configure Pydantic AI MongoDB Prevent Delete

Published
Length
3 min read

Luis Felipe Dias

All posts

You set up pydantic ai mongodb prevent delete rules by restricting database roles and agent tools. Assign a role without remove privileges and omit delete functions from your agent code. Many teams start by giving their AI agent a standard read and write role. That broad role lets a bad prompt remove documents or drop a whole collection. How to set up pydantic ai mongodb prevent delete in your app Start at the database layer before you write any agent tool logic. MongoDB uses role-based rules

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 prevent delete rules by restricting database roles and agent tools. Assign a role without remove privileges and omit delete functions from your agent code. Many teams start by giving their AI agent a standard read and write role. That broad role lets a bad prompt remove documents or drop a whole collection.

How to set up pydantic ai mongodb prevent delete in your app

Start at the database layer before you write any agent tool logic. MongoDB uses role-based rules to grant actions on specific databases and collections. A role applies to the database where you define it. It can grant permissions down to a single collection.

If your agent only needs to read records, assign the built-in read role. The read role lets the agent run find, listCollections, listIndexes, dbStats, collStats, and changeStream actions. It leaves out insert, update, remove, and dropCollection actions by default.

Sometimes your agent must add or edit records while blocking deletes. The built-in readWrite role includes insert and update, but it also includes remove and dropCollection. To allow writes and block deletes, build a custom role in MongoDB. Grant that role find, insert, and update actions on the target collection. Leave out the remove and dropCollection actions.

Next, lock down the Python tools you give to your Pydantic AI agent. Define separate tool functions for reading, adding, and updating documents. Do not give the agent a raw query runner that accepts arbitrary command dictionaries. Check all tool inputs with strict schema models so the agent only passes safe fields.

Where native roles fall short for AI database queries

Database roles and Python schemas solve part of the problem. They decide which user account logs in and which code functions run. They do not inspect what individual database commands do in flight.

First, maintaining custom roles across dozens of collections takes constant work. Teams often fall back to the built-in readWrite role to move fast. Once an agent has that role, a subtle bug in tool code can wipe records.

AI Data Exfiltration Prevention + MongoDB Authentication & Authorization: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Second, blocking deletes does not protect against bad updates. An agent with update permissions can overwrite a document with blank values. From a data loss angle, a bad update hurts just as much as a delete.

Third, database roles do not filter sensitive fields inside returned documents. When an agent runs a find action, the database returns full records. Those records often hold emails, phone numbers, card numbers, or national IDs. Static roles cannot rewrite matched fields in results before they reach the model.

Add command guardrails and field masking beside MongoDB

Collection roles and IP rules control who connects to MongoDB. Yet an active client or agent session can still run a pipeline that writes data or reads raw personal fields. In practice, hoop.dev runs beside your database as a sidecar for your MongoDB. It checks every wire command and result document without changing your drivers or credentials.

Using hoop.dev MongoDB controls, guardrails refuse banned commands before they run. Data masking rewrites matched fields in returned documents in memory. Furthermore, hoop.dev holds SOC 2 Type II, is MIT licensed, and generates evidence for GDPR, HIPAA, and PCI DSS programs. View the project on GitHub.

Frequently asked questions

Does the MongoDB read role block all delete commands?

Yes, the built-in read role only allows read actions like find and listCollections. It does not grant the remove or dropCollection actions on the database.

Can the built-in readWrite role block deletes while allowing inserts?

No, the built-in readWrite role bundles insert, update, remove, and dropCollection together. You must create a custom role if you want to allow inserts without remove privileges.

Why should I restrict agent tools if the database role already blocks deletes?

Restricting agent tools keeps the model from wasting steps on commands that will fail. Clear tool schemas guide the agent toward valid read, insert, and update actions.

Teams that document these role rules and review logs each month catch config drift early. Clear ownership for each role keeps live systems simple to run and easy to audit across the whole team.

The query runs. The emails stay hidden.