mysql-mcp-server

MCP.Pizza Chef: xiangma9712

The mysql-mcp-server is an MCP server designed to interface with MySQL databases, allowing execution of read-only queries and write queries that are rolled back for testing. It provides a secure and controlled environment for database interaction within MCP workflows, supporting environment variable configuration for flexible deployment. This server is ideal for integrating MySQL data access into AI-driven applications while preventing permanent data changes.

Use This MCP server To

Execute read-only SQL queries on MySQL databases Run test write queries with automatic rollback Integrate MySQL data access into AI workflows Validate SQL queries without affecting production data Provide database context to LLMs for real-time querying Enable safe experimentation with database commands Support multi-step reasoning involving MySQL data Automate data retrieval from MySQL for reports

README

MySQL MCP Server

An MCP server for interacting with MySQL databases.

This server supports executing read-only queries (query) and write queries that are ultimately rolled back (test_execute).

MySQL Server MCP server

Setup

Environment Variables

Add the following environment variables to ~/.mcp/.env:

MYSQL_HOST=host.docker.internal  # Hostname to access host services from Docker container
MYSQL_PORT=3306
MYSQL_USER=root
MYSQL_PASSWORD=your_password

Note: host.docker.internal is a special DNS name for accessing host machine services from Docker containers. Use this setting when connecting to a MySQL server running on your host machine. If connecting to a different MySQL server, change to the appropriate hostname.

mcp.json Configuration

{
  "mcpServers": {
    "mysql": {
      "command": "docker",
      "args": [
        "run",
        "-i",
        "--rm",
        "--add-host=host.docker.internal:host-gateway",
        "--env-file",
        "/Users/username/.mcp/.env",
        "ghcr.io/xiangma9712/mcp/mysql"
      ]
    }
  }
}

Usage

Starting the Server

docker run -i --rm --add-host=host.docker.internal:host-gateway --env-file ~/.mcp/.env ghcr.io/xiangma9712/mcp/mysql

Note: If you're using OrbStack, host.docker.internal is automatically supported, so the --add-host option can be omitted. While Docker Desktop also typically supports this automatically, adding the --add-host option is recommended for better reliability.

Available Commands

1. Execute Read-only Query

{
  "type": "query",
  "payload": {
    "sql": "SELECT * FROM your_table"
  }
}

Response:

{
  "success": true,
  "data": [
    {
      "id": 1,
      "name": "example"
    }
  ]
}

2. Test Query Execution

{
  "type": "test_execute",
  "payload": {
    "sql": "UPDATE your_table SET name = 'updated' WHERE id = 1"
  }
}

Response:

{
  "success": true,
  "data": "The UPDATE SQL query can be executed."
}

3. List Tables

{
  "type": "list_tables"
}

Response:

{
  "success": true,
  "data": ["table1", "table2", "table3"]
}

4. Describe Table

{
  "type": "describe_table",
  "payload": {
    "table": "your_table"
  }
}

Response:

{
  "success": true,
  "data": [
    {
      "Field": "id",
      "Type": "int(11)",
      "Null": "NO",
      "Key": "PRI",
      "Default": null,
      "Extra": ""
    },
    {
      "Field": "name",
      "Type": "varchar(255)",
      "Null": "YES",
      "Key": "",
      "Default": null,
      "Extra": ""
    }
  ]
}

Implementation Details

  • Implemented in TypeScript
  • Uses mysql2 package
  • Runs as a Docker container
  • Accepts JSON commands through standard input
  • Returns JSON responses through standard output
  • Uses host.docker.internal to connect to host MySQL (compatible with both OrbStack and Docker Desktop)

Security Considerations

  • Uses environment variables for sensitive information management
  • SQL injection prevention is the implementer's responsibility
  • Proper network configuration required for production use
  • Appropriate firewall settings needed when connecting to host machine services

mysql-mcp-server FAQ

How do I configure the mysql-mcp-server to connect to my database?
Set environment variables MYSQL_HOST, MYSQL_PORT, MYSQL_USER, and MYSQL_PASSWORD in ~/.mcp/.env to match your MySQL server credentials.
Can this server execute write operations on the database?
It supports write queries only in test mode where changes are rolled back, ensuring no permanent data modification.
Is it possible to use this server with Docker?
Yes, the server supports Docker setups and uses 'host.docker.internal' to access host services from containers.
What types of queries are supported by the mysql-mcp-server?
It supports read-only queries and test_execute queries that simulate writes with rollback.
How does the server ensure data safety during write operations?
Write queries are executed in a transaction that is rolled back, preventing any permanent changes.
Can this MCP server be integrated with multiple LLM providers?
Yes, it is provider-agnostic and can work with OpenAI, Claude, Gemini, and others.
Where can I find the server's source code and documentation?
The source code and setup instructions are available on its GitHub repository linked in the MCP registry.
What is the recommended use case for the test_execute feature?
Use test_execute to validate write queries or simulate database changes without affecting live data.