MySQL MCP Server

MySQL MCP Server

This server connects to MySQL databases, enabling listing databases and tables, describing table schemas, and executing read-only SQL queries with optional write support and SSL security.

Category
访问服务器

README

MySQL MCP (FastMCP)

Python MCP server built with FastMCP to connect to MySQL. It exposes tools to list databases and tables, describe tables, and execute SQL queries (read-only by default).

Requirements

  • Python 3.10+
  • MySQL reachable (host, username, password)

Installation

Requires uv. In the project directory:

cd /Users/macbook/projets/MCP/mysql
uv sync

This creates the virtual environment (.venv) and installs dependencies. No pip step is required.

To validate locally:

uv run env PYTHONPATH=src python -m mysql_mcp

Configuration

How the server reads credentials

The server uses environment variables with prefix MYSQL_. The resolution happens in two layers:

  1. First: values provided to the process via env in Cursor mcp.json (or via shell/CI).
  2. Second (fallback): values from the project .env file, but only for fields that are not defined in the environment.

This means that if you configure MYSQL_USER and MYSQL_PASSWORD in mcp.json, the project .env file will not override those values.

Environment variables (or .env file):

Variable Required Description Default
MYSQL_USER Yes MySQL username -
MYSQL_PASSWORD Yes Password -
MYSQL_HOST No Host 127.0.0.1
MYSQL_PORT No Port 3306
MYSQL_DATABASE No Default database -
MYSQL_POOL_SIZE No Connection pool size 5
MYSQL_ALLOW_WRITE No Allow INSERT/UPDATE/DELETE false
MYSQL_SSL_ENABLED No Enable TLS/SSL for MySQL connection false
MYSQL_SSL_VERIFY_CERT No Validate server certificate chain true
MYSQL_SSL_CA No Path to CA bundle (recommended in prod) -
MYSQL_SSL_CERT No Client certificate path (for mTLS) -
MYSQL_SSL_KEY No Client private key path (for mTLS) -

Security details

By default, the server only accepts read-only queries (for example: SELECT, SHOW, DESCRIBE). When MYSQL_ALLOW_WRITE=true, it also accepts INSERT, UPDATE, DELETE, and REPLACE.

Additional rules:

  • UPDATE and DELETE require an explicit WHERE clause (otherwise the query is rejected).

.env file (optional)

If you want, create /Users/macbook/projets/MCP/mysql/.env using the same format as the variables above. You can use /.env.example in this repository as a reference.

Minimum example (read-only):

MYSQL_USER=user
MYSQL_PASSWORD=password
MYSQL_DATABASE=database
MYSQL_ALLOW_WRITE=false

Production note (require_secure_transport=ON)

If your MySQL server enforces secure transport (--require_secure_transport=ON), you must enable TLS:

MYSQL_SSL_ENABLED=true
MYSQL_SSL_VERIFY_CERT=true
MYSQL_SSL_CA=/absolute/path/to/ca.pem

If you do not have a CA certificate available yet, you can still use TLS encryption without certificate validation:

MYSQL_SSL_ENABLED=true
MYSQL_SSL_VERIFY_CERT=false

This is useful as a temporary fallback, but it is less secure (susceptible to man-in-the-middle attacks). Prefer MYSQL_SSL_VERIFY_CERT=true with a valid CA bundle in production.

If your database requires mTLS, also set:

MYSQL_SSL_CERT=/absolute/path/to/client-cert.pem
MYSQL_SSL_KEY=/absolute/path/to/client-key.pem

Cursor note (mcp.json)

If you are using Cursor, the most common setup is to configure MYSQL_* directly in the env of the mysql server block inside /Users/macbook/.cursor/mcp.json (as shown in the Cursor example below). This avoids relying on the local .env file for credentials.

Running the server

STDIO (e.g. Claude Desktop, Cursor):

/usr/bin/env PYTHONPATH=/Users/macbook/projets/MCP/mysql/src /Users/macbook/projets/MCP/mysql/.venv/bin/python -m mysql_mcp

Alternative:

uv run env PYTHONPATH=src python -m mysql_mcp

HTTP (port 8000):

/Users/macbook/projets/MCP/mysql/.venv/bin/python -c "from mysql_mcp.server import run; run(transport='http', port=8000)"

Or with the FastMCP CLI:

uv run fastmcp run mysql_mcp.server:mcp --transport http --port 8000

MCP Tools

  • list_databases - Lists all databases.
  • list_tables - Lists tables in a database (optional database parameter).
  • describe_table - Returns column information (name, type, null, key, default, extra) for a table.
  • execute_query - Executes a validated SQL query (following the security rules).

Cursor configuration example

In Cursor Settings > MCP, add a server that will be started via stdio.

