Skip to main content
Glama
xujcbiz

mysql-mcp-server

by xujcbiz

mysql-mcp-server

一个基于 stdio 的只读 MySQL MCP Server。它为支持 Model Context Protocol 的客户端提供 MySQL 查询、库表浏览和表结构查看能力,并在执行前对 SQL 做只读校验。

功能特性

  • 通过 MCP stdio transport 暴露 MySQL 只读工具

  • 支持 SELECTWITH SELECTSHOWDESCRIBE / DESC 和安全的 EXPLAIN

  • 拒绝多语句、写入语句、管理语句、锁相关语句和高风险函数

  • 使用 START TRANSACTION READ ONLY 执行查询,并在查询后回滚事务

  • 支持结果行数上限、查询超时、连接池和可选 SSL

Related MCP server: MySQL MCP Server

环境要求

  • Node.js >= 20

  • MySQL 兼容数据库

  • 一个建议只授予读取权限的 MySQL 用户

安装与构建

npm install
npm run build

本地开发可使用:

npm run dev

构建后启动:

npm start

使用 MCP Inspector 调试:

npm run inspect

配置

服务通过环境变量读取 MySQL 连接配置。可以参考 .env.example

MYSQL_HOST=127.0.0.1
MYSQL_PORT=3306
MYSQL_USER=readonly_user
MYSQL_PASSWORD=change-me
MYSQL_DATABASE=
MYSQL_SSL=false
MYSQL_SSL_REJECT_UNAUTHORIZED=true
MYSQL_CONNECTION_LIMIT=5
MYSQL_CONNECT_TIMEOUT_MS=10000
MYSQL_QUERY_TIMEOUT_MS=30000
MYSQL_MAX_ROWS=500

变量

必填

默认值

说明

MYSQL_HOST

127.0.0.1

MySQL 主机

MYSQL_PORT

3306

MySQL 端口

MYSQL_USER

-

MySQL 用户名

MYSQL_PASSWORD

-

MySQL 密码

MYSQL_DATABASE

-

默认数据库,留空则不指定

MYSQL_SSL

false

是否启用 SSL

MYSQL_SSL_REJECT_UNAUTHORIZED

true

SSL 是否校验证书

MYSQL_CONNECTION_LIMIT

5

连接池连接数上限

MYSQL_CONNECT_TIMEOUT_MS

10000

建连超时时间,单位毫秒

MYSQL_QUERY_TIMEOUT_MS

30000

查询超时时间,单位毫秒

MYSQL_MAX_ROWS

500

单次返回的最大行数

布尔值支持 1trueyeson0falsenooff

MCP 客户端配置示例

发布到 npm 后,可以在 MCP 客户端配置中加入:

{
  "mcpServers": {
    "mysql": {
      "command": "npx",
      "args": [
        "-y",
        "@xujcbiz/mysql-mcp-server"
      ],
      "env": {
        "MYSQL_HOST": "127.0.0.1",
        "MYSQL_PORT": "3306",
        "MYSQL_USER": "readonly_user",
        "MYSQL_PASSWORD": "change-me",
        "MYSQL_DATABASE": "",
        "MYSQL_SSL": "false",
        "MYSQL_MAX_ROWS": "500"
      }
    }
  }
}

也可以参考仓库中的 mcp-config.example.json。本地开发调试时,可以先执行 npm run build,再临时使用 node D:/project/mysql-mcp-server/dist/index.js 这种绝对路径方式。

可用工具

mysql_query

执行只读 SQL。

输入:

{
  "sql": "SELECT * FROM users LIMIT 10",
  "maxRows": 100
}

maxRows 可选,不能超过 MYSQL_MAX_ROWS

mysql_list_databases

列出当前 MySQL 用户可见的数据库。

输入:

{
  "pattern": "app_%"
}

pattern 可选,使用 MySQL LIKE 语法。

mysql_list_tables

列出当前或指定数据库下的表和视图。

输入:

{
  "database": "app_db",
  "pattern": "user%",
  "includeViews": true
}

mysql_describe_table

查看表字段信息,可选返回索引信息。

输入:

{
  "database": "app_db",
  "table": "users",
  "includeIndexes": true
}

mysql_connection_info

返回当前数据库、连接用户、有效用户和 MySQL 版本。

输入:

{}

返回格式

查询类工具会返回 JSON 文本和结构化结果,结构大致如下:

{
  "rows": [],
  "fields": [],
  "rowCount": 0,
  "truncated": false,
  "executionMs": 0
}
  • rows:返回的数据行

  • fields:字段名列表

  • rowCount:查询得到的总行数

  • truncated:结果是否因 maxRows 被截断

  • executionMs:执行耗时,单位毫秒

只读安全策略

服务会在执行前对 SQL 做校验:

  • 仅允许 SELECTWITH SELECTSHOWDESCRIBE / DESCEXPLAIN

  • 禁止多条 SQL 语句

  • 禁止 INSERTUPDATEDELETEALTERDROPCREATETRUNCATE 等写入或管理语句

  • 禁止 SELECT ... INTOFOR UPDATELOCK IN SHARE MODE 等可能产生副作用或锁的语法

  • 禁止 SLEEPBENCHMARKGET_LOCK 等高风险函数

