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 increasingly need to query production databases to provide real-time insights. Direct access, however, raises the risk of credential leakage and audit gaps. db-mcp-gateway offers a self-hosted Model Context Protocol (MCP) gateway that isolates credentials, integrates with enterprise SSO, and records every query in an immutable audit trail. This article explains the security model, configuration workflow, and practical benefits for Platform/SRE teams, backend developers, and security officers.

Core Security Principles

Credential Isolation

The gateway stores all database passwords and never returns connection strings to the AI agent. The flow looks like this:

AI Agent → MCP Protocol → Gateway → Database
          ↓ ↓ ↓
      No Credentials Auth Only Least Privilege
Enter fullscreen mode Exit fullscreen mode

Because credentials are never exposed in logs, errors, or responses, the attack surface is dramatically reduced.

Identity & Access Control

db-mcp-gateway relies on SSO-driven authentication. It supports Okta, Google Workspace, Entra, Authentik, and Keycloak through a browser-based flow that does not require embedded browsers. Permissions are expressed as YAML grants, for 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

Each grant is tied to a group, and the gateway validates the user’s membership in real time before allowing a query.

Audit Trail

Every query generates a row in the audit log stored in PostgreSQL. The log records the SSO user, group, grant, query text, and timestamp. This immutable record satisfies many compliance reporting requirements without the gateway itself being a certified product.

Key Features

  • MCP Tool Surface - list_databases, describe_schema, sample_table, run_query, explain, get_query_history.
  • SSO Integration - Browser-flow login for Okta, Google Workspace, Entra, Authentik, Keycloak.
  • Config-as-Code - Permissions live in YAML, reviewed via pull requests, enabling GitOps workflows.
  • Deployment - Single Docker container, one YAML configuration file, PostgreSQL for state and audit logs. Targets PostgreSQL and MongoDB; other databases are rejected at boot.

Deployment Quick Start

# Pull the latest image
 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

After the container is running, AI agents can issue MCP commands such as run_query to retrieve data safely.

Use Cases

Platform / SRE Teams

SRE teams can grant AI agents read-only access to production databases without ever distributing passwords. The audit trail provides full visibility for incident response and post-mortem analysis.

Backend Developers

Developers can query production data using natural language prompts, knowing that the gateway enforces row limits and schema restrictions. No developer needs to store or manage database credentials locally.

Security & Compliance Officers

The immutable audit log and SSO attribution simplify compliance reporting. While the gateway is not certified, it supports compliance initiatives by delivering detailed access records.

Conclusion

db-mcp-gateway addresses the core security concerns of AI-driven database access: credential isolation, identity-based permissions, and comprehensive audit logging. By deploying a single Docker container and configuring YAML grants, teams can enable safe AI queries across PostgreSQL and MongoDB while maintaining strict control over who can see what. The project is open source and available at https://github.com/developerz-ai/db-mcp-gateway.


Ready to protect your database access? Pull the image, configure your SSO provider, and start using the MCP tools today.

Top comments (0)