Secure Database Access for AI Agents with db-mcp-gateway
Secure Database Access for AI Agents with db-mcp-gateway Introduction Artificial intelligence agents increasingly need to read data from production databases to generate accurate responses. Providing that access without exposing credentials is a hard problem for Platform and Security teams. The db-mcp-gateway project offers a self-hosted Model Context Protocol gateway that isolates credentials,…
Artificial intelligence agents require access to production databases to provide accurate responses. However, sharing database credentials with these agents poses a significant security risk. The db-mcp-gateway project offers a solution that isolates credentials, integrates with enterprise single sign-on (SSO), and logs all activity for audit purposes.
At its core, the gateway stores all database passwords internally. AI agents never receive a connection string directly. Instead, the data flow resembles this sequence: AI Agent → MCP Protocol → Gateway → Database. The gateway authenticates the request and forwards only the query results to the agent, eliminating the risk of credential leakage through logs or error messages.
Authentication is handled through SSO providers such as Okta, Google Workspace, Entra, Authentik, or Keycloak. Users log in via the browser, and permissions are defined in a YAML file tied to groups within the SSO system. The YAML config file specifies which groups can access which databases, the permitted actions (like query_read), and constraints like row limits or required reason fields for compliance.
Every request processed by the gateway is logged with crucial details: timestamp, SSO user identifier, group and grant used, database and schema accessed, the query text, and any required reason. These logs reside in a PostgreSQL table inside the gateway container, ensuring an immutable record for security reviews or audits.
The permission model is entirely code-based, residing in a configuration file named config.yaml. To implement a new access rule, update config.yaml, submit a pull request, and follow the standard GitOps workflow of reviewing and merging the change. This approach integrates seamlessly with continuous integration and deployment practices and minimizes the need for an in-band admin interface, thereby reducing the attack surface.
Deployment is straightforward, with the gateway available as a single Docker image. To get started, pull the latest image and run it with your custom configuration:
```
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
```
This container uses its own PostgreSQL instance for state and audit logs, supporting both PostgreSQL and MongoDB databases. It rejects other engines at startup, simplifying security hardening.
The db-mcp-gateway caters to various teams: Platform and SRE teams can grant AI agents read-only access to production databases without storing passwords on the host, while backend developers write natural language prompts that the AI agent executes as safe SELECT statements, protected by the gateway's row limits and schema filters. Security officers benefit from full attribution of each query to an SSO user, enabling detailed investigations and the option to export immutable logs for external audits.
In summary, db-mcp-gateway provides a practical, secure method for AI agents to query production databases. By keeping credentials hidden, enforcing least privilege, and delivering a comprehensive audit log, it aligns well with zero trust security models. Organizations already following GitOps practices and zero trust principles will find this solution fitting for their needs. For further information, clone the repository and follow the provided quick start guide.
Rewritten by urgent.news; may contain errors — check the original.
Source: dev.to