Secure Database Access for AI Agents with db-mcp-gateway
Database credentials are a high-value target for any organization. When AI agents need to query production data, exposing connection strings can create a serious security gap. The db-mcp-gateway solves this problem by acting as a trusted intermediary that never leaks credentials while still providing rich query capabilities.
Credential Isolation
All database passwords and URLs are stored only inside the gateway container. AI agents interact with the gateway through the MCP (Model Context Protocol) and receive only the result set of a query. No log line, error message, or response contains a connection string. This design guarantees that credentials never appear on a developer laptop, CI system, or in network traffic beyond the gateway.
SSO-Driven Authentication
The gateway supports popular SSO providers such as Okta, Google Workspace, Entra, Authentik, and Keycloak. Authentication is performed via a browser-based flow; no embedded browsers are required inside the AI agent. Once a user is authenticated, the gateway maps the user to a group and evaluates the YAML-defined grants. Example grant configuration:
grants:
- group: backend-devs
databases: [production_postgres]
actions: [query_read]
constraints:
schemas: [public, analytics]
row_limit: 1000
require_reason: true
The grant limits the agent to read-only queries on specific schemas, caps the number of rows returned, and forces a reason to be supplied for each query. This fine-grained control reduces the attack surface and aligns with least-privilege principles.
Full Audit Trail
Every query passes through the gateway is logged with the following attributes:
- SSO user identity
- Group membership
- Grant used for the request
- Timestamp and duration
- Executed SQL statement
- Result row count
These logs are stored in a PostgreSQL database that the gateway manages. Security officers can query the audit table to answer questions such as “who accessed which table and when”. The audit trail is essential for compliance reporting, even though the gateway itself is not a certified product.
Core MCP Tool Surface
The gateway exposes a small set of commands that AI agents can invoke:
-
list_databases- shows available databases for the authenticated user. -
describe_schema- returns table structures and relationships. -
sample_table- previews a few rows before a full query. -
run_query- safely executes SELECT statements within the defined constraints. -
explain- provides query optimization hints. -
get_query_history- retrieves past audit entries for the user.
All commands respect the same credential isolation and audit logging guarantees.
Deployment Simplicity
Deploy the gateway as a single Docker container. The configuration lives in a YAML file that can be version-controlled and reviewed via pull requests, making it GitOps-friendly. Example Docker run command:
# 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 supports PostgreSQL and MongoDB backends. MySQL and MSSQL are rejected at boot to keep the focus on the most common enterprise databases.
Who Benefits?
- Platform/SRE Teams - can enable AI-driven automation without risking credential exposure.
- Backend Developers - get self-service read access to production data while maintaining strict auditability.
- Security Officers - receive complete attribution for every database query, simplifying compliance audits.
Conclusion
The db-mcp-gateway provides a security-first approach to AI-driven database access. By isolating credentials, integrating with enterprise SSO, and delivering a comprehensive audit trail, it lets organizations leverage AI agents safely. For more details and to contribute, visit the GitHub repository:
Top comments (0)