DEV Community

developerz.ai
developerz.ai

Posted on

Secure Database Access for AI Agents with db-mcp-gateway

Secure Database Access for AI Agents with db-mcp-gateway

Introduction

AI agents need to read data from production databases, but exposing credentials creates a security risk. The db-mcp-gateway solves this problem by acting as a broker that never reveals database URLs to the agent. This article explains the security model, configuration, and integration steps for platform, SRE, and security teams.

Credential Isolation

All database passwords are stored only inside the gateway container. When an AI agent issues a query, the gateway authenticates the request, runs the query against the target database, and returns only the result set. No connection string appears in logs, error messages, or response payloads. This isolation eliminates the chance of credential leakage from developer machines or CI pipelines.

SSO-Driven Authentication

The gateway supports Okta, Google Workspace, Entra, Authentik, and Keycloak for single sign-on. Users authenticate through a browser flow, and the gateway validates the token in real time. Permissions are granted per group, allowing fine-grained control such as read-only access to specific schemas or row limits. The configuration is expressed in YAML, making it easy to review and version control.

Audit Trail

Every query is recorded with the SSO user, group, grant, and timestamp. The audit log lives in a PostgreSQL table inside the gateway, providing a complete history for compliance reviews. Because the gateway enforces constraints like row caps and schema filters, the audit trail reflects the exact data accessed.

Config-as-Code

Permissions are defined in a single YAML file. Example:

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

Changes are applied via a pull request, ensuring that any modification is reviewed before taking effect.

Deployment

The gateway runs in a single Docker container. Pull the image and start it with your configuration file:

# Pull the latest image
ocker pull ghcr.io/developerz-ai/db-mcp-gateway:1.1.1

# Run with your config
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 gateway supports PostgreSQL and MongoDB backends and rejects MySQL or MSSQL at startup.

Conclusion

By keeping credentials inside the gateway, integrating with corporate SSO, and providing a detailed audit log, db-mcp-gateway offers a practical way to let AI agents query production databases safely. The solution aligns with compliance initiatives without claiming formal certification. For more details, visit the GitHub repository.

https://github.com/developerz-ai/db-mcp-gateway

Top comments (0)