Are you the author? Sign in to claim
Postgres - Model Context Protocol
A comprehensive Model Context Protocol (MCP) Server built with FastAPI that provides full PostgreSQL database operations through a standardized JSON-RPC 2.0 interface. This server can be integrated with VS Code and other MCP-compatible tools for seamless database management.
| Tool Name | Description | Parameters |
|---|---|---|
echo | Echo back input message | message (string) |
postgres_connection_test | Test PostgreSQL connection | None |
postgres_query | Execute SQL query | query (string), params (array, optional) |
postgres_schema | Get database schema | None |
postgres_table_info | Get table information | table_name (string), schema (string, default: "public") |
postgres_query_analyze | Analyze query performance | query (string) |
postgres_database_size | Get database size info | None |
postgres_table_stats | Get table statistics | table_name (string), schema (string, default: "public") |
postgres_active_connections | Get active connections | None |
postgres_backup_table | Backup table | source_table (string), backup_table (string), schema (string), include_data (boolean) |
postgres_create_index | Create table index | table_name (string), column_names (array), index_name (string, optional), schema (string), unique (boolean) |
postgres_slow_queries | Get slow queries | limit (integer, default: 10) |
postgres_optimize_table | Optimize table | table_name (string), schema (string, default: "public") |
postgres_locks_info | Get database locks | None |
git clone <repository-url>
cd pg-mcp
pip install -r requirements.txt
# or using pyproject.toml
pip install -e .
app/config.py with your PostgreSQL settings:POSTGRES_DB_CONFIG = {
"host": "localhost",
"port": "5432",
"dbname": "your_database",
"user": "your_username",
"password": "your_password"
}
MCP_API_KEY in app/config.py or set environment variable.# Development
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000
# Production
uvicorn app.main:app --host 0.0.0.0 --port 8000
.vscode/mcp.json:{
"mcpServers": {
"postgres-mcp": {
"name": "PostgreSQL MCP Server",
"url": "http://localhost:8000/mcp",
"apiKey": "your-secret-mcp-api-key"
}
}
}
Test connection:
curl -X GET http://localhost:8000/mcp \
-H "Authorization: Bearer your-secret-mcp-api-key"
Execute query:
curl -X POST http://localhost:8000/mcp \
-H "Content-Type: application/json" \
-H "Authorization: Bearer your-secret-mcp-api-key" \
-d '{
"jsonrpc": "2.0",
"id": 1,
"method": "tools/call",
"params": {
"name": "postgres_query",
"arguments": {
"query": "SELECT version()"
}
}
}'
# Run unit tests
pytest test/
# Run comprehensive integration test
python test_postgres_mcp_comprehensive.py
# Test specific functionality
python test_postgres_mcp.py
pg-mcp/
├── app/
│ ├── main.py # FastAPI application entry point
│ ├── mcp.py # MCP server implementation
│ ├── postgres_service.py # PostgreSQL service layer
│ ├── postgres_mock.py # Mock service for testing
│ ├── json_rpc.py # JSON-RPC 2.0 implementation
│ ├── auth.py # Authentication middleware
│ ├── config.py # Configuration settings
│ └── logger.py # Logging configuration
├── test/ # Test files
├── .vscode/
│ └── mcp.json # VS Code MCP configuration
└── pyproject.toml # Project dependencies
{
"jsonrpc": "2.0",
"id": 1,
"method": "tools/call",
"params": {
"name": "postgres_query",
"arguments": {
"query": "SELECT * FROM users WHERE active = $1",
"params": ["true"]
}
}
}
{
"jsonrpc": "2.0",
"id": 2,
"method": "tools/call",
"params": {
"name": "postgres_backup_table",
"arguments": {
"source_table": "users",
"backup_table": "users_backup_20240115",
"schema": "public",
"include_data": true
}
}
}
{
"jsonrpc": "2.0",
"id": 3,
"method": "tools/call",
"params": {
"name": "postgres_create_index",
"arguments": {
"table_name": "users",
"column_names": ["email", "status"],
"index_name": "idx_users_email_status",
"unique": false
}
}
}
MIT License - see LICENSE file for details.
For issues and questions:
Google's universal MCP server supporting PostgreSQL, MySQL, MongoDB, Redis, and 10+ databases
Official MongoDB integration — query collections, run aggregations, inspect schemas
Secure MCP server for MySQL database interaction, queries, and schema management
Run Claude Code as an MCP server so any agent can delegate coding tasks to it