Important: for stdio, do not set transport=http and do not provide port. The server uses stdio by default.

Example (JSON in ~/.cursor/mcp.json)

If you use Cursor global configuration, edit /Users/macbook/.cursor/mcp.json (or create a .cursor/mcp.json inside this project directory) and add a mcpServers entry like this:

{
  "mcpServers": {
    "mysql": {
      "command": "/usr/bin/env",
      "args": [
        "PYTHONPATH=/Users/macbook/projets/MCP/mysql/src",
        "/Users/macbook/projets/MCP/mysql/.venv/bin/python",
        "-m",
        "mysql_mcp"
      ],
      "cwd": "/Users/macbook/projets/MCP/mysql",
      "env": {
        "MYSQL_USER": "user",
        "MYSQL_PASSWORD": "password",
        "MYSQL_HOST": "127.0.0.1",
        "MYSQL_PORT": "3306",
        "MYSQL_DATABASE": "database",
        "MYSQL_ALLOW_WRITE": "false"
      }
    }
  }
}

Per-project (recommended) - 3 environments

You can configure multiple MySQL targets per workspace by creating/updating:

  • /Users/macbook/projets/MCP/mysql/.cursor/mcp.json

Example (3 server blocks, read-only by default):

{
  "mcpServers": {
    "mysql_local": {
      "command": "/usr/bin/env",
      "args": [
        "PYTHONPATH=/Users/macbook/projets/MCP/mysql/src",
        "/Users/macbook/projets/MCP/mysql/.venv/bin/python",
        "-m",
        "mysql_mcp"
      ],
      "cwd": "/Users/macbook/projets/MCP/mysql",
      "env": {
        "MYSQL_USER": "your_user",
        "MYSQL_PASSWORD": "your_password",
        "MYSQL_HOST": "127.0.0.1",
        "MYSQL_PORT": "3306",
        "MYSQL_DATABASE": "database",
        "MYSQL_ALLOW_WRITE": "false"
      }
    },
    "mysql_staging": {
      "command": "/usr/bin/env",
      "args": [
        "PYTHONPATH=/Users/macbook/projets/MCP/mysql/src",
        "/Users/macbook/projets/MCP/mysql/.venv/bin/python",
        "-m",
        "mysql_mcp"
      ],
      "cwd": "/Users/macbook/projets/MCP/mysql",
      "env": {
        "MYSQL_USER": "your_user",
        "MYSQL_PASSWORD": "your_password",
        "MYSQL_HOST": "staging-db.example.com",
        "MYSQL_PORT": "3306",
        "MYSQL_DATABASE": "database",
        "MYSQL_ALLOW_WRITE": "false"
      }
    },
    "mysql_prod": {
      "command": "/usr/bin/env",
      "args": [
        "PYTHONPATH=/Users/macbook/projets/MCP/mysql/src",
        "/Users/macbook/projets/MCP/mysql/.venv/bin/python",
        "-m",
        "mysql_mcp"
      ],
      "cwd": "/Users/macbook/projets/MCP/mysql",
      "env": {
        "MYSQL_USER": "your_user",
        "MYSQL_PASSWORD": "your_password",
        "MYSQL_HOST": "prod-db.example.com",
        "MYSQL_PORT": "3306",
        "MYSQL_DATABASE": "database",
        "MYSQL_ALLOW_WRITE": "false",
        "MYSQL_SSL_ENABLED": "true",
        "MYSQL_SSL_VERIFY_CERT": "true",
        "MYSQL_SSL_CA": "/absolute/path/to/ca.pem"
      }
    }
  }
}

Notes:

  • For stdio, do not set transport=http and do not provide port.
  • If you removed the project .env, it is still fine: MYSQL_* must be provided via env in mcp.json (as shown above).
  • Recommended startup in MCP clients is setting PYTHONPATH directly in command/args (via /usr/bin/env), because some clients do not consistently apply env.PYTHONPATH.

Option A (recommended) - using uv:

  • Command: uv
  • Args: run, env, PYTHONPATH=src, python, -m, mysql_mcp
  • Cwd: project directory (where pyproject.toml lives)
  • Env: set MYSQL_USER, MYSQL_PASSWORD, and optionally MYSQL_HOST, MYSQL_PORT, MYSQL_DATABASE, MYSQL_ALLOW_WRITE, MYSQL_SSL_*

Option B - using the Makefile shortcut:

  • Command: make
  • Args: up
  • Cwd: project directory
  • Env: same values as Option A

Alternative - using the venv interpreter directly:

  • Command: /usr/bin/env
  • Args: PYTHONPATH=/absolute/path/to/project/src, .venv/bin/python, -m, mysql_mcp

License

MIT.

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选