mcp-server-postgres
Provides a secure, schema-aware PostgreSQL database agent for LLMs, enabling natural language queries and validated SQL execution with strong security guardrails.
README
🚀 mcp-server-postgres
Production-Grade, Schema-Aware PostgreSQL Agent via Model Context Protocol (MCP)
mcp-server-postgres is an intelligent database isolation layer designed to bridge the gap between Large Language Models (LLMs) and production PostgreSQL clusters. Developed with a security-first, schema-aware architecture, it translates natural language requests into validated, optimized, and ACID-compliant SQL.
🏗️ Technical Architecture
graph TD
Client[MCP Client/LLM] -->|JSON-RPC| Server[MCP Server Hub]
Server -->|Validation| Guard[Security Guardrails]
Guard -->|AST/Pattern Scan| SQL[Validated SQL]
SQL -->|Execution| Pool[Connection Pool Manager]
Pool -->|Write Ops| Primary[Primary DB]
Pool -->|Read Ops| Replica[Read Replicas]
Pool -->|Auditing| Audit[Pino Structured Logs]
Key Architectural Pillars
- Zero-Trust Safety Pipeline: Multi-stage validation including "SELECT *" detection, mandatory schema qualification, and SQL injection prevention.
- Intelligent Pagination: Transparent query wrapping to ensure result boundedness (Default: 100 rows) without performance degradation.
- Dynamic Schema Introspection: Enables LLMs to resolve exact table relationships and indexes in real-time.
- Isolation Layers: Implements strict Permission Tiers (
READ_ONLYtoADMIN) to maintain least-privilege principles.
🔒 Security Guardrails (Non-Negotiable)
- Rule-01: No Unbounded Writes: Rejects any
UPDATEorDELETEmissing an explicitWHEREclause. - Rule-04: DDL Confirmation: High-risk schema mutations require a two-step tokenized confirmation flow.
- Masking: Native support for PII redaction on sensitive columns (e.g.,
password_hash,ssn). - Transaction Wrapping: All multi-statement operations are executed inside durable
BEGIN/COMMITblocks.
🛠️ Configuration
Configure your environment variables in .env:
| Variable | Description | Default |
|---|---|---|
DATABASE_URL |
Primary PostgreSQL DSN | Required |
READ_REPLICA_URL |
Optional replica DSN for read routing | null |
PERMISSION_TIER |
READ_ONLY | READ_WRITE | DDL_ALLOWED | ADMIN |
READ_ONLY |
MASKED_COLUMNS |
Comma-separated list of columns to redact | null |
MAX_ROWS |
Hard upper limit for result pages | 100 |
SLOW_QUERY_THRESHOLD_MS |
Warning threshold for latency | 500 |
📋 Toolset API
query(sql, params, page, page_size): Executes validated SQL. Includes auto-pagination.nl_query(request, context): High-level translation interface (requires LLM bridge).schema_inspect(schema, table): Deep introspection of schema metadata.list_tables(schema): Optimized enumeration of current database schema.describe_table(schema, table): Column definitions, indexes, and FK relationships.explain(sql, params): Transparent query analyzer with cost estimation.transaction(operations): Atomic batch execution for complex mutations.confirm_ddl(token): Final execution of verified schema changes.
🚦 Getting Started
Development
npm install
npm run build
npm run start
Integration with Claude Desktop
Add to yours claude_desktop_config.json:
{
"mcpServers": {
"postgres": {
"command": "node",
"args": ["/absolute/path/to/mcp-server-postgres/dist/index.js"],
"env": {
"DATABASE_URL": "postgresql://user:pass@localhost:5432/db",
"PERMISSION_TIER": "READ_WRITE"
}
}
}
}
📈 Observability & Logging
All operations are logged via pino for enterprise observability.
{
"level": "INFO",
"module": "audit",
"operation_type": "QUERY",
"affected_tables": ["public.users"],
"execution_ms": 12.4,
"row_count": 8,
"agent_identity": "mcp-server-postgres"
}
⚖️ License
MIT License. Created with precision by Ismail-2001.
Recommended Servers
playwright-mcp
A Model Context Protocol server that enables LLMs to interact with web pages through structured accessibility snapshots without requiring vision models or screenshots.
Magic Component Platform (MCP)
An AI-powered tool that generates modern UI components from natural language descriptions, integrating with popular IDEs to streamline UI development workflow.
Audiense Insights MCP Server
Enables interaction with Audiense Insights accounts via the Model Context Protocol, facilitating the extraction and analysis of marketing insights and audience data including demographics, behavior, and influencer engagement.
VeyraX MCP
Single MCP tool to connect all your favorite tools: Gmail, Calendar and 40 more.
graphlit-mcp-server
The Model Context Protocol (MCP) Server enables integration between MCP clients and the Graphlit service. Ingest anything from Slack to Gmail to podcast feeds, in addition to web crawling, into a Graphlit project - and then retrieve relevant contents from the MCP client.
Kagi MCP Server
An MCP server that integrates Kagi search capabilities with Claude AI, enabling Claude to perform real-time web searches when answering questions that require up-to-date information.
E2B
Using MCP to run code via e2b.
Neon Database
MCP server for interacting with Neon Management API and databases
Exa Search
A Model Context Protocol (MCP) server lets AI assistants like Claude use the Exa AI Search API for web searches. This setup allows AI models to get real-time web information in a safe and controlled way.
Qdrant Server
This repository is an example of how to create a MCP server for Qdrant, a vector search engine.