multidb_mcp_server

multidb_mcp_server

3.2

If you are the rightful owner of multidb_mcp_server and would like to certify it and/or have it hosted online, please leave a comment on the right or send an email to henry@mcphub.com.

A Model Context Protocol (MCP) server that facilitates secure interaction with multiple SQL databases, enabling AI assistants to perform database operations safely and efficiently.

Multi-Database MCP Server

A Model Context Protocol (MCP) server that enables secure interaction with multiple SQL databases (MySQL, PostgreSQL, MSSQL). This server allows AI assistants to list tables, read data, and execute SQL queries through a controlled interface across different database types, making database exploration and analysis safer and more structured.

πŸš€ Features

Multi-Database Support

  • MySQL - Full support via mysql-connector-python
  • PostgreSQL - Full support via psycopg2
  • Microsoft SQL Server - Full support via pyodbc

Unified Operations

  • Database Discovery - List all configured databases
  • Table Operations - List tables, describe structure, read data
  • Schema Exploration - Get complete database schemas
  • Query Execution - Execute SQL queries with proper dialect handling
  • Read-Only Mode - Configurable read-only restrictions per database
  • Secure Configuration - JSON/YAML configuration with password masking

Deployment Options

  • STDIO Mode - Direct MCP protocol via stdin/stdout
  • HTTP Mode - REST API with SSE support for multi-user access
  • Docker Deployment - Containerized deployment for production

πŸ“¦ Installation

Prerequisites

  • Python 3.11+
  • Database drivers (installed automatically):
    • MySQL: mysql-connector-python
    • PostgreSQL: psycopg2-binary
    • MSSQL: pyodbc + ODBC drivers

Install from PyPI

pip install mysql_mcp_server

Install from Source

git clone https://github.com/your-repo/mysql_mcp_server.git
cd mysql_mcp_server
pip install -e .

πŸ”§ Configuration

Database Configuration File

Create a db_config.json file with your database connections:

{
  "databases": [
    {
      "id": "core_mysql",
      "type": "mysql",
      "host": "127.0.0.1",
      "port": 3306,
      "username": "admin",
      "password": "your_password",
      "database": "core",
      "readonly": false,
      "charset": "utf8mb4",
      "collation": "utf8mb4_unicode_ci",
      "sql_mode": "TRADITIONAL"
    },
    {
      "id": "analytics_pg",
      "type": "postgres",
      "host": "127.0.0.1",
      "port": 5432,
      "username": "readonly_user",
      "password": "your_password",
      "database": "analytics",
      "readonly": true
    },
    {
      "id": "crm_mssql",
      "type": "mssql",
      "host": "127.0.0.1",
      "port": 1433,
      "username": "crm_readonly",
      "password": "your_password",
      "database": "CRM_PROD",
      "readonly": true,
      "driver": "ODBC Driver 17 for SQL Server"
    }
  ]
}

Read-Only Database Configuration

You can configure databases to be read-only, which restricts them to SELECT, SHOW, DESCRIBE, and EXPLAIN operations only:

{
  "databases": [
    {
      "id": "production_readonly",
      "type": "mysql",
      "host": "prod-db.company.com",
      "port": 3306,
      "username": "readonly_user",
      "password": "readonly_password",
      "database": "production",
      "readonly": true
    },
    {
      "id": "dev_readwrite", 
      "type": "mysql",
      "host": "dev-db.company.com",
      "port": 3306,
      "username": "dev_user",
      "password": "dev_password",
      "database": "development",
      "readonly": false
    }
  ]
}

Readonly Restrictions:

  • βœ… Allowed: SELECT, SHOW, DESCRIBE, DESC, EXPLAIN, WITH
  • ❌ Blocked: INSERT, UPDATE, DELETE, DROP, CREATE, ALTER, TRUNCATE, REPLACE, MERGE, GRANT, REVOKE, SET, USE, EXECUTE, CALL

