AI agents are useful when they can inspect data, but giving them direct database credentials creates a large security problem. A connection string in an agent prompt or a local environment variable is hard to rotate and easy to leak. db-mcp-gateway addresses this by acting as a self hosted MCP gateway between AI agents and databases. The agent sends a query request through MCP. The gateway checks identity and permissions before the database responds.
The repository is available at https://github.com/developerz-ai/db-mcp-gateway.
Credential isolation
The core rule is simple. Database credentials live only in the gateway. An AI agent receives query results, not connection strings. This removes the need to place a database URL on a developer laptop or inside an agent configuration file.
The gateway also avoids credential leakage in logs, errors, and responses. That matters because agents can produce verbose output, and debugging tools may capture more than expected. When the database password never enters the agent context, there is less to leak.
A typical request path looks like this. The AI agent calls an MCP tool. The gateway checks the caller identity and the grant. The gateway opens the database connection using its own credentials. The database returns rows. The gateway returns only the requested data to the agent.
Identity and SSO
db-mcp-gateway uses browser based SSO for authentication. Supported identity providers include Okta, Google Workspace, Entra, Authentik, and Keycloak. The agent does not need an embedded browser. The login flow happens through the user's normal browser session.
Every query can be traced from SSO user to group to grant to audit row. This gives security teams a clear attribution path. If an investigation asks who ran a query, the answer is not a shared service account. It is the identity that authenticated through SSO.
Group based permissions keep access manageable. A platform team can map an SSO group to a grant, and the grant can be reviewed like code.
Grants as code
Permissions are defined in YAML. There is no in band admin UI by design. This makes the permission model reviewable through pull requests and compatible with GitOps workflows.
A grant can specify the group, databases, actions, schemas, row limits, and whether a reason is required.
grants:
- group: backend-devs
databases: [production_postgres]
actions: [query_read]
constraints:
schemas: [public, analytics]
row_limit: 1000
require_reason: true
This example allows the backend-devs group to run read queries against production_postgres. It limits the grant to the public and analytics schemas. It caps rows at 1000 and requires a reason for the query.
The gateway is read only by default. Writes are opt in per grant. Per database least privilege roles can be used, and statement timeouts and row caps are enforced at both the database and the gateway. Grants can also allow or deny schemas and define time windows.
Audit trail
Database access for AI agents needs an audit trail that is useful during an incident review. db-mcp-gateway records query history and stores gateway state and audit logs in PostgreSQL. The MCP tool surface includes get_query_history, which lets operators inspect prior queries.
The audit trail should answer who accessed a database and what query was run. It should also show which grant was used. This supports compliance efforts by providing access control and query logging. It does not make the project a certified compliance product, but it gives teams evidence they can review.
Deployment model
The gateway is distributed as a single Docker container. Configuration is one YAML file. PostgreSQL is used for gateway state and audit logs. The project targets PostgreSQL and MongoDB. MySQL and MSSQL are rejected at boot, which keeps the supported database surface explicit.
A basic run looks like this.
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 deployment style is small enough for a platform team to operate without a large control plane. The important operational control is the configuration file, because it defines grants and identity behavior.
MCP tool surface
The gateway exposes a small set of MCP tools. list_databases shows available databases. describe_schema returns table structure and relationships. sample_table previews data before a full query. run_query executes SELECT queries. explain helps with query analysis. get_query_history exposes the audit trail.
This tool surface is useful for AI agents because it gives them a safe way to explore data before running expensive queries. It also gives platform teams a narrow interface to monitor and restrict.
Who benefits
Platform and SRE teams get a way to enable AI agent database access without spreading credentials. Backend developers can query production data with their own SSO identity instead of a shared password. Security and compliance teams get centralized access control and query logging. They also get user attribution.
The main tradeoff is that the gateway becomes a required component for agent database access. That is usually acceptable when credential isolation and auditability are the goal.
Start with the repository
The project README includes the security model, grant examples, and deployment steps. Start by defining one read only grant for a non production database. Review the audit output. Then expand to production only after the identity and grant model is clear.
The repository is at https://github.com/developerz-ai/db-mcp-gateway. #MCP #DatabaseSecurity
Top comments (0)