mysql-mcp
An MCP server that enables secure, read-only access to MySQL databases with structured query tools, and optional opt-in write operations when explicitly enabled.
README
mysql-mcp
Query MySQL tables from Claude Code — read-only by default.
An MCP server that gives Claude Code (and any MCP client) read access to a MySQL database through structured, parameterized query tools. No raw SQL passthrough by default — the tool surface is designed so the worst possible outcome is a SELECT that returns too many rows.
Modeled on Agent360's browser-mcp (same MCP server patterns: stdio transport, lifecycle handling, LLM-facing tool descriptions).
Testing
npm test
Three layers, all under node --test (no test framework dependency):
| File | Layer | Needs DB? |
|---|---|---|
| test/query-builder.test.js | SQL builder security contract (identifiers, params, clamps, footgun defense) | No |
| test/error-classifier.test.js | Error → LLM-hint mapping + result truncation | No |
| test/integration.test.js | Full MCP protocol against a real MySQL, via test/helpers/mcp-client.js | Yes — suites skip gracefully when unreachable |
Integration env (defaults match the local Docker dev setup; override freely):
TEST_DB_HOST / TEST_DB_PORT / TEST_DB_USER / TEST_DB_PASSWORD / TEST_DB_NAME
TEST_DB_RW_USER / TEST_DB_RW_PASSWORD → write suite (skipped if unset)
TEST_DB_ADMIN_PASSWORD → cleanup of write-test rows
Status — M5
Five read tools plus two opt-in write tools, all integration-tested against a real MySQL:
| Tool | What it does |
|---|---|
ping |
Verify MCP + MySQL connectivity, returns guiding errors |
list_tables |
SHOW TABLES |
describe_table |
Column names/types/keys/defaults/comments via information_schema |
query_table |
Structured queries — key/value WHERE, column lists, order, limit (hard cap 200) |
check_permissions |
Shows the account's grants and whether it is read-only |
insert_row (opt-in) |
INSERT + read-back of the inserted row |
update_rows (opt-in) |
UPDATE with REQUIRED where, default LIMIT 1 (cap 100) + read-back of the new state |
Writing — opt-in at two levels
Write tools exist only when both hold:
- Server: started with
ALLOW_WRITES=1— otherwise the tools are not even advertised intools/list, and dispatch refuses them anyway. - Database: the connected account holds INSERT/UPDATE grants
(verify with
check_permissions).
Safety defaults on update_rows: where is required (a where-less UPDATE is
rejected — the classic footgun), limit defaults to 1 and caps at 100.
Every write reads the affected rows back and returns the verified new state.
Security — three tiers, all enforced
| Tier | Defense | Where |
|---|---|---|
| 1. Database | SELECT-only account (GRANT SELECT, ideally on a replica) |
Your DBA work — verified by check_permissions |
| 2. Connection | multipleStatements: false, connectTimeout: 5s, per-query timeout 10s (DB_QUERY_TIMEOUT_MS to tune) |
db.js |
| 3. Application | No raw SQL passthrough; identifier allowlist + ?? escaping; values via ? only; LIMIT hard cap 200 |
db.js buildSelectQuery |
Every tool call is audited to stderr — shape only (tool, table name, row count), never values. Same secret-hygiene contract as browser-mcp's action log.
Reliability & LLM experience
- Errors are LLM-actionable. Raw MySQL/network errors are classified and
shipped with a hint that says what to do next:
Error: Table 'x' doesn't exist→Hint: Call list_tables...,Unknown column→Hint: Call describe_table..., connection failures →Hint: Check DB_HOST / DB_PORT...(marked retryable). - Retry policy. Transient connection failures retry once automatically (SELECTs are idempotent); SQL errors never retry. Same read-only/retryable split as browser-mcp's CDP whitelist.
- Result truncation. Results are capped at 50,000 serialized chars
(on top of the 200-row cap). When truncated, the response carries
truncated: true,fetched, and a hint telling the model to narrow (WHERE filters, fewer columns, offset paging) instead of re-querying bigger.
Roadmap
- ✅ M1–M6 core done: skeleton, query tools, security, reliability, opt-in writes, test suite
- ⏸️ npm publishing — skipped by decision; add
@scopename, LICENSE, registry metadata (server.json/glama.json) if you ever publish
Install (local — no npm publish needed)
cd /path/to/mysql-mcp
node bin/cli.js install
This writes the mysql-mcp server into ~/.claude/mcp.json, pointing at this
checkout's index.js by absolute path (dev-mode install, same pattern as
browser-mcp's install.sh). Credentials use ${VAR} placeholders — Claude Code
resolves them from your environment, never stored in the config file:
{
"mcpServers": {
"mysql-mcp": {
"command": "node",
"args": ["/path/to/mysql-mcp/index.js"],
"env": {
"DB_HOST": "${DB_HOST}",
"DB_PORT": "${DB_PORT}",
"DB_USER": "${DB_USER}",
"DB_PASSWORD": "${DB_PASSWORD}",
"DB_NAME": "${DB_NAME}"
}
}
}
}
Database account — read-only, always
CREATE USER 'mcp_ro'@'%' IDENTIFIED BY '<strong-password>';
GRANT SELECT ON <your_db>.* TO 'mcp_ro'@'%';
Point it at a read replica if you have one.
Dev
npm ci
node index.js # → [mysql-mcp] MySQL MCP server running (stdio)
Environment variables
| Var | Default | Description |
|---|---|---|
DB_HOST |
127.0.0.1 |
MySQL host |
DB_PORT |
3306 |
MySQL port |
DB_USER |
(required) | MySQL user |
DB_PASSWORD |
(empty) | MySQL password |
DB_NAME |
(none) | Default database |
DB_QUERY_TIMEOUT_MS |
10000 |
Per-query timeout in ms |
ALLOW_WRITES |
(unset) | Set to 1 to enable insert_row / update_rows |
License
MIT
推荐服务器
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 模型以安全和受控的方式获取实时的网络信息。