πŸƒ Usage

STDIO Mode (Direct MCP)

Run the server in STDIO mode for direct MCP protocol communication:

mysql_mcp_server

HTTP Mode (Multi-User)

Run the server in HTTP mode for web API access:

mysql_mcp_http_server

The server will start on http://0.0.0.0:8000 with the following endpoints:

  • GET / - Health check and database list
  • GET /databases - List configured databases
  • POST /test-connection - Test database connectivity
  • GET /sse - SSE endpoint for MCP protocol
  • POST /message - HTTP endpoint for MCP messages

Docker Deployment

Build the Docker Image
docker build -t multi-db-mcp-server .
Run with Configuration File
 docker run -d --name mysql-mcp-server --network host -v $(pwd)/db_config_sample.json:/app/db_config.json mysql-mcp-server python -m mysql_mcp_server.http_server --config db_config.json --host 0.0.0.0 --port 8000

πŸ› οΈ Available Tools

The server provides the following MCP tools:

list_databases

Lists all configured databases.

{
  "name": "list_databases",
  "arguments": {}
}

list_tables

Lists tables in a specific database.

{
  "name": "list_tables",
  "arguments": {
    "database_id": "core_mysql"
  }
}

describe_table

Describes the structure of a table.

{
  "name": "describe_table", 
  "arguments": {
    "database_id": "core_mysql",
    "table_name": "users"
  }
}

get_schema

Gets the complete schema of a database.

{
  "name": "get_schema",
  "arguments": {
    "database_id": "core_mysql"
  }
}

execute_sql

Executes an SQL query on a specific database.

{
  "name": "execute_sql",
  "arguments": {
    "database_id": "core_mysql", 
    "query": "SELECT * FROM users LIMIT 10"
  }
}

Download the schemas

For schema of all databases to your local, you can run this script

python download_schemas.py --server http://<server_ip>: --format markdown --output my_schemas

πŸ”— Integration

Cursor/VS Code

Configure the MCP server in your editor settings to connect to the HTTP endpoint:

{
  "mcp.servers": [
    {
      "name": "multi-db",
      "url": "http://your-server:8000/sse"
    }
  ]
}

πŸ”’ Security

Password Handling

  • Passwords are masked in all log output
  • Configuration files should be properly secured
  • Consider using environment variables or secret managers for production

Network Security

  • Database connections should use SSL/TLS when possible
  • Configure firewalls to restrict database access
  • Use read-only database users when possible

Production Deployment

  • Use reverse proxy (nginx/traefik) for HTTPS termination
  • Implement authentication/authorization as needed
  • Monitor and log all database access

πŸ“Š Monitoring

Health Check

Check server health via HTTP:

curl http://localhost:8000/health

Response:

{
  "status": "healthy",
  "databases": [
    {
      "id": "core_mysql",
      "type": "mysql", 
      "host": "localhost",
      "database": "core"
    }
  ],
  "version": "0.3.0"
}

Test Database Connectivity

curl -X POST http://localhost:8000/test-connection \
  -H "Content-Type: application/json" \
  -d '{"database_id": "core_mysql"}'

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests for new functionality
  5. Submit a pull request

πŸ“„ License

This project is licensed under the MIT License - see the file for details.

πŸ™‹ Support

⚑ Performance Tips

Connection Pooling

For high-throughput scenarios, consider implementing connection pooling in the database adapters.

Query Optimization

  • Use LIMIT clauses for large result sets
  • Create appropriate database indexes
  • Monitor slow query logs

Resource Limits

  • Set appropriate memory limits for Docker containers
  • Monitor CPU and memory usage
  • Implement query timeouts for long-running queries

πŸ—ΊοΈ Roadmap

  • Connection pooling support
  • Query result caching
  • Advanced authentication methods
  • Database migration tools
  • Query performance analytics
  • Support for additional database types (Oracle, SQLite, etc.)
  • GUI configuration interface