mysql_mcp_server
一个提供MySQL数据库操作的模型上下文协议服务器。该服务器使AI模型能够通过标准化接口与MySQL数据库进行交互。
MCP 服务配置
复制以下 JSON 到 OPClaw 或其他 MCP 客户端的配置文件中即可使用
{
"mcpServers": {
"mysql": {
"args": [
"-y",
"@f4ww4z/mcp-mysql-server"
],
"command": "npx",
"env": {
"MYSQL_DATABASE": "your_database",
"MYSQL_HOST": "your_host",
"MYSQL_PASSWORD": "your_password",
"MYSQL_USER": "your_user"
}
}
}
}
该服务需要配置环境变量:MYSQL_DATABASE、MYSQL_HOST、MYSQL_PASSWORD、MYSQL_USER
可用工具 (5 个)
该服务在 MCP 协议中暴露的工具,AI 可按需调用
connect_db 4 个参数 需填 4 项
Connect to MySQL database
必填参数:host、user、password、database
query 2 个参数 需填 1 项
Execute a SELECT query
必填参数:sql
execute 2 个参数 需填 1 项
Execute an INSERT, UPDATE, or DELETE query
必填参数:sql
list_tables
List all tables in the database
该工具无需必填参数,直接调用即可
describe_table 1 个参数 需填 1 项
Get table structure
必填参数:table
服务介绍
@f4ww4z/mcp-mysql-server
这是一个提供MySQL数据库操作的Model Context Protocol服务器。该服务器使AI模型能够通过标准化接口与MySQL数据库进行交互。
安装
通过Smithery安装
要通过Smithery自动为Claude Desktop安装MySQL Server:
npx -y @smithery/cli install @f4ww4z/mcp-mysql-server --client claude
手动安装
npx @f4ww4z/mcp-mysql-server
配置
服务器需要在您的MCP设置配置文件中设置以下环境变量:
推荐使用
{
"mcpServers": {
"mysql": {
"command": "npx",
"args": ["-y", "@f4ww4z/mcp-mysql-server", "mysql://user:password@localhost:port/database"],
}
}
}
{
"mcpServers": {
"mysql": {
"command": "npx",
"args": ["-y", "@f4ww4z/mcp-mysql-server"],
"env": {
"MYSQL_HOST": "your_host",
"MYSQL_USER": "your_user",
"MYSQL_PASSWORD": "your_password",
"MYSQL_DATABASE": "your_database"
}
}
}
}
运行评估
evals包加载一个mcp客户端,然后运行index.ts文件,因此在测试之间无需重建。您可以通过在npx命令前添加前缀来加载环境变量。完整文档请参阅这里。
OPENAI_API_KEY=your-key npx mcp-eval src/evals/evals.ts src/index.ts
可用工具
1. connect_db
使用提供的凭据建立与MySQL数据库的连接。
use_mcp_tool({
server_name: "mysql",
tool_name: "connect_db",
arguments: {
host: "localhost",
user: "your_user",
password: "your_password",
database: "your_database"
}
});
2. query
执行SELECT查询,并可选地使用预处理语句参数。
use_mcp_tool({
server_name: "mysql",
tool_name: "query",
arguments: {
sql: "SELECT * FROM users WHERE id = ?",
params: [1]
}
});
3. execute
执行INSERT、UPDATE或DELETE查询,并可选地使用预处理语句参数。
use_mcp_tool({
server_name: "mysql",
tool_name: "execute",
arguments: {
sql: "INSERT INTO users (name, email) VALUES (?, ?)",
params: ["John Doe", "john@example.com"]
}
});
4. list_tables
列出已连接数据库中的所有表。
use_mcp_tool({
server_name: "mysql",
tool_name: "list_tables",
arguments: {}
});
5. describe_table
获取特定表的结构。
use_mcp_tool({
server_name: "mysql",
tool_name: "describe_table",
arguments: {
table: "users"
}
});
特性
- 自动清理的安全连接处理
- 支持带有查询参数的预处理语句
- 全面的错误处理和验证
- 支持TypeScript
- 自动连接管理
安全性
- 使用预处理语句防止SQL注入
- 通过环境变量支持安全密码处理
- 在执行前验证查询
- 完成后自动关闭连接
错误处理
服务器为常见问题提供了详细的错误信息:
- 连接失败
- 无效查询
- 缺少参数
- 数据库错误
贡献
欢迎贡献!请随时向https://github.com/f4ww4z/mcp-mysql-server提交Pull Request
许可证
MIT