DEV Community

developerz.ai
developerz.ai

Posted on

What Actually Lands in Your Audit Trail When an AI Agent Queries a Database

The problem with agent database access

Most teams adopting AI agents for operational work hit the same wall. The agent needs data, and the easiest way to give it data is a connection string. Once that string exists in an agent config, an environment file, or a prompt, you have a credential outside your control that points at production.

You can rotate it, but you cannot easily answer the question a security reviewer will ask six months later. Which human caused this query to run?

db-mcp-gateway is a self-hosted MCP gateway that deals with this by removing the credential from the agent entirely. The agent talks MCP to the gateway. The gateway holds the database connection and returns results. This article walks through the audit trail it produces, because the audit trail is what makes the rest of it defensible.

The identity chain

Every query resolves through the same chain:

SSO user -> group -> grant -> audit row
Enter fullscreen mode Exit fullscreen mode

The user authenticates through your identity provider. The gateway supports Okta, Google Workspace, Entra, Authentik and Keycloak, and the login is a browser flow triggered by the first query, so nothing is embedded in the client.

Group membership maps to a grant. A grant is a YAML block that names the databases the group may reach, the actions allowed, and the constraints applied:

grants:
  - group: backend-devs
    databases: [production_postgres]
    actions: [query_read]
    constraints:
      schemas: [public, analytics]
      row_limit: 1000
      require_reason: true
Enter fullscreen mode Exit fullscreen mode

That chain matters for access reviews. When someone asks who could have read a table, the answer is a group membership plus a YAML line plus a git history, not a shared read-only account used by eight people.

What the audit row contains

The gateway writes a per-query audit record containing:

  • user identity and timestamp
  • the full SQL query executed
  • the reason provided, when the grant requires one
  • row count and duration
  • success or failure status

Two details are worth calling out. The row count and duration turn a log into something you can use for capacity and abuse questions, not just forensics. The reason field, when you require it, puts intent in the record at write time. Reconstructing intent from a query string after the fact is guesswork.

For retention and routing, hot storage is PostgreSQL, with optional archive to S3, GCS or Azure, plus OTLP, syslog or stdout forwarding. Retention policies are configurable. That means you can keep 90 days in Postgres for incident work and ship the long tail to object storage for compliance retention without changing the gateway.

Constraints are part of the record too

An audit trail is more useful when the allowed envelope is also in git. Constraints sit next to the grant:

constraints:
  statement_timeout_ms: 5000
  row_limit: 10000
  allowed_schemas: [public]
  require_reason: true
  time_windows:
    - days: [Mon-Fri]
      hours: [9-17]
      timezone: America/New_York
Enter fullscreen mode Exit fullscreen mode

This is a five second statement timeout, a ten thousand row cap, a single allowed schema, a required reason, and business hours only in New York time. A query that would produce a bulk export is not blocked by a policy document. It is blocked by the constraint the gateway enforces at the database and the gateway layer.

All grants default to SELECT only. Writes require an explicit query_write action, and schema changes are never permitted through the gateway.

Honest note on compliance

This matters if you are picking tooling for a regulated environment. db-mcp-gateway is not certified against SOC 2, HIPAA, ISO 27001 or any other framework. What it gives you is the technical substrate a compliance program builds on: complete per-query audit trails, user attribution for all access, permission review through git pull requests, and configurable data access logging and retention. Your certification is still your own work.

Wiring it into an agent

The client side is deliberately boring. For Claude Code, with no database URL or password anywhere in the client config:

claude mcp add --transport http db-gateway --scope project https://db.internal.acme.com
Enter fullscreen mode Exit fullscreen mode

The gateway exposes tools including list_databases, describe_schema, sample_table, run_query, explain and get_query_history. That last one is neat, because it lets the agent read its own audit trail, which is often exactly what a developer wants when reviewing what an agent did during an incident.

Deployment is a single Docker container with one YAML config file:

docker pull ghcr.io/developerz-ai/db-mcp-gateway:1.1.1
docker run -p 8080:8080 -v $(pwd)/config.yaml:/app/config.yaml ghcr.io/developerz-ai/db-mcp-gateway:1.1.1
Enter fullscreen mode Exit fullscreen mode

The current release is v1.1.1, stable and in production use, with images for linux/amd64 and linux/arm64. It targets PostgreSQL and MongoDB. MySQL and MSSQL are rejected at boot. It is MIT licensed.

Where it does not fit

Do not reach for this on a single-admin setup where direct database access is fine, or for very high frequency automated query workloads where a purpose-built path may serve you better. It is built for the case where humans and agents both need scoped access to shared databases and someone will eventually ask who did what.

That question is the whole point. Credentials stay in the gateway. The answer to who ran the query lives in a row with a name on it.

Top comments (0)