MySQL MCP Server
MySQL MCP 服务器
用于与 MySQL 数据库交互的 MCP 服务器。
该服务器支持执行只读查询(query)和最终回滚的写查询(test_execute)。
设置
环境变量
将以下环境变量添加到~/.mcp/.env :
MYSQL_HOST=host.docker.internal # Hostname to access host services from Docker container
MYSQL_PORT=3306
MYSQL_USER=root
MYSQL_PASSWORD=your_password注意:
host.docker.internal是一个特殊的 DNS 名称,用于从 Docker 容器访问主机服务。连接到主机上运行的 MySQL 服务器时请使用此设置。如果连接到其他 MySQL 服务器,请更改为相应的主机名。
mcp.json 配置
{
"mcpServers": {
"mysql": {
"command": "docker",
"args": [
"run",
"-i",
"--rm",
"--add-host=host.docker.internal:host-gateway",
"--env-file",
"/Users/username/.mcp/.env",
"ghcr.io/xiangma9712/mcp/mysql"
]
}
}
}Related MCP server: MCP MySQL Server
用法
启动服务器
docker run -i --rm --add-host=host.docker.internal:host-gateway --env-file ~/.mcp/.env ghcr.io/xiangma9712/mcp/mysql注意:如果您使用的是 OrbStack,则会自动支持
host.docker.internal,因此可以省略--add-host选项。虽然 Docker Desktop 通常也自动支持此功能,但为了获得更好的可靠性,建议添加--add-host选项。
可用命令
1.执行只读查询
{
"type": "query",
"payload": {
"sql": "SELECT * FROM your_table"
}
}回复:
{
"success": true,
"data": [
{
"id": 1,
"name": "example"
}
]
}2.测试查询执行
{
"type": "test_execute",
"payload": {
"sql": "UPDATE your_table SET name = 'updated' WHERE id = 1"
}
}回复:
{
"success": true,
"data": "The UPDATE SQL query can be executed."
}3. 列出表格
{
"type": "list_tables"
}回复:
{
"success": true,
"data": ["table1", "table2", "table3"]
}4.描述表
{
"type": "describe_table",
"payload": {
"table": "your_table"
}
}回复:
{
"success": true,
"data": [
{
"Field": "id",
"Type": "int(11)",
"Null": "NO",
"Key": "PRI",
"Default": null,
"Extra": ""
},
{
"Field": "name",
"Type": "varchar(255)",
"Null": "YES",
"Key": "",
"Default": null,
"Extra": ""
}
]
}实现细节
使用 TypeScript 实现
使用 mysql2 包
作为 Docker 容器运行
通过标准输入接受 JSON 命令
通过标准输出返回 JSON 响应
使用
host.docker.internal连接到主机 MySQL(兼容 OrbStack 和 Docker Desktop)
安全注意事项
使用环境变量进行敏感信息管理
预防 SQL 注入是实施者的责任
生产使用需要正确的网络配置
连接主机服务时需要适当的防火墙设置
Available Tools
4 toolsdescribe_tableC
Retrieves column information for a table.
| Name | Required | Description | Default |
|---|---|---|---|
| tableName | Yes |
TDQS
Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?
With no annotations provided, the description carries the full burden of behavioral disclosure. It states the tool retrieves information, implying a read-only operation, but doesn't specify permissions required, rate limits, error handling, or the format of returned column information, leaving significant gaps for a tool with no structured safety hints.
Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.
Is the description appropriately sized, front-loaded, and free of redundancy?
The description is a single, efficient sentence that directly states the tool's purpose without unnecessary words. It's front-loaded and wastes no space, making it easy for an agent to parse quickly.
Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.
Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?
Given no annotations, no output schema, and low schema coverage, the description is incomplete. It lacks details on behavioral traits, parameter constraints, and return values, which are critical for a tool that retrieves structured data. This leaves the agent with insufficient context for reliable invocation.
Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.
Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?
The description mentions 'tableName' implicitly by referring to 'a table', but with 0% schema description coverage and one parameter, it adds minimal semantic value beyond what the schema's property name suggests. Since schema coverage is low, the description doesn't compensate with details like valid table name formats or examples, meeting the baseline for a single parameter.
Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.
Does the description clearly state what the tool does and how it differs from similar tools?
The description clearly states the action ('Retrieves') and resource ('column information for a table'), making the purpose immediately understandable. However, it doesn't explicitly differentiate from siblings like 'list_tables' (which likely lists table names) or 'query' (which might execute SQL queries), missing full sibling distinction.
Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.
Does the description explain when to use this tool, when not to, or what alternatives exist?
The description provides no guidance on when to use this tool versus alternatives like 'list_tables' or 'query'. It lacks context about prerequisites, such as needing an existing table name, or exclusions, leaving the agent to infer usage from the purpose alone.
Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.
list_tablesB
Retrieves a list of tables in the database.
| Name | Required | Description | Default |
|---|---|---|---|
No parameters | |||
TDQS
Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?
With no annotations provided, the description carries full burden for behavioral disclosure. It states it 'retrieves' data (implying read-only), but doesn't mention any constraints like permissions needed, rate limits, pagination, or what format the list returns. This leaves significant behavioral gaps for a tool with zero annotation coverage.
Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.
Is the description appropriately sized, front-loaded, and free of redundancy?
The description is a single, clear sentence with no wasted words. It's front-loaded with the core purpose and efficiently communicates the essential function without redundancy or fluff.
Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.
Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?
For a simple list-retrieval tool with no parameters and no output schema, the description is minimally adequate. However, without annotations or output details, it lacks completeness regarding behavioral aspects like return format or constraints. It meets basic needs but has clear gaps in context.
Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.
Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?
The tool has 0 parameters, and schema description coverage is 100% (though trivial since there's no schema). The description doesn't need to explain parameters, so it appropriately avoids unnecessary detail. A baseline of 4 is justified as it doesn't mislead about parameters that don't exist.
Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.
Does the description clearly state what the tool does and how it differs from similar tools?
The description clearly states the action ('Retrieves') and resource ('list of tables in the database'), making the tool's purpose immediately understandable. However, it doesn't explicitly differentiate from sibling tools like 'describe_table' or 'query', which prevents a perfect score.
Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.
Does the description explain when to use this tool, when not to, or what alternatives exist?
No guidance is provided on when to use this tool versus alternatives like 'describe_table' (for table details) or 'query' (for data retrieval). The description only states what it does, not when it's appropriate, leaving the agent to infer usage context.
Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.
queryB
Executes a read-only SQL query.
| Name | Required | Description | Default |
|---|---|---|---|
| sql | Yes |
TDQS
Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?
With no annotations provided, the description carries the full burden. It discloses 'read-only', indicating non-destructive behavior, which is useful. However, it lacks details on permissions, rate limits, error handling, or response format, leaving significant behavioral gaps for a tool that executes SQL queries.
Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.
Is the description appropriately sized, front-loaded, and free of redundancy?
The description is a single, efficient sentence with zero waste. It's appropriately sized and front-loaded, clearly stating the core action without unnecessary elaboration, making it easy for an agent to parse quickly.
Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.
Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?
Given the complexity of executing SQL queries, no annotations, no output schema, and low schema coverage, the description is incomplete. It lacks information on return values, error cases, or operational context, making it inadequate for safe and effective tool invocation.
Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.
Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?
Schema description coverage is 0%, so the schema provides no parameter details. The description adds minimal semantics by implying the 'sql' parameter is for SQL queries, but it doesn't explain syntax, supported SQL dialects, or constraints, failing to compensate for the low coverage.
Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.
Does the description clearly state what the tool does and how it differs from similar tools?
The description clearly states the verb ('executes') and resource ('SQL query'), making the purpose understandable. However, it doesn't differentiate from sibling tools like 'test_execute' or 'describe_table', which might also involve query execution or database operations, so it's not fully specific to sibling context.
Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.
Does the description explain when to use this tool, when not to, or what alternatives exist?
The description provides no guidance on when to use this tool versus alternatives like 'test_execute' or 'list_tables'. It mentions 'read-only', which hints at usage context, but lacks explicit when/when-not instructions or named alternatives, leaving the agent with minimal direction.
Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.
test_executeB
Checks if an SQL query can be executed and rolls back afterward.
| Name | Required | Description | Default |
|---|---|---|---|
| sql | Yes |
TDQS
Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?
No annotations are provided, so the description carries the full burden. It discloses key behavioral traits: the tool checks executability and performs a rollback, indicating it's a safe, non-destructive operation. However, it doesn't mention error handling, performance implications, or what 'checks' entails (e.g., syntax validation, permission checks), leaving gaps for a mutation-related tool.
Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.
Is the description appropriately sized, front-loaded, and free of redundancy?
The description is a single, efficient sentence: 'Checks if an SQL query can be executed and rolls back afterward.' It's front-loaded with the core purpose, has zero waste, and appropriately sized for the tool's complexity.
Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.
Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?
Given the tool involves SQL execution testing (a mutation-related operation), no annotations, no output schema, and low parameter coverage, the description is incomplete. It lacks details on return values (e.g., success/failure indicators), error responses, or rollback specifics, making it inadequate for safe agent use.
Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.
Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?
Schema description coverage is 0%, so the description must compensate. It mentions 'SQL query' as the input, which adds meaning to the 'sql' parameter by specifying it's an SQL query string. However, it doesn't provide details on query format, supported SQL dialects, or constraints, failing to fully compensate for the low coverage.
Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.
Does the description clearly state what the tool does and how it differs from similar tools?
The description clearly states the tool's purpose: 'Checks if an SQL query can be executed and rolls back afterward.' This specifies the verb ('checks') and resource ('SQL query'), and distinguishes it from siblings like 'query' (which likely executes queries) and 'describe_table'/'list_tables' (which are read-only metadata tools). However, it doesn't explicitly differentiate from 'query' beyond implying a test vs. execution distinction, keeping it at 4 rather than 5.
Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.
Does the description explain when to use this tool, when not to, or what alternatives exist?
The description implies usage context: it's for testing SQL queries before actual execution, suggesting when to use it (to validate queries) versus alternatives like 'query' (for actual execution). However, it doesn't explicitly state when-not-to-use or name alternatives, and lacks prerequisites (e.g., database connection requirements), so it's not fully explicit.
Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.
Tool Schema Changelog
Recent tool additions, removals, and schema changes observed during successful MCP inspections.
4 tool updates
v1.0.0- Added
describe_table - Added
list_tables - Added
query - Added
test_execute
TDQS
Scored across 4 tools
Each tool has a clearly distinct purpose: describe_table for column metadata, list_tables for table enumeration, query for read-only SQL execution, and test_execute for query validation with rollback. There is no overlap in functionality, making tool selection straightforward for an agent.
The naming follows a consistent verb_noun pattern (describe_table, list_tables, query, test_execute), with 'query' as a minor deviation as it lacks a noun suffix. Overall, the pattern is predictable and readable, though not perfectly uniform.
With 4 tools, the count is reasonable for a database server, covering essential operations like listing tables, describing schema, querying, and testing queries. It is slightly lean but well-scoped, lacking only advanced features like write operations or transaction management.
The toolset covers basic read and validation operations but has notable gaps: there are no tools for data manipulation (e.g., insert, update, delete), schema modification (e.g., create_table), or transaction control. This limits the server to read-only and diagnostic tasks, which may cause agent failures for write workflows.
Maintenance
Related MCP Connectors
PostgreSQL, MySQL, OpenAPI/Swagger, and shared Agent Memory with scoped access.
Connect to PlanetScale databases, branches, schema, query insights, and execute SQL
Generate, fix, explain and run read-only SQL on PostgreSQL, MySQL and SQL Server
- mcpOAuthcom.gibsonai
GibsonAI MCP server: manage your databases with natural language
Related MCP Servers
- FlicenseNot gradedqualityDmaintenanceEnables secure interaction with MySQL databases, allowing AI assistants to list tables, read data, and execute SQL queries through a controlled interface.-
- AlicenseNot gradedqualityDmaintenanceEnables interaction with MySQL databases through MCP, supporting query execution, table operations (insert, update, delete), and schema inspection for natural language database management.158 npmMIT
- AlicenseNot gradedqualityDmaintenanceEnables AI models to interact with MySQL databases through standardized operations including querying, executing commands, listing tables, and describing table structures with secure prepared statement support.180 npmMIT
- FlicenseAqualityDmaintenanceEnables querying local MySQL databases and managing Docker containers (listing, logs) through natural language via the Model Context Protocol.425 npm-