MCP-SQL

judyfang0108/MCP-SQL

3.1

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

A Model Context Protocol (MCP) server that supports both PostgreSQL and MySQL databases, providing a unified interface for database exploration and querying.

Tools
9
Resources
0
Prompts
0

Multi-Database MCP Server

A Model Context Protocol (MCP) server that supports both PostgreSQL and MySQL databases. This server provides a unified interface for exploring and querying different database types through MCP.

Features

  • Multi-Database Support: Connect to PostgreSQL or MySQL databases
  • Auto-Detection: Automatically detects database type from connection string
  • Unified Interface: Same tools and API regardless of database type
  • Database-Specific Optimizations: Uses appropriate SQL syntax for each database type
  • Schema Exploration: List schemas, tables, and relationships
  • Query Execution: Run SQL queries with proper parameter handling
  • Resource Support: MCP resource endpoints for table data

Installation

  1. Install dependencies:
pip install -r requirements.txt

Usage

Connection Strings

PostgreSQL
# Using connection string
python postgresql-server.py --conn "postgresql://user:password@host:port/database"

# Using environment variable
export DATABASE_CONNECTION_STRING="postgresql://user:password@host:port/database"
python multi-db-server.py
MySQL
# Using connection string
python multi-db-server.py --conn "mysql://user:password@host:port/database"

# Using environment variable
export DATABASE_CONNECTION_STRING="mysql://user:password@host:port/database"
python multi-db-server.py

Command Line Options

python multi-db-server.py [OPTIONS]

Options:
  --conn TEXT           Database connection string (supports PostgreSQL and MySQL)
  --db-type TEXT        Database type: auto, postgresql, or mysql (default: auto)
  --transport TEXT      Transport protocol: stdio, sse, or streamable-http (default: stdio)
  --host TEXT           Host to bind for SSE/HTTP transports (default: 127.0.0.1)
  --port INTEGER        Port to bind for SSE/HTTP transports (default: 8000)
  --mount TEXT          Optional mount path for SSE transport (e.g., /mcp)
  --help                Show this message and exit

Environment Variables

  • DATABASE_CONNECTION_STRING: Database connection string
  • DATABASE_READONLY: Set to "true" to enable read-only mode
  • DATABASE_STATEMENT_TIMEOUT_MS: Query timeout in milliseconds
  • MCP_TRANSPORT: Transport protocol (stdio, sse, streamable-http)
  • MCP_HOST: Host for network transports
  • MCP_PORT: Port for network transports
  • MCP_SSE_MOUNT: Mount path for SSE transport

Database-Specific Features

PostgreSQL

  • Full schema support with information_schema
  • Advanced relationship detection
  • ILIKE for case-insensitive pattern matching
  • current_schema() function support

MySQL

  • Database-level organization (no schemas like PostgreSQL)
  • SHOW DATABASES and SHOW TABLES for performance
  • DESCRIBE for table structure
  • SHOW CREATE TABLE for detailed table information

Tools

The server provides the following MCP tools:

  • server_info: Get server and database information
  • db_identity: Get current database identity details
  • query: Execute SQL queries (legacy)
  • run_query: Execute SQL queries with typed input
  • list_schemas: List all schemas/databases
  • list_tables: List tables in a schema
  • describe_table: Get table structure information
  • get_foreign_keys: Get foreign key relationships
  • find_relationships: Find explicit and implied table relationships

Examples

List Schemas

# PostgreSQL: lists schemas
# MySQL: lists databases
result = await client.call_tool("list_schemas", {})

List Tables

# Lists tables in the current schema/database
result = await client.call_tool("list_tables", {"db_schema": "public"})

Execute Query

# Works with both PostgreSQL and MySQL
result = await client.call_tool("run_query", {
    "input": {
        "sql": "SELECT * FROM users LIMIT 10",
        "format": "json"
    }
})

Notes

  • The server automatically detects database type from the connection string
  • MySQL connection strings must start with mysql://
  • PostgreSQL connection strings can start with postgresql:// or postgres://
  • Some features may have different implementations for each database type
  • The server maintains backward compatibility with existing PostgreSQL tools

Troubleshooting

Connection Issues

  • Ensure the database server is running and accessible
  • Check connection string format and credentials
  • Verify network connectivity and firewall settings

MySQL-Specific Issues

  • Ensure MySQL server supports the connection protocol
  • Check user permissions for the specified database
  • Verify MySQL connector version compatibility

PostgreSQL-Specific Issues

  • Ensure PostgreSQL server is running
  • Check pg_hba.conf for connection permissions
  • Verify psycopg version compatibility