DB MCP Server是一款强大的服务,能帮助你高效地进行数据库相关操作。本指南将详细介绍其快速开始、安装、使用等方面的内容,助你顺利使用该服务。
要快速启动并运行DB MCP Server,请按照以下步骤操作:
git clone https://github.com/FreePeak/db-mcp-server.git
cd db-mcp-server
go mod tidy
go build -o bin/server .
./bin/server --config=config.json
docs/目录下的详细使用说明。确保已安装以下工具:
在终端中运行:
git clone https://github.com/FreePeak/db-mcp-server.git
cd db-mcp-server
执行以下命令安装项目所需的Go包:
go mod tidy
构建服务器二进制文件:
go build -o bin/server .
创建config.json配置文件,参考示例内容:
{
"port": 9095,
"mode": "sae", // 可选值:sae、http
"connections": [
{
"id": "mysql1",
"type": "mysql",
"host": "localhost",
"port": 3306,
"user": "root",
"password": "password",
"name": "testdb"
}
]
}
运行以下命令启动服务器:
./bin/server --config=config.json
在浏览器或其他客户端中,可以通过如下方式连接:
const eventSource = new EventSource('http://localhost:9095/sse');
eventSource.onmessage = (event) => {
console.log('Received:', event.data);
};
通过REST API进行交互,支持JSON请求和响应。
config.json主要包含以下配置项:
| 属性 | 详情 |
|---|---|
port |
服务监听的端口,默认为9095。 |
mode |
运行模式,可选值为sae或http,默认为sae(支持SSE)。 |
connections |
数据库连接池配置,每个连接包含以下属性: - id:唯一标识符。- type:数据库类型(mysql、postgres等)。- host:数据库主机地址。- port:数据库端口。- user:数据库用户名。- password:用户密码。- name:数据库名称。 |
{
"tool": "query_mysql",
"params": {
"query": "SELECT * FROM users LIMIT 10"
}
}
{
"tool": "transaction_mysql",
"params": {
"operations": [
{ "query": "UPDATE orders SET status = 'paid'" },
{ "query": "INSERT INTO log (action) VALUES ('order paid')" }
]
}
}
config.json中的数据库参数是否正确。max_open_conns和max_idle_conns。⚠️ 重要提示
如果遇到问题或有改进建议,请随时在GitHub上提出issue。