用 Spring AI Alibaba 开发了一款能连接 MySQL 的 MCP 服务,手把手教你
nanshan 2025-07-10 19:28 14 浏览 0 评论
免责声明
本文档(含所有案例、代码及参考资料)仅供学习交流与参考用途。未经严格测试及专业评估前,禁止直接复制、引用或用于任何实际场景。使用者应自行承担因不当使用所产生的全部风险及法律责任,作者及文档提供方概不负责。
直接看成品
数据库表
还能统计数量
数量对应上表中的行数,准确无误。
详细实现步骤
使用 IDEA 创建新的项目
注意:
- Spring Boot 版本需要 3.2.x
- JDK 版本 17
- Maven 版本:3.9.9(这里我直接用的最新的,我用 3.6.1 打包项目成 jar 包是不行的,各位自行测试吧。)
Maven 依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.4.4</version>
<relativePath/>
</parent>
<properties>
<java.version>17</java.version>
<!-- Spring AI -->
<spring-ai.version>1.0.0-M6</spring-ai.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-mcp-server-spring-boot-starter</artifactId>
<version>${spring-ai.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- Connection Pool -->
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>5.1.0</version>
</dependency>
</dependencies>
系统配置
spring:
main:
web-application-type: none # Disable web for stdio mode
banner-mode: off
ai:
mcp:
server:
stdio: true
name: mysql-server
version: 1.0.0
相关代码
启动类
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.ai.tool.ToolCallbackProvider;
import org.springframework.ai.tool.method.MethodToolCallbackProvider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.core.JdbcTemplate;
import top.trysmile.javamcpmysql.service.MySqlService;
import javax.sql.DataSource;
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class JavaMcpMysqlApplication {
public static void main(String[] args) {
SpringApplication.run(JavaMcpMysqlApplication.class, args);
}
@Bean
public DataSource dataSource() {
HikariDataSource dataSource = new HikariDataSource();
dataSource.setJdbcUrl("jdbc:mysql://IP:PORT/database"); // Added common parameters
dataSource.setUsername("username");
dataSource.setPassword("password");
return dataSource;
}
@Bean
public JdbcTemplate jdbcTemplate(DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
@Bean
public MySqlService mySqlService(JdbcTemplate jdbcTemplate) {
return new MySqlService(jdbcTemplate);
}
@Bean
public ToolCallbackProvider toolCallbackProvider(MySqlService mySqlService) {
return MethodToolCallbackProvider.builder()
.toolObjects(mySqlService)
.build();
}
}
执行 SQL 语句
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
@Service
public class MySqlService {
private final JdbcTemplate jdbcTemplate;
public MySqlService(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
@Tool(description = "Execute a SQL query and return results")
public List<Map<String, Object>> executeQuery(
@ToolParam(description = "SQL query to execute") String query) {
try {
return jdbcTemplate.queryForList(query);
} catch (Exception e) {
throw new RuntimeException("Error executing query: " + e.getMessage(), e);
}
}
@Tool(description = "Execute an update SQL statement (INSERT, UPDATE, DELETE)")
public int executeUpdate(
@ToolParam(description = "SQL statement to execute") String sql) {
try {
return jdbcTemplate.update(sql);
} catch (Exception e) {
throw new RuntimeException("Error executing update: " + e.getMessage(), e);
}
}
@Tool(description = "Get table schema information")
public List<Map<String, Object>> getTableSchema(
@ToolParam(description = "Table name") String tableName) {
if (!tableName.matches("^[a-zA-Z0-9_]+#34;)) {
throw new IllegalArgumentException("Invalid table name format.");
}
try {
return jdbcTemplate.queryForList(
"SELECT column_name, data_type, is_nullable " +
"FROM information_schema.columns " +
"WHERE table_schema = database() AND table_name = ?", tableName);
} catch (Exception e) {
throw new RuntimeException("Error getting table schema: " + e.getMessage(), e);
}
}
@Tool(description = "Count records in a table")
public int countRecords(
@ToolParam(description = "Table name") String tableName) {
if (!tableName.matches("^[a-zA-Z0-9_]+#34;)) {
throw new IllegalArgumentException("Invalid table name format.");
}
try {
String sql = String.format("SELECT COUNT(*) FROM `%s`", tableName.replace("`", "``"));
return jdbcTemplate.queryForObject(sql, Integer.class);
} catch (Exception e) {
throw new RuntimeException("Error counting records: " + e.getMessage(), e);
}
}
}
OK,系统服务代码、配置到这里就结束啦。
现在你可以尝试一下运行服务是否能启动。
注意
数据库相关连接信息记得更新。
开始使用 AI 客户端接入 MCP 工具服务
客户端使用:ChatWise
开始配置 MCP 服务
配置如下图:
-jar 这个命令参数后的一串,就是你 Java 服务 jar 包所处的位置。
验证是否集成成功
点击 “查看工具”,当加载成功以后,就能看见能使用的工具啦。
文章到这里就结束啦,如果你有任何疑问,欢迎评论私信我哦~
更多文章一键直达
相关推荐
- 三种自建KMS激活系统自动激活windows方法
-
第一种:在windows服务器上搭建主要针对vol版本(win7、win10、win20xx、win2012等等)平台:我自己搭建的windows虚拟机,windows2016的操作系统软件:...
- 重装系统被收98元?避开Windows付费陷阱的实用指南
-
重装系统被收98元?避开Windows付费陷阱的实用指南有网友反映,在重装Windows系统后,屏幕突然弹出“激活系统需支付98元服务费”的提示,疑惑自己是不是遭遇了付费陷阱。事实上,微软官方的Wi...
- Windows Server2012远程桌面服务配置和授权激活
-
安装:注意:安装完毕之后需手动重启一下计算机配置终端服务管理工具---远程桌面服务---RD授权诊断程序,查看当前服务器有没有授权授权:运行—>gpedit.msc->计算机配置---管理...
- 新书速览|Windows Server 2022 系统与网站配置实战
-
讲述桌面体验、ServerCore/NanoServer,容器与云系统的配置1本书内容《WindowsServer2022系统与网站配置实战》秉持作者一贯理论兼具实践的写作风格,以新版的Wi...
- Windows激活全攻略:KMS神钥与专业工具的完美结合!
-
对于许多Windows用户来说,系统的激活是一个必经的过程。虽然Windows操作系统在未经激活的状态下也可以使用一段时间,但长期来看,未激活的系统会限制某些功能并频繁提示用户激活。以下是两种流行的激...
- 微软Win9全新激活技术曝光(微软系统激活有什么用)
-
2014-07-0905:46:00作者:徐日俄罗斯Wzor日前披露了更多关于Windows9的最新消息,据悉,Windows9将会在今年秋季亮相,其宣传口号是“想要开始按钮和开始菜单?如你所...
- 快速激活Windows 10/11:CMD命令详细教程
-
#记录我的2024#激活Windows操作系统是确保系统功能和安全更新正常运行的重要步骤。本文将为您分享如何使用命令提示符(CMD)在Windows10和Windows11上进行激活的详细步骤。...
- Wndows 2019 RDS应用发布部署(rds的安装和应用程序的发布)
-
安装前的准备1、需要提供服务器作为应用中心,应用中心的推荐配置如下表所示。规格建议1-10人11-20人21-50人51-100人100+人CPU4核8核16核内存8GB16GB32GB64GB系统盘...
- 解决 Windows 系统激活难题(如何解决windows激活问题)
-
今天,一位朋友给我说,他手头有三台电脑,均同时弹出系统未激活的提示。他对此毫无头绪,便急忙将电脑上出现的激活提示信息一股脑发给了我。我看到其中一台显示的是“Windows10企业版LTSC尚...
- 自建KMS激活服务器(自建kms激活服务器的风险)
-
自建KMS激活服务器Win10和office安装后,都需要激活才可以使用,一般可以输入购买的MAK激活码进行在线激活,也可以通过KMS激活,网上也有很多激活工具,但这些工具一般都含有病毒或木马程序,容...
- 30秒免费激活windows和office亲测有效!
-
“第三方工具有病毒?”“KMS服务器激活总失效?”今天给大家分享一个开源激活工具——MicrosoftActivationScripts(MAS),无需密钥、不装软件,30秒永久激活Window...
- 「操作系统」Windows 10 LTSC 2019 企业版C大集成更新版
-
Windows10LTSC企业版CHIANNET集成更新优化整合多镜像版,CHIANNET,是USBOS超级PE维护盘工具箱作者,长久以来一直默默的更新着,USBOSPE软件,电脑城装机及...
- 一文看懂Windows激活:自查方法+授权类型科普(Win7/Win10通用)
-
一、如何判断Windows是否永久激活?无论是Win7还是Win10,均可通过以下方法快速验证:命令提示符法(通用):按下Win+R,输入slmgr.vbs/xpr并按回车键运行即可查看是否...
- 部分Windows Server 2019/2022用户反馈无法运行微软Teams应用
-
IT之家7月2日消息,科技媒体borncity今天(7月2日)发布博文,报道称在多个WindowsServer版本上,MicrosoftTeams应用近期出现了运行故障。用...
- 这种Windows激活方式已有20年...(windows现在激活)
-
2006年微软正式发布WindowsVista,随之而来引入了一项新的激活机制「OEM激活」,这项机制在Vista和Win7上最为流行。其实WindowsServer自2008开始至2025版本一...
你 发表评论:
欢迎- 一周热门
-
-
UOS服务器操作系统防火墙设置(uos20关闭防火墙)
-
极空间如何无损移机,新Z4 Pro又有哪些升级?极空间Z4 Pro深度体验
-
NAS:DS video/DS file/DS photo等群晖移动端APP远程访问的教程
-
手机如何设置与显示准确时间的详细指南
-
如何在安装前及安装后修改黑群晖的Mac地址和Sn系列号
-
如何修复用户配置文件服务在 WINDOWS 上登录失败的问题
-
一加手机与电脑互传文件的便捷方法FileDash
-
日本海上自卫队的军衔制度(日本海上自卫队的军衔制度是什么)
-
10个免费文件中转服务站,分享文件简单方便,你知道几个?
-
爱折腾的特斯拉车主必看!手把手教你TESLAMATE的备份和恢复
-
- 最近发表
-
- 三种自建KMS激活系统自动激活windows方法
- 重装系统被收98元?避开Windows付费陷阱的实用指南
- Windows Server2012远程桌面服务配置和授权激活
- 新书速览|Windows Server 2022 系统与网站配置实战
- Windows激活全攻略:KMS神钥与专业工具的完美结合!
- 微软Win9全新激活技术曝光(微软系统激活有什么用)
- 快速激活Windows 10/11:CMD命令详细教程
- Wndows 2019 RDS应用发布部署(rds的安装和应用程序的发布)
- 解决 Windows 系统激活难题(如何解决windows激活问题)
- 自建KMS激活服务器(自建kms激活服务器的风险)
- 标签列表
-
- linux 查询端口号 (58)
- docker映射容器目录到宿主机 (66)
- 杀端口 (60)
- yum更换阿里源 (62)
- internet explorer 增强的安全配置已启用 (65)
- linux自动挂载 (56)
- 禁用selinux (55)
- sysv-rc-conf (69)
- ubuntu防火墙状态查看 (64)
- windows server 2022激活密钥 (56)
- 无法与服务器建立安全连接是什么意思 (74)
- 443/80端口被占用怎么解决 (56)
- ping无法访问目标主机怎么解决 (58)
- fdatasync (59)
- 405 not allowed (56)
- 免备案虚拟主机zxhost (55)
- linux根据pid查看进程 (60)
- dhcp工具 (62)
- mysql 1045 (57)
- 宝塔远程工具 (56)
- ssh服务器拒绝了密码 请再试一次 (56)
- ubuntu卸载docker (56)
- linux查看nginx状态 (63)
- tomcat 乱码 (76)
- 2008r2激活序列号 (65)