mysql-mcp-server
MCP server that lets clients run SQL (select/insert/update/delete) against a MySQL database, with readonly and readwrite modes.
README
mysql-mcp-server
A Model Context Protocol (MCP) server that lets an MCP
client (Claude Desktop, Claude Code, etc.) run SQL against a MySQL database through four
tools: select, insert, update, and delete.
The server runs as a uv-managed Python package and communicates with the client over
stdio, as a subprocess started by the client.
Requirements
- Python 3.11+
uv- A reachable MySQL server
Installation
uv sync
Configuration
The server requires six values, each settable via environment variable and/or CLI flag (CLI flags take priority over environment variables):
| Parameter | Env var | CLI flag | Required | Default |
|---|---|---|---|---|
| Mode | MYSQL_MODE |
--mysql-mode |
yes | — (readonly or readwrite) |
| Host | MYSQL_HOST |
--mysql-host |
yes | — |
| Port | MYSQL_PORT |
--mysql-port |
no | 3306 |
| User | MYSQL_USER |
--mysql-user |
yes | — |
| Password | MYSQL_PASSWORD |
--mysql-password |
yes | — |
| Database | MYSQL_DATABASE |
--mysql-database |
yes | — |
If a required value is missing, or MYSQL_MODE is not readonly/readwrite, the server
prints an error to stderr and exits with status code 1 without starting.
readonlymode: only theselecttool is allowed.insert/update/deleteare rejected with aPERMISSION_DENIEDerror.readwritemode: all four tools are allowed.
The mode is fixed for the lifetime of the process; it cannot be changed at runtime.
Security recommendation:
readonlymode is an application-level guard, not a substitute for database privileges. Where possible, pointreadonlymode at a MySQL account that only hasSELECTgrants.
Is a
.envfile required? No. The server itself never reads.envfiles — it only reads CLI flags and real process environment variables (os.environ). How you get values into that environment depends on how you run it:
- As an MCP server (see Connecting from an MCP client below): the client (Claude Desktop/Code) spawns the server process and injects the
envblock from its own JSON config directly as environment variables. No.envfile is involved or needed.- Running the CLI directly for local dev/testing:
.envis just a convenience so you don't have toexportsix variables by hand. Copy.env.exampleto.env, fill in real values, and load it explicitly — it is not read automatically:uv run --env-file .env mysql-mcp-server.envis git-ignored and must never be committed.
Running
# Environment variables (or use `uv run --env-file .env mysql-mcp-server`, see above)
export MYSQL_MODE=readonly
export MYSQL_HOST=127.0.0.1
export MYSQL_PORT=3306
export MYSQL_USER=app_user
export MYSQL_PASSWORD=secret
export MYSQL_DATABASE=mydb
uv run mysql-mcp-server
# Or, equivalently, via CLI flags
uv run mysql-mcp-server \
--mysql-mode readonly \
--mysql-host 127.0.0.1 \
--mysql-port 3306 \
--mysql-user app_user \
--mysql-password secret \
--mysql-database mydb
Connecting from an MCP client
Claude Desktop / Claude Code
Add an entry to your MCP client's server config (e.g. Claude Desktop's
claude_desktop_config.json, or .mcp.json for Claude Code):
{
"mcpServers": {
"mysql": {
"command": "uv",
"args": [
"--directory",
"/absolute/path/to/mysql-mcp-server",
"run",
"mysql-mcp-server"
],
"env": {
"MYSQL_MODE": "readonly",
"MYSQL_HOST": "127.0.0.1",
"MYSQL_PORT": "3306",
"MYSQL_USER": "app_user",
"MYSQL_PASSWORD": "secret",
"MYSQL_DATABASE": "mydb"
}
}
}
}
Restart the client after editing the config. The select, insert, update, and
delete tools (subject to MYSQL_MODE) should then be available to the model.
Tools
All four tools take {"query": string, "params"?: array} and always use %s
parameter-binding placeholders in query — never string-format user input into a query.
| Tool | Allowed in | Query must start with | Success data shape |
|---|---|---|---|
select |
any mode | SELECT / WITH |
{rows, row_count, truncated} (capped at 1000 rows) |
insert |
readwrite only |
INSERT |
{affected_rows, last_insert_id} |
update |
readwrite only |
UPDATE |
{affected_rows} (+ warning if no WHERE) |
delete |
readwrite only |
DELETE |
{affected_rows} (+ warning if no WHERE) |
Every tool call returns one of:
{ "success": true, "data": { ... } }
{ "success": false, "error": { "code": "...", "message": "..." } }
Error codes: PERMISSION_DENIED, INVALID_QUERY_TYPE, MULTI_STATEMENT_NOT_ALLOWED,
DB_CONNECTION_ERROR, DB_EXECUTION_ERROR, INTERNAL_ERROR.
Multi-statement queries (;-separated) and any DDL/privilege statement (DROP,
TRUNCATE, ALTER, GRANT, CREATE USER, ...) are always rejected, since only the
four whitelisted statement types above are ever accepted.
Development
uv sync
uv run ruff format .
uv run ruff check .
uv run pytest -v
uv run uv build # packaging check
Troubleshooting
- Server exits immediately with status 1: a required
MYSQL_*value is missing orMYSQL_MODEis invalid — check stderr for which one. DB_CONNECTION_ERROR: MySQL is unreachable, or the credentials are wrong. The server keeps running and will retry the connection on the next tool call.PERMISSION_DENIEDon insert/update/delete: the server is running inreadonlymode; restart it withMYSQL_MODE=readwriteif writes are intended.
Version history
- 0.1.0 — Initial release:
select/insert/update/deletetools,readonly/readwritemode policy, stdio MCP transport, automatic reconnect-and-retry on lost connections.
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。