How to Configure Claude Code MongoDB Prevent Delete
To set up claude code mongodb prevent delete rules, assign a restricted database role. You must also control the shell commands and external tools the agent runs. Engineering teams often connect AI coding agents to live databases to speed up debugging. When developers use broad credentials, a single bad prompt can wipe out whole collections. Restricting what the agent can run protects your data from accidental loss. How to set up claude code mongodb prevent delete in native roles Claude Code
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 claude code mongodb prevent delete rules, assign a restricted database role. You must also control the shell commands and external tools the agent runs. Engineering teams often connect AI coding agents to live databases to speed up debugging. When developers use broad credentials, a single bad prompt can wipe out whole collections. Restricting what the agent can run protects your data from accidental loss.
How to set up claude code mongodb prevent delete in native roles
Claude Code reaches external systems and databases through Model Context Protocol servers. It can also run local shell commands through its built-in Bash tool. In manual mode, the Bash tool asks for human approval before running most commands. Only a small built-in list of safe read commands runs without a prompt. When teams set up database tools, vendor docs advise using a read-only database user. This stops the queries the agent runs from changing or deleting any data.
On the database side, MongoDB controls user access through role-based rules. A role grants the right to run specific actions on a chosen database. You can scope these rules down to the individual collection level. The built-in read role lets a user view non-system collections in that database. It allows actions like find, listCollections, listIndexes, dbStats, collStats, and changeStream. It leaves out the insert, update, remove, and dropCollection actions. By giving the agent a user with the read role, you block all deletes.
What native roles solve and what they leave open
Sometimes an AI agent needs to add test records or update existing documents. In those cases, a strict read role will block the agent from working. Teams often switch the database user to the built-in readWrite role instead. The readWrite role includes everything in the read role plus data change rights. Its allowed actions include insert, update, remove, createCollection, createIndex, dropIndex, and dropCollection. Once you grant readWrite, the database user can delete records and drop collections again.
You can build custom roles in MongoDB that leave out remove and dropCollection actions. Even then, static database roles only check who logs in at the start. They do not inspect the content of individual queries while they run. A role with read access still lets the agent pull sensitive customer records. If a collection holds emails, phone numbers, or card numbers, queries return them raw. Also, static credentials sit inside local config files on the developer machine. If the agent uses the Bash tool, it can read those local files. Native roles cannot mask fields in results or require approval for one risky action.
Claude Code Security + MongoDB Authentication & Authorization: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
How to keep local agent sessions safe
Start by creating a dedicated database user just for your AI coding agent. Never share your personal admin account or app service credentials with the agent. Give that new user the built-in read role whenever the task only needs lookups. If the agent must write data, build a custom role for one collection. Leave the remove and dropCollection actions out of that custom role.
Next, check the Model Context Protocol servers you add to your local setup. Only connect servers that you trust and have reviewed with your security team. Keep the Bash tool in manual mode so you review every shell command. Watch for commands that call database client binaries or read local credential files. Combine tight database roles with careful human review to keep your data safe.
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, inline in the MongoDB wire protocol. On every command an AI agent or engineer runs against production, hoop.dev decides to pass, mask, block, or hold it for human review.
Before a command runs, hoop.dev guardrails check its effect and refuse banned drops or writes with MongoDB error code 13, or hold risky commands for approval. On the way back, hoop.dev data masking rewrites sensitive fields in returned documents in memory, and every verdict is logged. See the open-source code on GitHub.
Frequently asked questions
Does the MongoDB read role stop all delete actions?
Yes, the built-in read role only allows viewing data and collection stats. It does not include the remove or dropCollection actions needed to delete data.
How does Claude Code connect to a MongoDB database?
The agent connects to databases through Model Context Protocol servers or local shell commands. Vendor docs advise putting a read-only user in the database connection string.
Why is the readWrite role risky for AI coding agents?
The readWrite role grants the remove and dropCollection actions along with insert and update. An agent with this role can delete documents or drop a whole collection.
Related guides
In this post