Securing AI Agent Database Access with db-mcp-gateway
Introduction
AI agents increasingly need to read data from production databases to provide context-aware responses. Directly embedding database credentials in agents creates a high-risk surface: passwords can be leaked through logs, error messages, or compromised hosts. The db-mcp-gateway solves this problem by acting as a privileged, self-hosted MCP (Model Context Protocol) gateway that isolates credentials, enforces identity-based access, and records a complete audit trail.
Credential Isolation
The gateway stores all database passwords internally. When an AI agent issues a query, it communicates with the gateway over the MCP protocol. The gateway authenticates the request, executes the query against the target database, and returns only the result set. No connection string ever leaves the gateway, and logs never contain credentials. This design eliminates accidental credential exposure on developer laptops or CI pipelines.
AI Agent → MCP Protocol → Gateway → Database
No Credentials Auth Only Least Privilege
SSO-Driven Authentication
db-mcp-gateway integrates with popular SSO providers such as Okta, Google Workspace, Entra, Authentik, and Keycloak. The login flow is browser-based, requiring no embedded browsers inside the agent. Group-based permissions are mapped directly from the identity provider, allowing real-time validation of user membership before a query is executed.
grants:
- group: backend-devs
databases: [production_postgres]
actions: [query_read]
constraints:
schemas: [public, analytics]
row_limit: 1000
require_reason: true
Each grant can restrict schemas, limit rows, and require a justification for every query, providing fine-grained control over what AI agents can read.
Audit Trail
All queries are recorded in a PostgreSQL audit log. The log entry includes the SSO user, group, grant details, timestamp, and the exact SQL statement. This immutable trail satisfies security and compliance teams that need to trace data access back to an individual identity.
The gateway also offers the get_query_history endpoint for quick retrieval of recent activity, enabling rapid investigations when suspicious queries appear.
Config-as-Code Permissions
Permissions live in a YAML file that can be version-controlled and reviewed via pull requests. This GitOps-friendly approach ensures that changes to database access are auditable and reproducible. Because there is no in-band admin UI, the attack surface is reduced.
Deployment Overview
Deploy the gateway as a single Docker container. The image is available on GitHub Container Registry.
# Pull the latest image
Docker 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
The gateway stores its state and audit logs in PostgreSQL. It supports PostgreSQL and MongoDB backends; other databases are rejected at boot, simplifying security decisions.
Use Cases
- Platform/SRE Teams: Provide AI-driven monitoring tools with read-only access to production databases while maintaining strict credential isolation.
- Backend Developers: Query live data from code or notebooks without ever handling passwords.
- Security Officers: Leverage the audit trail and SSO integration to meet internal compliance requirements without needing external certifications.
Conclusion
db-mcp-gateway offers a pragmatic, security-first approach to granting AI agents database access. By keeping credentials inside the gateway, enforcing SSO authentication, and logging every query, it reduces risk and provides the visibility required by modern security programs. The solution is easy to deploy, configure as code, and works with existing identity providers, making it a strong fit for organizations that value both security and operational simplicity.
For more details and to get started, visit the GitHub repository: https://github.com/developerz-ai/db-mcp-gateway
Top comments (0)