MySQL MCP Server

repository·main·Indexed 23 days ago

https://github.com/designcomputer/mysql_mcp_server

A Model Context Protocol (MCP) server that enables AI assistants to interact securely with MySQL databases. It supports STDIO and SSE transport modes, allowing for database exploration, schema inspection, and SQL execution through tools like execute_sql, get_schema_info, and get_table_sample. The server supports single-database and multi-database modes, as well as SSH tunneling for secure connections.

Tokens
3.5K
Snippets
6
Records
23
Agent score
80%

What's inside mysql_mcp_server

  1. Use Multi-database mode

    main

    If you do not set the MYSQL_DATABASE environment variable, the server operates in Multi-database mode:

    • list_resources will return all available user databases (system databases are filtered out).
    • You must use fully qualified table names (e.g., database_name.table_name) in your SQL queries.
    • Limitation: Only single SQL statements are supported. Multi-statement queries (like USE db; SELECT ...) are not allowed.
  2. Configure Claude Desktop for MySQL MCP

    main

    To use the MySQL MCP server with Claude Desktop, add the following configuration to your claude_desktop_config.json. Ensure you replace the placeholder values in the env block with your actual database credentials.

    {
      "mcpServers": {
        "mysql": {
          "command": "uv",
          "args": [
            "--directory",
            "path/to/mysql_mcp_server",
            "run",
            "mysql_mcp_server"
          ],
          "env": {
            "MYSQL_HOST": "localhost",
            "MYSQL_PORT": "3306",
            "MYSQL_USER": "your_username",
            "MYSQL_PASSWORD": "your_password",
            "MYSQL_DATABASE": "your_database"
          }
        }
      }
    }
  3. Configure VS Code for MySQL MCP

    main

    To use the MySQL MCP server with Visual Studio Code, add this to your mcp.json. Note that you must have uv installed for this configuration to work.

    {
      "mcpServers": {
        "mysql": {
          "type": "stdio",
          "command": "uvx",
          "args": [
            "--from",
            "mysql-mcp-server",
            "mysql_mcp_server"
          ],
          "env": {
            "MYSQL_HOST": "localhost",
            "MYSQL_PORT": "3306",
            "MYSQL_USER": "your_username",
            "MYSQL_PASSWORD": "your_password",
            "MYSQL_DATABASE": "your_database"
          }
        }
      }
    }
  4. Secure SSE Transport deployments

    main

    The SSE transport mode has no built-in authentication and binds to 0.0.0.0 by default. If exposing the server beyond localhost, you must place it behind a reverse proxy (like Nginx, Caddy, or Traefik) that enforces authentication.

    Recommended Security Pattern:

    1. Set MCP_SSE_HOST=127.0.0.1 so the server only listens on the loopback interface.
    2. Use a reverse proxy to handle public requests and enforce authentication (e.g., HTTP Basic Auth).
    3. Configure MCP_SSE_ALLOWED_HOSTS to include your proxy's public hostname.

    Example Nginx Configuration:

    location /sse {
        auth_basic "MCP";
        auth_basic_user_file /etc/nginx/.htpasswd;
        proxy_pass http://127.0.0.1:8000;
        proxy_set_header Host $host;
        proxy_buffering off;
    }
    location /messages/ {
        auth_basic "MCP";
        auth_basic_user_file /etc/nginx/.htpasswd;
        proxy_pass http://127.0.0.1:8000;
    }
    location /sse {
        auth_basic "MCP";
        auth_basic_user_file /etc/nginx/.htpasswd;
        proxy_pass http://127.0.0.1:8000;
        proxy_set_header Host $host;
        proxy_buffering off;
    }
    location /messages/ {
        auth_basic "MCP";
        auth_basic_user_file /etc/nginx/.htpasswd;
        proxy_pass http://127.0.0.1:8000;
    }
  5. Install MySQL MCP Server

    main

    You can install the MySQL MCP Server using several methods depending on your environment:

    Manual Installation

    Install via pip:

    pip install mysql-mcp-server

    Via Smithery (Automatic Claude Desktop setup)

    Use the Smithery CLI to install the server for Claude Desktop:

    npx -y @smithery/cli install designcomputer/mysql-mcp-server --client claude

    Via Claude Code CLI

    Add the server to Claude Code using uvx:

    claude mcp add --transport stdio designcomputer-mysql_mcp_server uvx mysql_mcp_server
    pip install mysql-mcp-server
  6. Discover databases and tables

    main

    When first connecting to a MySQL instance, use the list_resources capability to discover available data.

    • List all tables (Single-DB mode): Call list_resources to see the tables in the current database.
    • List all databases (Multi-DB mode): Call list_resources. It returns URIs in the format mysql://database/my_db.
    • List tables in a specific database: In multi-DB mode, read the resource URI for the target database (e.g., mysql://database/my_db) to discover its tables.
  7. Access MySQL data as Resources

    main

    The server exposes MySQL tables and databases as MCP resources using the mysql:// URI scheme.

    • List Databases: If no default database is configured, you can discover databases via mysql://database/{db_name}.
    • List Tables: In a specific database, you can list tables via mysql://database/{db_name}.
    • Read Table Data: You can read a sample of data (up to 100 rows) from a table using mysql://{table_name}/data.
  8. Configure MySQL MCP Server environment variables

    main

    The server is configured using environment variables.

    Important for Claude Desktop/Claude Code: Do not rely on a .env file, as these hosts launch the server from their own working directories. Instead, place your configuration directly in the env block of your MCP configuration file.

    Core Connection Settings

    • MYSQL_HOST: Database host
    • MYSQL_PORT: Database port (defaults to 3306)
    • MYSQL_USER: Database username
    • MYSQL_PASSWORD: Database password
    • MYSQL_DATABASE: Specific database to use. If omitted, the server enters Multi-database mode.

    Advanced & Compatibility Settings

    • MYSQL_SSL_MODE: SSL mode (DISABLED, REQUIRED, VERIFY_CA, VERIFY_IDENTITY)
    • MYSQL_CONNECT_TIMEOUT: Timeout in seconds
    • MYSQL_SQL_MODE: SQL mode (default: TRADITIONAL)
    • MYSQL_CHARSET: e.g., utf8mb4
    • MYSQL_COLLATION: e.g., utf8mb4_unicode_ci
    • MYSQL_AUTH_PLUGIN: e.g., mysql_native_password
    • MYSQL_USE_PURE: Force pure-Python connector (default: false)
    • MYSQL_RAISE_ON_WARNINGS: Raise on SQL warnings (default: false)

    SSE Transport Settings

    • MCP_TRANSPORT: Set to sse for HTTP transport
    • MCP_SSE_HOST: Listen address (required for Docker/hosting)
    • PORT: HTTP port (fallback for MCP_SSE_PORT)
    • MCP_SSE_ALLOWED_HOSTS: Comma-separated allowed Host headers

    SSH Tunneling Settings

    • MYSQL_SSH_ENABLE: Set to true to enable
    • MYSQL_SSH_HOST: SSH jump host
    • MYSQL_SSH_PORT: SSH port (default: 22)
    • MYSQL_SSH_USER: SSH username
    • MYSQL_SSH_KEY_PATH: Path to SSH private key
    • MYSQL_SSH_REMOTE_HOST: Host from the perspective of the jump host (default: localhost)
    • MYSQL_SSH_REMOTE_PORT: Remote MySQL port (default: 3306)
    • MYSQL_LOCAL_PORT: Local port for the tunnel (default: 3330)
  9. Use MCP prompts for database exploration

    main

    The server provides guided workflows via MCP prompts. In Claude Code, these are accessed via /mcp__mysql__<prompt_name>; in Claude Desktop, they appear in the prompts menu.

    PromptArgumentsDescription
    explore_database(none)Systematically discover tables, inspect schemas, sample data, and summarize the database.
    analyze_tabletable_name (required)Deep-dive into a specific table: retrieve schema, sample data, and suggest queries. Supports database.table notation.

    Example (Claude Code):

    /mcp__mysql__explore_database
    /mcp__mysql__analyze_table customers
    /mcp__mysql__explore_database
    /mcp__mysql__analyze_table customers
  10. Inspect data with get_table_sample

    main

    Use get_table_sample to quickly verify table contents and data formats (such as date formats or status strings) without fetching large datasets. It returns a small sample of rows along with column names.

    • Default behavior: Fetches 5 rows.
    • Maximum limit: 20 rows.
    • Custom limit: Use the limit parameter to specify the number of rows.
    get_table_sample({"table_name": "orders", "limit": 10})
  11. Explore schema with get_schema_info

    main

    Use the get_schema_info tool to retrieve detailed column information, data types, and comments to understand table structures and relationships.

    • To get an overview of all tables: Pass an empty object {}.
    • To get detailed info for a specific table: Pass an object containing the table_name key.
  12. Use get_schema_info tool

    main

    Provides detailed metadata about database structures.

    • Arguments: table_name (optional string)
    • Output: Column names, types, nullability, default values, and comments.
    • Cross-database: Pass database.table to query a table outside the configured MYSQL_DATABASE.
    • Identifier rules: Names must contain only alphanumeric characters, underscores, and $ (dots are allowed as a separator between database and table names).