How to Configure OpenAI Agents SDK MongoDB Prevent Delete

Published
Length
3 min read

Luis Felipe Dias

All posts

You set up an openai agents sdk mongodb prevent delete workflow by limiting database roles. You also restrict the custom tools that your AI agent can call during a run. Many teams start by giving an agent a full admin login to test fast. When a model makes a bad tool call, broad roles let it wipe collections. A safe setup strips out destructive rights before the agent ever runs a query. How to set up openai agents sdk mongodb prevent delete roles MongoDB controls access through role-based rul

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 an openai agents sdk mongodb prevent delete workflow by limiting database roles. You also restrict the custom tools that your AI agent can call during a run. Many teams start by giving an agent a full admin login to test fast. When a model makes a bad tool call, broad roles let it wipe collections. A safe setup strips out destructive rights before the agent ever runs a query.

How to set up openai agents sdk mongodb prevent delete roles

MongoDB controls access through role-based rules that grant actions on specific database resources. A role applies to its home database and can limit actions down to one collection. Self-hosted servers and cloud clusters use different built-in role presets. Still, both systems build their roles from the exact same set of basic privilege actions.

To stop deletes at the database level, you can assign the built-in read role. The read role lets the user run find commands and list collections or indexes. It also allows viewing database stats, collection stats, and change streams on non-system collections. It does not include the remove or drop collection actions, so deletes fail right away.

Some agents must add new records or change existing ones during a normal task. The standard read and write role includes everything in the read role plus change rights. That built-in role bundles insert and update actions with remove and drop collection actions. To allow writes while blocking deletes, you must create a custom database role instead. You grant only find, insert, and update actions on the target collections for the agent. Then you bind that custom role to the login credentials that the agent uses.

How agent tool definitions shape database queries

The agent framework does not talk to your database unless you give it a tool. Developers write Python functions that use a database driver and hand them to the agent. The model reads the function name and description to decide when to call it. If you pass a raw query tool, the model can build any command it wants.

You can narrow this risk by writing strict function tools for each allowed task. For example, you can build one tool that only inserts a single support ticket document. You can build another tool that only updates a status field on an existing record. You leave out any function that calls delete or drop methods in the driver code. Even if a prompt tries to trick the agent, no delete tool exists to call.

MongoDB Authentication & Authorization + OpenAI API Security: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

What native roles solve and what they leave open

Database roles and narrow function tools form a strong base for agent safety. Native roles decide who can log in and which action types that account may run. If a tool or prompt tries to run a remove action, the database blocks it. This stops complete collection drops and unplanned record removal across your data store.

Still, static database roles only check the action type at login and collection scope. They cannot inspect the content of an allowed command while it travels over the wire. An agent with update rights can still overwrite every document with blank fields by mistake. A read query can also pull back raw emails, phone numbers, or card numbers. Static roles cannot rewrite those matched fields in query results before the model reads them. They also cannot pause a risky update command to ask a human for approval first.

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

Can the built-in readWrite role block delete commands in MongoDB?

No, the built-in readWrite role always includes remove and drop collection actions. To allow inserts and updates without deletes, you must define a custom role instead.

Why is hiding the delete function in the agent code not enough?

Code bugs or shared credentials can still expose the database to bad commands. Setting a strict database role ensures the server rejects deletes from any source.

How can an agent damage data if remove actions are blocked?

If an agent has update permissions, a bad filter can overwrite many records at once. It can also replace valid fields with empty values across an entire collection.

The query runs. The emails stay hidden.