MCP与数据库查询

该文章已生成可运行项目,

MCP与数据库查询

[原创 快乐王子HP 快乐王子AI说 2025年03月24日 23:32 广东]--同步公众号

1、前置条件

    a. Python 3.11;

    b. mysql , 数据库中要增加数据;

    c. 大模型的基座(最好有代码的);

    d. 了解MCP;

MCP,全称是“模型上下文协议”(Model Context Protocol),是Anthropic开源的一个标准协议。

图片

2、xiyan-mcp-server 

    xiyan-mcp-server是一个模型上下文协议 (MCP) 服务器,支持通过自然语言查询数据库。

图片

参考: https://github.com/XGenerationLab/xiyan_mcp_server

2.1 安装

pip install xiyan-mcp-server

设置配置文件:

model:     name: "qwen2.5_7b"       key: "sk-"   # key可从https://www.modelscope.cn/my/myaccesstoken 页面获取       url: "http://192.168.*.*:*/v1"database:       host: "localhost"       port: 3306       user: "root"       password: "********"       database: "world"

3、MCP inspectory

The MCP inspector is a developer tool for testing and debugging MCP servers.(mcp inspector是测试与调试MCP服务的工具。)

图片

MCP inspector:

https://github.com/modelcontextprotocol/inspector

注: npx可以通过安装node来安装。

npx @modelcontextprotocol/inspector -e YML=C:/Users/liangr/Desktop/H20/mcp/config.yml python -m xiyan_mcp_server

0

打开http://localhost:5173

0

4、附相关的知识

查看大模型输出的日志:

<|im_start|>system你现在是一名mysql数据分析专家,你的任务是根据参考的数据库schema和用户的问题,编写正确的SQL来回答用户的问题,生成的SQL用``sql 和```包围起来。【数据库schema】【DB_ID】 【Schema】# Table: countrylanguage[(IsOfficial:ENUM, Examples: [T, F]),(Percentage:DECIMAL, Examples: [5.3, 9.5, 76.7]),(Language:CHAR, Primary Key, Examples: [Dutch, English, Papiamento]),(CountryCode:CHAR, Primary Key, Examples: [ABW, AFG, AGO])]# Table: country[(HeadOfState:CHAR, Examples: [Beatrix]),(LifeExpectancy:DECIMAL, Examples: [78.4, 45.9, 38.3]),(Continent:ENUM, Examples: [North America, Asia, Africa]),(Name:CHAR, Examples: [Aruba, Afghanistan, Angola]),(GNPOld:DECIMAL, Examples: [793.0, 7984.0, 2500.0]),(Population:INTEGER, Examples: [103000, 22720000, 12878000]),(Region:CHAR, Examples: [Caribbean]),(GNP:DECIMAL, Examples: [828.0, 5976.0, 6648.0]),(SurfaceArea:DECIMAL, Examples: [193.0, 652090.0, 1246700.0]),(GovernmentForm:CHAR, Examples: [Nonmetropolitan Territory of The Netherlands]),(LocalName:CHAR, Examples: [Aruba]),(Code:CHAR, Primary Key, Examples: [ABW, AFG, AGO]),(Capital:INTEGER, Examples: [129, 1, 56]),(IndepYear:SMALLINT, Examples: [1919, 1975, 1912]),(Code2:CHAR, Examples: [AW, AF, AO])]# Table: city[(ID:INTEGER, Primary Key, Examples: [129, 1, 2]),(Population:INTEGER, Examples: [1780000, 237500, 186800]),(District:CHAR, Examples: [Kabol, Qandahar, Herat]),(CountryCode:CHAR, Examples: [ABW, AFG, AGO]),(Name:CHAR, Examples: [Kabul, Qandahar, Herat])]【Foreign keys】city.CountryCode=country.Codecountrylanguage.CountryCode=country.Code【问题】中国在哪个洲?<|im_end|><|im_start|>user用户的问题是: 中国在哪个洲?<|im_end|><|im_start|>assistant

从源码中也可以找到:

0

查看mysql数据库:

0

[更多相关内容关注公从号"快乐王子AI说" ]--同步公众号 

本文章已经生成可运行项目
Cursor 编辑器是一款基于 Visual Studio Code 的 AI 驱动代码编辑器,它支持多种编程语言和插件扩展。虽然 Cursor 本身并不直接提供数据库连接功能,但可以通过安装插件或使用内置终端执行脚本的方式间接实现 MySQL 数据库的交互。 MCP(Message Channel Protocol)协议是一种用于在客户端和服务器之间建立双向通信的协议。尽管 MCP 协议主要用于某些特定场景下的消息传递,但在实际开发中,连接 MySQL 数据库通常采用标准的 JDBC(Java)、ODBC(C/C++)、MySQL Connector(Python、Node.js 等)等方式。 如果目标是通过某种方式在 Cursor 中配置 MCP 协议来连接 MySQL 数据库,则需要确认是否存在支持该协议的插件或自定义脚本。若不存在,建议采用以下替代方案: ### 使用 Python 脚本连接 MySQL 数据库 可以使用 `mysql-connector-python` 库来连接 MySQL 数据库,并在 Cursor 编辑器中运行该脚本[^1]。 ```python import mysql.connector # 建立数据库连接 db = mysql.connector.connect( host="localhost", user="yourusername", password="yourpassword", database="yourdatabase" ) # 创建游标对象 cursor = db.cursor() # 执行 SQL 查询 cursor.execute("SELECT * FROM yourtable") # 获取所有记录 results = cursor.fetchall() # 输出结果 for row in results: print(row) # 关闭连接 db.close() ``` ### 使用 Node.js 连接 MySQL 数据库 如果你更倾向于使用 JavaScript/Node.js,可以使用 `mysql2` 包来连接 MySQL 数据库[^1]。 ```javascript const mysql = require('mysql2'); // 创建连接池 const pool = mysql.createPool({ host: 'localhost', user: 'yourusername', password: 'yourpassword', database: 'yourdatabase', waitForConnections: true, connectionLimit: 10, queueLimit: 0 }); // 执行查询 pool.query("SELECT * FROM yourtable", (error, results, fields) => { if (error) throw error; console.log(results); }); ``` ### 在 Cursor 中运行脚本 1. 安装必要的依赖包(如 `mysql-connector-python` 或 `mysql2`)。 2. 将上述代码粘贴到 `.py` 或 `.js` 文件中,并根据实际情况修改数据库连接参数。 3. 在 Cursor 编辑器中打开终端,导航至脚本所在目录。 4. 运行脚本以测试数据库连接是否正常。 通过这些方法,即使没有直接支持 MCP 协议的插件,也可以在 Cursor 编辑器中实现 MySQL 数据库的有效交互。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值