基于MCP实现text2sql

目的:基于MCP实现text2sql能力

参考:https://blog.csdn.net/hacker_Lees/article/details/146426392

服务端

  1. 选用开源的MySQL MCP Server,本地部署:https://github.com/dpflucas/mysql-mcp-server
shell 复制代码
# Clone the repository
git clone https://github.com/dpflucas/mysql-mcp-server.git
cd mysql-mcp-server

# Install dependencies and build
npm install
npm run build

客户端

使用Cherry Studio作为客户端,内置接入MCP服务器功能,模型配置省略。

MCP有两种模式:

txt 复制代码
1)STDIO模式(本地运行)
    特点:本地安装运行MCP Server,但需配置开发环境。
    配置步骤:
    安装依赖:需Python(推荐用uv安装)和NodeJS(推荐bun代替npm),Windows/MacOS/Linux均支持
    添加MCP服务:在Cherry Studio->Settings->MCP Servers中,选择Edit JSON,填写mcpServers配置内容。
2)SSE模式(远程服务)
    特点:无需本地环境,仅需输入服务器URL,适合调用云端API(如天气查询、数据库接口)。
    配置步骤:直接输入SSE服务地址即可,例如集成Google Gemini或腾讯混元的联网搜索功能。

添加MCP json:

json 复制代码
{
  "mcpServers": {
    "mysql": {
      "isActive": true,
      "command": "node",
      "args": [
        "D:\\code\\MCP\\mysql-mcp-server-main\\build\\index.js"
      ],
      "env": {
        "MYSQL_HOST": "localhost",
        "MYSQL_PORT": "3306",
        "MYSQL_USER": "root",
        "MYSQL_PASSWORD": "root",
        "MYSQL_DATABASE": "test"
      },
      "disabled": false,
      "autoApprove": [],
      "name": "mysql"
    }
  }
}

测试

总的来说,text2sql能力还是取决于可用工具的功能,MCP就是让模型根据用户意图去执行接口调度的过程