db-mcp-gateway 如何保障 AI 智能體存取資料庫的安全
Securing AI-Driven Database Access with db-mcp-gateway
db-mcp-gateway 是自我託管的 MCP 閘道,將資料庫憑證保留在閘道容器內,並透過存取控制降低 AI 智能體查詢生產資料庫時的憑證外洩風險。AI 智能體經 MCP 傳送查詢,只會收到結果集;閘道可將企業 SSO 身份對應至 YAML 設定中的羣組權限。每項請求會記錄用戶、羣組、資料庫、查詢文本、時間戳記及結果,後端支援 PostgreSQL 和 MongoDB。
Securing AI-Driven Database Access with db-mcp-gateway
In environments where AI agents need to query production databases, exposing credentials is a major risk. The db-mcp-gateway addresses this by acting as a self-hosted MCP (Model Context Protocol) gateway that isolates credentials and enforces strict access controls.
Credential Isolation
All database passwords and connection strings reside only inside the gateway container. AI agents send queries through the MCP protocol and receive only result sets. No log line, error message, or response ever contains a URL or password, eliminating accidental leaks.
Identity & Access Control
Authentication is delegated to enterprise SSO providers such as Okta, Google Workspace, Entra, Authentik, and Keycloak. Users authenticate via a browser flow, and the gateway maps the SSO identity to groups defined in a YAML configuration. Permissions are expressed as grants, for example:
grants:
- group: backend-devs
databases: [production_postgres]
actions: [query_read]
constraints:
schemas: [public, analytics]
row_limit: 1000
require_reason: true
Each query is attributed to the SSO user, the group, and the specific grant, providing full traceability.
Audit Trail
Every request is recorded in a PostgreSQL audit log. The log includes the user, group, database, query text, timestamp, and outcome. The get_query_history endpoint allows teams to retrieve this history for compliance reviews or incident investigations.
SSO Integration
The gateway supports multiple SSO providers without requiring embedded browsers. After authentication, the gateway validates the user in real time, ensuring that revocation or group changes take effect immediately.
Config-as-Code
Permissions are stored as YAML files that live in version control. Changes are reviewed through pull requests, promoting a GitOps workflow. There is no in-band admin UI, reducing the attack surface.
Deployment
Deploying the gateway is straightforward: a single Docker container and one YAML configuration file are required. The gateway runs a lightweight PostgreSQL instance for state and audit logs and supports PostgreSQL and MongoDB as backend databases.
# 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
Who Should Use It?
- Platform/SRE Teams can provide AI-driven database access without credential exposure.
- Backend Developers gain safe, natural-language query capabilities.
- Security Officers receive a complete audit trail and fine-grained access control.
Conclusion
The db-mcp-gateway offers a security-first approach to AI-driven database access. By keeping credentials inside the gateway, integrating with enterprise SSO, and providing immutable audit logs, it helps teams meet compliance objectives while enabling powerful AI workflows. The project is open source and available at https://github.com/developerz-ai/db-mcp-gateway.
來源:DEV Community · dev.to