1.定义
MySQLMcp的server已被别人创建好,拉下来使用即可。
MySQL MCP ( Model Context Protocol for MySQL ) 是⼀个基于 MCP 协议的服务器组件,它像 ⼀座“桥梁”,连接⼤语⾔模型与 MySQL 数据库。通过它, LLM 可以直接理解⾃然语⾔查询,并⾃动 转换为 SQL 语句执⾏,返回结果。
2.安装服务
⾸先执⾏以下命令安装对应的 MCP Server 到机器上。
局部
npm install mysql-mcp-server全局
npm -g install mysql-mcp-server3.配置文件
server: port: 8013 spring: application: name: ai-siliconflow-glm-mcp-sse-client ai: openai: base-url: https://api.siliconflow.cn api-key: 你的硅基流动key chat: options: model: "zai-org/GLM-4.6" temperature: 0.7 mcp: client: name: ai-siliconflow-glm-mcp-mysql-client stdio: connections: mysql: # Windows操作系统使⽤npx.cmd,Linux和MacOS使⽤npx command: "npx.cmd" args: # mcp server名称 - "mysql-mcp-server" env: # 数据库ip "MYSQL_HOST": "localhost" # 数据库服务端⼝ "MYSQL_PORT": "3306" # 数据库⽤户名 "MYSQL_USER": "root" # 数据库密码 "MYSQL_PASSWORD": "123456" # 数据库名称 "MYSQL_DATABASE": "jiazhong_2025_2"4.config类
package com.jiazhong.mingxing.ai.siliconflow.glm.mcp.mysql.client.config; import jakarta.annotation.Resource; import org.springframework.ai.chat.client.ChatClient; import org.springframework.ai.mcp.SyncMcpToolCallbackProvider; import org.springframework.ai.openai.OpenAiChatModel; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class ChatClientConfig { @Resource private OpenAiChatModel openAiChatModel; @Resource private SyncMcpToolCallbackProvider syncMcpToolCallbackProvider; @Bean("openAiChatClient") public ChatClient openAiChatClient(){ return ChatClient.builder(openAiChatModel) .defaultToolCallbacks(syncMcpToolCallbackProvider) .build(); } }5.controller类
package com.jiazhong.mingxing.ai.siliconflow.glm.mcp.mysql.client.controller; import jakarta.annotation.Resource; import org.springframework.ai.chat.client.ChatClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import reactor.core.publisher.Flux; @RestController @RequestMapping("/mysql") public class MySQLController { @Resource private ChatClient openAiChatClient; @GetMapping(value = "/a",produces = "text/html;charset=utf-8") public Flux<String> a(@RequestParam("question") String questiono){ return openAiChatClient.prompt( """ 1.查询jiazhong_2025_2数据库 """ ) .user(questiono) .stream().content(); } }6.启动类
package com.jiazhong.mingxing.ai.siliconflow.glm.mcp.mysql.client; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class AiSiliconflowGlmMcpMySQLClientApplicatiion { public static void main(String[] args) { SpringApplication.run(AiSiliconflowGlmMcpMySQLClientApplicatiion.class,args); } }