仍然建议在数据库侧使用只读账号,并只授予必要库表的读取权限。

项目结构

src/
  config.ts      环境变量配置读取与校验
  database.ts    MySQL 连接池和只读查询封装
  index.ts       MCP Server 入口和工具注册
  sqlSafety.ts   SQL 只读校验与标识符转义

常用命令

npm run build    # 编译 TypeScript 到 dist/
npm run dev      # 使用 tsx 运行源码
npm start        # 运行 dist/index.js
npm run inspect  # 使用 MCP Inspector 调试

Available Tools

5 tools
mysql_connection_infoMySQL connection infoA

Return current database, user, effective user, and server version.

ParametersJSON Schema
NameRequiredDescriptionDefault

No parameters

TDQS

A4.3/5.0
Behavior4/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations, the description carries the full burden. It transparently lists all four pieces of information returned, making the tool's behavior clear. It doesn't discuss permissions or error conditions, but for a read-only info tool this is acceptable.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single, front-loaded sentence with no redundancy or wasted words.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness5/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

For a zero-parameter information tool with no output schema, the description fully captures the returned fields, making it complete within its scope.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters4/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

The tool takes zero parameters, so the description needs no further explanation. Baseline 4 applies.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description explicitly states the tool returns current database, user, effective user, and server version, which is a specific and distinct purpose from sibling tools like mysql_query or mysql_list_databases.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines3/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

Usage is implied by the purpose — it's the tool to call when you need connection context — but there is no explicit guidance on when to use it or when to prefer an alternative.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

mysql_describe_tableDescribe MySQL tableA

Return column metadata and optional indexes for a table.

ParametersJSON Schema
NameRequiredDescriptionDefault
tableYesTable name.
databaseNoOptional database name.
includeIndexesNoWhether to include index metadata.

TDQS

A3.5/5.0
Behavior3/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

No annotations are provided, so the description carries the full burden of behavioral disclosure. It states the output ('column metadata and optional indexes') but does not explicitly confirm that the operation is read-only, describe error behavior, or mention permission requirements. The read-only nature is implied by the name 'describe', which is a conventional MySQL operation, so this is moderately transparent but not fully disclosed.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single, front-loaded sentence that immediately conveys the tool's purpose without redundant words or additional filler. Every word contributes meaning.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness3/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

For a simple introspection tool, the description provides a high-level summary of the return value ('column metadata and optional indexes') but does not elaborate on what fields the column metadata includes (e.g., type, nullable, key). Given there is no output schema, more detail would help complete the picture, but the description is not severely inadequate.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

The input schema has 100% description coverage for all three parameters, with each parameter including a meaningful description. The tool description does not add additional parameter semantics, which is acceptable because the schema already documents them clearly.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description uses a specific verb ('Return') and clearly identifies the resource ('table') and the returned content ('column metadata and optional indexes'). It distinguishes this tool from sibling tools like mysql_query, mysql_list_tables, and mysql_list_databases by focusing on table schema introspection.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description gives no guidance on when to use this tool vs alternatives, such as when to choose it over mysql_query or mysql_list_tables. No exclusions or context are provided, leaving usage up to inference from the tool name and sibling names.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

mysql_list_databasesList MySQL databasesA

List databases visible to the configured read-only MySQL user.

ParametersJSON Schema
NameRequiredDescriptionDefault
patternNoOptional MySQL LIKE pattern.

TDQS

A3.8/5.0
Behavior3/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries the burden of disclosing behavior. It adds context by noting the 'read-only' user and visibility scope, but does not describe return format, pattern filtering effects, or error behavior. This is some, but not rich, behavioral transparency.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single, front-loaded sentence with no unnecessary words. Every word contributes to conveying the tool's purpose and access scope.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness4/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

For a simple read-only listing operation with one optional parameter, the description provides sufficient context (action, resource, scope). The schema covers the parameter, and the absence of an output schema is mitigated by the obvious return type implied by 'List'. Could be slightly more detailed, but adequate for this low complexity.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

The single optional parameter 'pattern' is fully described in the schema as 'Optional MySQL LIKE pattern.' The tool description adds no additional meaning beyond this, so the baseline score of 3 is appropriate.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the tool's action ('List') and resource ('databases'), with a specific scope ('visible to the configured read-only MySQL user'). It distinguishes from sibling tools like mysql_list_tables and mysql_query, which target different resources or operations.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines3/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description implies usage for enumerating databases but does not explicitly state when to prefer this over alternatives like mysql_query or mysql_list_tables. It also lacks mention of prerequisites or exclusions, though the 'read-only' phrasing hints at safe usage.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

mysql_list_tablesList MySQL tablesA

List tables and views for the current or selected database.

ParametersJSON Schema
NameRequiredDescriptionDefault
patternNoOptional MySQL LIKE pattern.
databaseNoOptional database name.
includeViewsNoWhether to include views.

