Skip to main content
Glama
mysql-mcp

mysql-mcp

by sundonglai03

mysql-mcp

一个支持 stdio 和 Streamable HTTP 的 MySQL MCP Server。模型可以通过工具查询表、执行受控 SQL,以及插入、更新和删除数据。

快速开始

cd /path/to/mysql-mcp
uv sync

stdio(桌面 MCP 客户端)

{
  "mcpServers": {
    "mysql-mcp": {
      "command": "/path/to/mysql-mcp/.venv/bin/python",
      "args": ["-m", "mysql_mcp"]
    }
  }
}

使用 .venv/bin/python 的绝对路径;不要在客户端配置中依赖 cwd 或 uv run。

Streamable HTTP

.venv/bin/python -m mysql_mcp.mcp_server \
  --transport streamable-http --host 127.0.0.1 --port 8000 --path /mcp

MCP 地址:http://127.0.0.1:8000/mcp。这是 MCP 协议端点,不是普通网页。 可信内网可以使用 HTTP;跨不可信网段时建议设置 MYSQL_MCP_AUTH_TOKEN 或使用 --auth-token。没有证书也可以先用 HTTP + Token,HTTPS 不是运行前提。

Docker

docker compose up -d --build
docker compose logs -f mysql-mcp
docker compose down

镜像名为 sundonglai/mysql-mcp:latest,容器名为 mysql-mcp。Compose 默认监听宿主机 所有网卡的 8002(容器内仍是 8000),局域网内用 http://<宿主机IP>:8002/mcp 访问; 这样可以与 contracts 和 Office 服务同机运行。

服务没有连接档案、remember 或兼容接口。数据库凭据必须由每次工具调用提供, 连接关闭后立即丢弃。

Related MCP server: Local MySQL Read-only MCP Server

连接数据库

每次业务调用都直接传入数据库凭据:

list_tables(credentials={
  "host": "db.example.com",
  "user": "reader",
  "password": "...",
  "database": "app"
})

服务只在内存中使用凭据,连接关闭后不会保存密码。

工具

工具

作用

health_check

检查连接和 MySQL 版本

get_databases

列出数据库

list_tables

列出表

describe_table

查看表结构

fetch_table

查询表数据,支持结构化过滤和排序

count_rows

统计行数

execute_query

执行单条 SQL,默认只读

insert_row / update_rows / delete_rows

数据变更

安全边界

  • execute_query 默认只允许 SELECT、SHOW、DESCRIBE、EXPLAIN。

  • 永远拒绝多语句 SQL;表名、字段名经过标识符校验,值使用参数绑定。

  • 查询默认返回 50 行,服务端上限 500 行。

  • 结构化更新和删除默认最多影响 1000 行,硬上限 10000 行。

  • read_only=true 的连接拒绝写操作。

  • execute_query(read_only=false) 仍是高权限通道,真正的权限控制应使用 MySQL 账号权限。

  • Docker 默认只绑定回环地址。可信内网开放端口时至少设置 Token;跨不可信网络再增加 HTTPS。

项目结构

src/mysql_mcp/
├── mcp_server.py   # MCP transport 入口
├── server.py       # 工具和 SQL 策略
├── client.py       # MySQL 连接生命周期
└── credentials.py  # 单次凭据校验和 TLS 配置(无文件读写)

开发

.venv/bin/python -m pytest
.venv/bin/ruff check .
.venv/bin/ruff format --check .
docker compose config

不要提交 .venv/、Docker 数据卷或任何密码文件。

License

MIT

Available Tools

11 tools
count_rowsD

Inputs: table_name (required), where. Returns: object

delete_rowsD

Inputs: where (required), table_name (required), where_params. Returns: object

describe_tableD

Inputs: table_name (required). Returns: result

execute_queryD

Inputs: sql (required), limit, read_only. Returns: object

execute_sqlD

Inputs: sql (required), limit, read_only. Returns: object

fetch_tableD

Inputs: table_name (required), limit, where, order_by. Returns: object

get_databasesD

Inputs: no named parameters. Returns: result

health_checkD

Inputs: no named parameters. Returns: object

insert_rowD

Inputs: row (required), table_name (required). Returns: object

list_tablesD

Inputs: no named parameters. Returns: result

update_rowsD

Inputs: where (required), updates (required), table_name (required), where_params. Returns: object

Tool Schema Changelog

Recent tool additions, removals, and schema changes observed during successful MCP inspections.

Loading tool schema changelog…

    TDQS

    D1.8/5.0

    Scored across 11 tools

    Disambiguation2/5

    Several tools have unclear boundaries: execute_sql and execute_query appear to do nearly the same thing, and fetch_table may overlap with execute_query for reading data. An agent could easily select the wrong generic tool despite the clear CRUD/schema tools.

    Naming Consistency4/5

    Most names follow a predictable verb_noun snake_case pattern (list_tables, count_rows, delete_rows). Minor inconsistencies exist, such as insert_row singular vs update_rows/delete_rows pluralched and execute_sql vs execute_query using different nouns for the same conceptual operation.

    Tool Count4/5

    11 tools is a reasonable size for a database MCP server case. However, execute_sql and execute_query are redundant, so not every tool clearly earns its place.

    Completeness4/5

    The set covers health, database/table listing, schema inspection, row CRUD, counting, and arbitrary SQL, so core database operations are well covered. Dedicated table lifecycle operations are missing but can be handled through execute_sql, making this a minor gap rather than a fatal one.

    Maintenance

    ActivitySlowing
    ResponsivenessNo recent community issues

    Related MCP Connectors

    Related MCP Servers

    • F
      license
      Not graded
      quality
      D
      maintenance
      Provides MySQL database integration for AI assistants and other MCP clients, allowing them to list tables, read table data, and execute SQL queries.
      -
    • F
      license
      Not graded
      quality
      D
      maintenance
      Provides a read-only interface to local MySQL databases via MCP, enabling listing databases, tables, describing schemas, and querying rows with structured filters.
      -
    • F
      license
      A
      quality
      D
      maintenance
      MCP server that lets clients run SQL (select/insert/update/delete) against a MySQL database, with readonly and readwrite modes.
      4
      -