An MCP server that exposes relational databases—PostgreSQL and MySQL, and also SQLite—to AI agents. It provides natural language to SQL query support, alongside direct SQL tooling for interacting with databases. The repository is licensed under Apache 2.0 and targets Python 3.11+.
🛠️ Key Features
Natural language to SQL query support
Direct SQL tooling
Database coverage: SQLite, PostgreSQL, MySQL
🚀 Use Cases
Querying relational databases via AI using natural language
Running direct SQL against supported database engines
⚡ Developer Benefits
Works with an MCP Model Context Protocol setup
Designed for Python 3.11+ environments
Clear focus on relational database integration
⚠️ Limitations
Source data does not specify tool names, configuration options, or supported schema/metadata behavior
An MCP (Model Context Protocol) server that exposes relational databases (PostgreSQL/MySQL) to AI agents with natural language query support. Transform natural language questions into SQL queries and get structured results.
Features
Multi-Database Support: Works with PostgreSQL and MySQL
Natural Language to SQL: Convert plain English queries to SQL using HuggingFace transformers
RESTful API: Clean FastAPI-based endpoints for database operations
Safety First: Read-only operations with query validation and result limits
Docker Ready: Complete containerization with Docker Compose
Production Ready: Health checks, logging, and error handling
AI Agent Friendly: Designed specifically for AI agent integration
API Endpoints
Endpoint
Method
Description
/health
GET
Health check and service status
/mcp/list_tables
GET
List all available tables with column counts
/mcp/describe/{table_name}
GET
Get detailed schema for a specific table
/mcp/query
POST
Execute natural language queries
/mcp/tables/{table_name}/sample
GET
Get sample data from a table
Quick Start
Option 1: Docker Compose (Recommended)
Clone and start the services:
bash
git clone https://github.com/Souhar-dya/mcp-db-server.git
cd mcp-db-server
docker-compose up --build
Test the endpoints:
bash
# Health check
curl http://localhost:8000/health
# List tables
curl http://localhost:8000/mcp/list_tables
# Describe a table
curl http://localhost:8000/mcp/describe/customers
# Natural language query
curl -X POST "http://localhost:8000/mcp/query" \
-H "Content-Type: application/json" \
-d '{"nl_query": "show top 5 customers by total orders"}'
Option 2: Local Development
Prerequisites:
Python 3.11+
PostgreSQL or MySQL database
Install dependencies:
bash
pip install -r requirements.txt
Set environment variables:
bash
export DATABASE_URL="postgresql+asyncpg://user:password@localhost:5432/dbname"# or for MySQL:# export DATABASE_URL="mysql+pymysql://user:password@localhost:3306/dbname"
Run the server:
bash
python -m app.server
Sample Database
The project includes a sample database with realistic e-commerce data:
customers: Customer information (10 sample customers)
orders: Order records (17 sample orders)
order_items: Individual items within orders
order_summary: View combining order and customer data
Natural Language Query Examples
The server can understand various types of natural language queries:
bash
# Get all customers
curl -X POST "http://localhost:8000/mcp/query" \
-H "Content-Type: application/json" \
-d '{"nl_query": "show all customers"}'# Count orders by status
curl -X POST "http://localhost:8000/mcp/query" \
-H "Content-Type: application/json" \
-d '{"nl_query": "count orders by status"}'# Top customers by order value
curl -X POST "http://localhost:8000/mcp/query" \
-H "Content-Type: application/json" \
-d '{"nl_query": "top 5 customers by total order amount"}'# Recent orders
curl -X POST "http://localhost:8000/mcp/query" \
-H "Content-Type: application/json" \
-d '{"nl_query": "show recent orders from last week"}'
# PostgreSQL
DATABASE_URL=postgresql+asyncpg://user:pass@localhost:5432/mydb
# MySQL
DATABASE_URL=mysql+pymysql://user:pass@localhost:3306/mydb
# PostgreSQL with SSL
DATABASE_URL=postgresql+asyncpg://user:pass@localhost:5432/mydb?sslmode=require
### Database Connection Examples
```bash
# PostgreSQL (local or cloud)
DATABASE_URL=postgresql+asyncpg://user:password@host:5432/dbname
# MySQL (local or cloud)
DATABASE_URL=mysql+aiomysql://user:password@host:3306/dbname
# PostgreSQL with SSL (cloud, e.g. Neon, Supabase, Aiven)
DATABASE_URL=postgresql+asyncpg://user:password@host:5432/dbname?sslmode=require
# MySQL with SSL (cloud, e.g. Aiven, PlanetScale)
DATABASE_URL=mysql+aiomysql://user:password@host:3306/dbname?ssl-mode=REQUIRED
Note:
For MySQL cloud providers, the ssl-mode parameter in the URL is ignored by the driver, but SSL is always enabled in the MCP server for cloud connections.
For PostgreSQL, use sslmode=require for cloud DBs. For MySQL, just use the standard URL; SSL is handled automatically.
If you see errors about ssl-mode or sslmode, check your URL and ensure you are using the correct driver prefix (mysql+aiomysql or postgresql+asyncpg).
If you get connect() got an unexpected keyword argument 'ssl-mode', ignore it: SSL is still enabled.
For network errors, check firewall and DB credentials.
For MySQL, always use mysql+aiomysql in the URL for async support.
code
## Security Features
- **Read-Only Operations**: Only SELECT queries are allowed
- **Query Validation**: Automatic detection and blocking of dangerous SQL operations
- **Result Limiting**: Maximum 50 rows per query (configurable)
- **Input Sanitization**: Protection against SQL injection
- **Safe Defaults**: Secure configuration out of the box
## Architecture
## Model Context Protocol (MCP) Integration
This server is designed to work seamlessly with MCP-compatible AI agents:
1. **Standardized Endpoints**: RESTful API following MCP conventions
2. **Structured Responses**: JSON responses optimized for AI consumption
3. **Error Handling**: Consistent error messages and status codes
4. **Documentation**: OpenAPI/Swagger documentation available at `/docs`
## Publish To VS Code MCP Store (Registry)
VS Code MCP gallery uses MCP Registry metadata. This repository now includes
`server.json` for registry publication.
### 1) Build and publish Docker image
```bash
docker build -t souhardyak/mcp-db-server:1.3.1 .
docker push souhardyak/mcp-db-server:1.3.1
2) Validate server metadata
server.json is configured for an OCI package and stdio transport:
Use the dedicated Docker smoke test in tests/docker:
bash
python tests/docker/smoke_test.py
This verifies Docker daemon access, image build, container startup, and health status.
Deployment
Docker Hub
bash
# Pull the latest image
docker pull souhardyak/mcp-db-server:latest
# Run with your database
docker run -d \
-p 8000:8000 \
-e DATABASE_URL="your_database_url_here" \
souhardyak/mcp-db-server:latest
# Start test database
docker-compose up postgres -d
# Wait for database to be readysleep 10
# Run tests
python -m pytest tests/ -v
Manual Testing
bash
# Test health endpoint
curl http://localhost:8000/health
# Test table listing
curl http://localhost:8000/mcp/list_tables
# Test natural language query
curl -X POST "http://localhost:8000/mcp/query" \
-H "Content-Type: application/json" \
-d '{"nl_query": "show me all customers from California"}'
Contributing
Fork the repository
Create your feature branch (git checkout -b feature/amazing-feature)
Commit your changes (git commit -m 'Add some amazing feature')
Push to the branch (git push origin feature/amazing-feature)
Open a Pull Request
License
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.
📝 Changelog
v1.3.0 (2025-12-24) - Docker Path Fix
Fixed: Resolved import path issues in Docker container causing from db import DatabaseManager to fail
Fixed: Changed relative paths to absolute paths in Dockerfile and docker-compose.yml healthchecks
Improved: mcp_server.py now uses robust path resolution that works both locally and in Docker containers
Updated: Docker image rebuilt and pushed with all path fixes
v1.2.0 (2025-11-03) - MySQL Column Access Fix
Fixed: Resolved Could not locate column in row for column 'column_name' error with MySQL databases
Fixed: Changed describe_table method to use index-based row access for better SQLAlchemy compatibility
Improved: Enhanced cross-database compatibility for schema introspection