TDQS

A3.5/5.0
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries the full behavioral disclosure burden. It states it lists tables and views but does not mention the default inclusion of views, how the 'selected database' is determined, output format, or any side effects. This is minimal coverage beyond the raw capability.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single, clear sentence that conveys the action, resource, and scope without any filler. It is appropriately sized for a list operation and front-loads the essential information.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness3/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

For a simple list tool with no output schema and no annotations, the description gives a basic understanding of what it does. However, it lacks details about return format, behavior when database is omitted, and whether views are included by default. It is adequate but has clear gaps.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema coverage is 100% because all three parameters (pattern, database, includeViews) have descriptions. The description's mention of 'current or selected database' adds slight context to the database parameter, but it essentially repeats the schema. Baseline 3 is appropriate.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description uses the specific verb 'List' with the resource 'tables and views' and scopes it to 'the current or selected database.' This clearly distinguishes the tool from siblings like mysql_list_databases (lists databases) and mysql_describe_table (describes a single table).

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines3/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides context that the tool operates on 'the current or selected database,' implying use when you need to discover tables/views. However, it does not explicitly mention when to prefer this over siblings like mysql_describe_table or mysql_query, nor does it state any exclusions.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

mysql_queryMySQL read-only queryA

Run a read-only MySQL query. Allowed statements: SELECT, WITH SELECT, SHOW, DESCRIBE/DESC, and safe EXPLAIN.

ParametersJSON Schema
NameRequiredDescriptionDefault
sqlYesRead-only SQL statement to execute.
maxRowsNoMaximum rows to return, up to 500.

TDQS

A4.2/5.0
Behavior4/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations, the description carries the full burden and discloses the critical behavioral constraint: only read-only statements are allowed, with explicit statement types. The term 'safe EXPLAIN' adds nuance beyond the schema. It does not cover failure modes or permissions, but the core safety behavior is clearly stated.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

Two sentences contain the essential information without padding. The first sentence states the action and constraint, and the second enumerates the allowed statements. Front-loaded and easy to scan.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness4/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

For a simple two-parameter tool with no output schema, the description covers the core usage context: what query types are permitted and that it is read-only. It could mention the result shape, but for a generic query tool that is reasonably obvious; the schema handles parameter details.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 100%: sql is described as read-only SQL and maxRows has its limit and bounds documented. The description adds no additional parameter meaning beyond what the schema already provides, so it stays at the baseline.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description uses a specific verb ('Run') and resource ('MySQL query'), clearly stating it executes read-only statements. The allowed statements list (SELECT, WITH SELECT, SHOW, DESCRIBE/DESC, safe EXPLAIN) differentiates it from sibling tools that list databases/tables or describe tables.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines4/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

It establishes clear context: this is the generic read-only query tool, while specialized siblings like mysql_list_databases, mysql_list_tables, and mysql_describe_table cover narrower cases. However, it does not explicitly name alternatives or provide exclusion rules like 'use mysql_describe_table when you only need a table schema'.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

TDQS

A3.9/5.0
Disambiguation5/5

Each tool has a distinct purpose: querying, listing databases, listing tables, describing table structure, and retrieving connection metadata. There is no overlap or ambiguity between them.

Naming Consistency4/5

All tools share the mysql_ prefix and most follow a verb_noun pattern (list_databases, list_tables, describe_table, query). mysql_connection_info is slightly inconsistent as it uses a noun phrase instead of a verb, but the pattern is still clear and predictable.

Tool Count5/5

Five tools is well-scoped for a read-only MySQL introspection server. Each tool serves a clear and necessary function without redundancy or bloat.

Completeness4/5

The toolset covers the core read-only operations: generic querying, database/table listing, schema description, and connection info. The only minor gap is a lack of a dedicated tool for viewing table indexes without using describe_table, but that is covered by the optional indexes parameter. Overall, the surface is complete for its intended purpose.

Maintenance

ActivityInactive
ResponsivenessNo issues

Resources

Unclaimed servers have limited discoverability.

Looking for Admin?

If you are the server author, to access and configure the admin panel.

Related MCP Connectors

Related MCP Servers

  • A
    license
    B
    quality
    D
    maintenance
    Provides secure, read-only access to MariaDB/MySQL databases, allowing users to list databases, explore table schemas, and execute SQL queries with built-in security measures.
    4
    72
    4
    MIT
  • F
    license
    Not graded
    quality
    D
    maintenance
    Enables safe interaction with MySQL databases through SELECT queries, table structure inspection, and database schema exploration. Provides read-only access to query data and examine database metadata.
    1
  • A
    license
    Not graded
    quality
    D
    maintenance
    Enables schema introspection and safe read-only queries on MySQL databases, supporting table/column discovery, relationship exploration, and controlled SELECT queries.
    25
    MIT
  • A
    license
    Not graded
    quality
    C
    maintenance
    Provides secure, read-only access to a single MySQL database for schema inspection and querying.
    66
    5
    MIT

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/xujcbiz/mysql-mcp-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server