How to Configure Pydantic AI MongoDB Audit Log

Published
Length
3 min read

Luis Felipe Dias

All posts

You set up a pydantic ai mongodb audit log by pairing database roles with event capture. Your app tracks tool calls while the database records user logins and actions. Teams often give an AI agent one shared database user for all tasks. Broad roles let the agent read or change every collection in that database. When an agent runs a bad query, the log only shows the shared role. How a pydantic ai mongodb audit log works in practice A good audit setup starts with how MongoDB grants access throu

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 a pydantic ai mongodb audit log by pairing database roles with event capture. Your app tracks tool calls while the database records user logins and actions. Teams often give an AI agent one shared database user for all tasks. Broad roles let the agent read or change every collection in that database. When an agent runs a bad query, the log only shows the shared role.

How a pydantic ai mongodb audit log works in practice

A good audit setup starts with how MongoDB grants access through roles. A role gives a user the right to run actions on set resources. Each role works on the database where you define it. You can scope a role down to a single collection. MongoDB Atlas and self-hosted setups use different built-in roles. Still, both build their roles from the same basic privilege actions.

When you connect a Pydantic AI agent, you pick a role for its tools. The built-in read role lets the agent read all non-system collections. It also allows reading the system.js collection in that database. This role permits actions like find, listCollections, listIndexes, dbStats, collStats, and changeStream. It blocks insert, update, remove, and dropCollection actions.

Some AI workflows need to write data or save state. The readWrite role includes all read rights plus data change actions. It allows insert, update, remove, createCollection, createIndex, dropIndex, and dropCollection. On the app side, Pydantic AI runs structured tool functions for the model. Your code can log the tool inputs before the database driver runs the query. The database audit system then records the role action on the target collection.

Why native roles and logs leave an operational gap

Native roles and logs solve the basic problem of who can log in. They verify the database user and record that a collection action happened. Setting the read role stops an agent from dropping a collection or deleting rows. This setup works well for static apps with fixed queries.

Still, AI agents build and pick query parameters on the fly. A database role checks who logs in, not what a query returns in flight. Even a read role can pull every row from a sensitive collection by default. If a collection holds emails, phone numbers, or card numbers, the agent gets them. Native role checks apply to the whole collection, not to specific fields inside documents.

AI Audit Trails + Audit Log Integrity: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Joining app logs with database logs also creates hard work for your team. The database log shows the shared app user, not the human who asked the agent. Your team must match timestamps between Python logs and database logs to trace an event. If the agent has readWrite rights, it can drop indexes or remove whole collections. Native roles cannot pause a dangerous command for human approval before it runs.

How to scope agent permissions for cleaner audit trails

Start by splitting read tools and write tools into separate database users. Give the read tools a role scoped only to the collections they need. Never give a general agent the readWrite role across a whole database. If an agent only needs to add logs, build a custom role for one collection.

Pass a trace ID from the user request into your app log entry. This step helps your security team link user prompts to database queries during reviews. Even with tight roles, you still need controls that inspect queries and mask fields like national IDs.

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

Can a MongoDB role restrict an AI agent to one collection?

Yes, MongoDB roles can grant access down to the collection level. You can create a role that allows read actions on just one target collection.

What is the risk of giving a Pydantic AI agent the readWrite role?

The readWrite role allows insert, update, and remove actions across the database. It also lets the agent run dropCollection and dropIndex, which can break production apps.

Does the built-in read role block data changes from AI tool calls?

Yes, the read role only allows read actions like find and listCollections. It does not include insert, update, remove, or dropCollection rights.

The query runs. The emails stay hidden.