@f4ww4z/mcp-mysql-server
一个提供MySQL数据库操作的模型上下文协议服务器。该服务器使AI模型能够通过标准接口与MySQL数据库进行交互。
安装
通过Smithery安装
要自动通过Smithery安装Claude桌面版的MySQL服务器:
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
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

