How to Configure Mastra MongoDB Prevent Delete

Published
Length
3 min read

Luis Felipe Dias

All posts

To set up a mastra mongodb prevent delete rule, restrict the database user role. You must remove drop and delete actions from the credentials your AI agent uses. Most teams start by giving their agent a standard read and write role. That broad role lets the agent run remove and dropCollection commands by mistake. A bad prompt can then wipe out key data in seconds. How a mastra mongodb prevent delete setup works MongoDB controls access through role-based rules on each database. A role grants s

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 a mastra mongodb prevent delete rule, restrict the database user role. You must remove drop and delete actions from the credentials your AI agent uses. Most teams start by giving their agent a standard read and write role. That broad role lets the agent run remove and dropCollection commands by mistake. A bad prompt can then wipe out key data in seconds.

How a mastra mongodb prevent delete setup works

MongoDB controls access through role-based rules on each database. A role grants specific actions on defined resources down to the collection level. Both self-hosted setups and MongoDB Atlas build roles from these same privilege actions. When an agent connects, the database checks the assigned role before running any command.

The built-in read role grants access to all non-system collections in a database. It allows actions like find, listCollections, listIndexes, dbStats, collStats, and changeStream. It does not include insert, update, remove, or dropCollection actions. If your agent only needs to look up records, the read role stops deletes.

Many AI workflows also need to add or edit records during a run. Teams often pick the built-in readWrite role for these agents. Still, readWrite includes remove and dropCollection alongside insert and update. To let an agent write data without deleting it, you need a custom role. You build this role by granting find, insert, and update while leaving out remove. You also leave out dropCollection and dropIndex so the agent cannot remove structures.

What native roles solve and what they miss

Database roles do a great job of blocking full delete commands at the collection level. If a user lacks the remove action, the database rejects delete calls right away. This setup stops an agent from dropping a collection or running a delete query. It gives your team a solid first line of defense against bad tool calls.

Still, static database roles only check who logs in and which action runs. They do not inspect the content of individual statements in flight. An agent with update rights can still overwrite fields with blank values. That bad update destroys data just like a delete command would. Static roles also cannot require a human to approve risky writes before they run.

MongoDB Authentication & Authorization + End-to-End Encryption: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Native roles also cannot filter sensitive fields inside the documents an agent reads. When an agent runs a find query, the database returns the full document. If those records hold emails, phone numbers, card numbers, or national IDs, raw data flows back. Masking rewrites matched fields in results before they reach the agent. Finally, shared database credentials make it hard to tie each query to one user.

Best practices for safer agent tool design

Good database roles work best when paired with strict tool definitions in your app. Give your AI agent narrow tools that only run one specific task at a time. Do not give the agent a general tool that runs raw database queries. Instead, build separate tools for finding records, adding new items, and updating single fields.

You should also split database users by the job each agent performs. A research agent should log in with the built-in read role only. An intake agent should use a custom role that only permits insert and find. Keeping these accounts separate limits the blast radius if an agent makes a mistake. Review your database logs often to spot rejected remove calls or strange update patterns.

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

Does the MongoDB readWrite role allow agents to delete data?

Yes, the built-in readWrite role includes both the remove and dropCollection privilege actions. If a Mastra agent uses a readWrite account, it can delete documents and collections. You must use the read role or a custom role to block deletes.

Can I block deletes on just one collection in MongoDB?

Yes, MongoDB roles can grant privilege actions down to the collection level. You can create a custom role that allows find and insert on one collection. That same role can omit the remove action so records stay safe.

How can an agent lose data if remove is blocked?

An agent with update access can still replace existing document fields with empty values. Database roles check the command type, not the payload values inside an update statement. Stopping bad updates requires checking and approving commands before they hit the database.

The query runs. The emails stay hidden.