Dev.to Security 🔐 Cybersecurity 👁 0 📖 2 min read

How We Built a Self-Hosted MCP Gateway That Keeps Database Credentials Off Developer Laptops

When we started letting AI agents query production databases, the first question from our security team was obvious: where do the credentials live? The answer we settled on: nowhere the agent can see them. The

When we started letting AI agents query production databases, the first question from our security team was obvious: where do the credentials live?

The answer we settled on: nowhere the agent can see them.

The Problem

Most database tools for AI agents work by injecting a connection string into the agent's context. That means the model, the orchestration layer, and often the developer's laptop all have access to production database URLs. One leaked log line or copied prompt and your credentials are exposed.

We wanted a different model. The agent should send a query intent, not a connection string. The gateway should handle authentication, authorization, and execution - then return only results.

Architecture Overview

AI Agent → MCP Protocol → db-mcp-gateway → Database

The gateway is a single Docker container. It speaks MCP on one side and PostgreSQL/MongoDB wire protocols on the other. Credentials are configured once in a YAML file that lives on the server. The agent never sees them.

SSO-Driven Identity

Every request flows through an SSO provider first. We support Okta, Google Workspace, Entra, Authentik, and Keycloak using a browser-based flow - no embedded browsers required. The user authenticates, the gateway validates their group membership, and permissions are resolved from a grants file:

grants:
  - group: backend-devs
    databases: [production_postgres]
    actions: [query_read]
    constraints:
      schemas: [public, analytics]
      row_limit: 1000
      require_reason: true

This means a developer in the backend-devs group can run SELECT queries against the production PostgreSQL database, limited to the public and analytics schemas, capped at 1000 rows, and only if they provide a reason. The gateway enforces all of this at the database level with statement timeouts and row caps.

Audit Trail by Default

Every query creates an audit row linking the SSO user, their group, the grant that authorized it, the database, the exact SQL, and the execution time. No separate logging infrastructure required - it's built into the gateway's PostgreSQL backend.

MCP Tool Surface

The gateway exposes six MCP tools:

  • list_databases - shows databases the user has grants for
  • describe_schema - table structure and relationships
  • sample_table - preview data before writing a query
  • run_query - execute SELECT queries (writes are opt-in per grant)
  • explain - query plan analysis
  • get_query_history - personal and team audit trail

Deployment

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

One container. One config file. PostgreSQL for gateway state and audit logs. Targets PostgreSQL and MongoDB (MySQL and MSSQL are rejected at boot).

Why This Matters

If you're running AI agents that need database access, the credential problem is real. db-mcp-gateway solves it by keeping credentials in your infrastructure, tying every query to a real identity, and giving you an audit trail that satisfies compliance requirements - without claiming certifications we don't have.

The code is open source: https://github.com/developerz-ai/db-mcp-gateway

MCP #DatabaseSecurity #AI #Compliance #PostgreSQL

📰 Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.