news 2026/2/1 11:44:48

SpringAI-mcp-入门案例

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
SpringAI-mcp-入门案例

1.搭建服务端

1.1导依赖

<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.ai</groupId> <artifactId>spring-ai-starter-mcp-server</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>com.jiazhong.mingxing.ai.server.AiSiliconflowGlmMcpStdioServer</mainClass> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build>

1.2配置yml文件

server: port: 8009 spring: ai: mcp: server: stdio: true # 开启stdio name: ai-mcp-stdio-server # 服务器名称 version: 1.0.0 # 服务器版本 type: sync # 同步模式 main: banner-mode: off web-application-type: none application: name: ai-mcp-stdio-server version: 1.0.0 #高德的key AMAP-KEY:#自己的高德key

1.3配置工具

package com.jiazhong.mingxing.ai.server.service; import org.springframework.stereotype.Service; @Service public interface WeatherService { String weather(String city); }
package com.jiazhong.mingxing.ai.server.service.impl; import com.jiazhong.mingxing.ai.server.service.WeatherService; import jakarta.annotation.Resource; import org.springframework.ai.tool.annotation.Tool; import org.springframework.ai.tool.annotation.ToolParam; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; @Service public class WeatherServiceImpl implements WeatherService { @Resource private RestTemplate restTemplate; @Value("${AMAP-KEY}") private String key; @Tool(name = "weatherService", description = "获取某个城市的气温") public String weather(@ToolParam(description = "城市名称") String city) { String url="https://restapi.amap.com/v3/weather/weatherInfo?key="+ key + "&city=" + city + "&extensions=all"; return restTemplate.getForObject(url,String.class); } }

1.4启动类

package com.jiazhong.mingxing.ai.server; import com.jiazhong.mingxing.ai.server.service.WeatherService; 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.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @SpringBootApplication public class AiSiliconflowGlmMcpStdioServer { @Bean("restTemplate") public RestTemplate restTemplate(){ return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(AiSiliconflowGlmMcpStdioServer.class,args); } @Bean("reg") public ToolCallbackProvider reg(WeatherService weatherService){ return MethodToolCallbackProvider.builder() .toolObjects(weatherService) .build(); } }

1.5打包为jar

2.配置服务端

2.1导包

<dependencies> <dependency> <groupId>org.springframework.ai</groupId> <artifactId>spring-ai-starter-model-openai</artifactId> </dependency> <dependency> <groupId>org.springframework.ai</groupId> <artifactId>spring-ai-starter-mcp-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>

2.2配置 yml文件

server: port: 8010 spring: application: name: ai-siliconflow-advisor-glm ai: openai: base-url: https://api.siliconflow.cn api-key: sk-rlpneielwjrtbzwghvmtnkrfzsqoorkclubnimumojlptvqz chat: options: model: "zai-org/GLM-4.6" temperature: 0.7 mcp: client: enabled: true # 启⽤MCP客户端 name: ai-mcp-stdio-client # MCP客户端名称 version: 1.0.0 # 客户端版本 initialized: true # ⾃动初始化客户端 request-timeout: 20s # 请求超时时间 type: sync # 客户端类型为同步模式 root-change-notification: true # 启⽤客户端变更通知 toolcallback: enabled: true # 启⽤⼯具回调 与Spring AI⼯具执⾏框架集成 stdio: servers-configuration: classpath:/stdio-server-config.json

2.3写配置类

package com.jiazhong.mingxing.ai.client.config; import jakarta.annotation.Resource; import org.springframework.ai.chat.client.ChatClient; import org.springframework.ai.openai.OpenAiChatModel; import org.springframework.ai.tool.ToolCallbackProvider; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class ChatClientConfig { @Resource private OpenAiChatModel openAiChatModel; @Resource private ToolCallbackProvider reg; @Bean("openAiChatClient") public ChatClient openAiChatClient(){ return ChatClient.builder(openAiChatModel) .defaultToolCallbacks(reg) .build(); } }

2.4写controller类

package com.jiazhong.mingxing.ai.client.controller; import io.modelcontextprotocol.client.McpAsyncClient; import io.modelcontextprotocol.client.McpSyncClient; import io.modelcontextprotocol.spec.McpSchema; import jakarta.annotation.Resource; import org.springframework.ai.chat.client.ChatClient; import org.springframework.beans.factory.annotation.Autowired; 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; import java.util.List; @RestController @RequestMapping("/stdio_client") public class StdioClientController { @Resource private ChatClient openAiChatClient; // 同步客户端 @Autowired private List<McpSyncClient> mcpSyncClients; @GetMapping(value = "/a", produces = "text/html;charset=utf-8") public Flux<String> a(@RequestParam("question") String question) { System.out.println("==================================================="); System.out.println("length:" + mcpSyncClients.size()); for (McpSyncClient client : mcpSyncClients) { McpSchema.ListToolsResult listToolsResult = client.listTools(); System.out.println("这个是我的工具"); List<McpSchema.Tool> tools = listToolsResult.tools(); for (McpSchema.Tool tool : tools) { System.out.println(tool.name()); } } System.out.println("==================================================="); return openAiChatClient.prompt() .user(question) .stream().content(); } }

2.5写Json文件

{ "mcpServers": { "mcp-server": { "command": "java", "args": [ "-Dspring.ai.mcp.server.stdio=true", "-Dspring.main.web-application-type=none", "-Dlogging.pattern.console=", "-Dfile.encoding=UTF-8", "-jar", "D:\\code\\jiazhong-mingxing-01\\jiazhong-ai\\ai-siliconflow-glm-mcp-stdio-client\\src\\main\\resources\\ai-siliconflow-glm-mcp-stdio-server-3.5.3.jar" //服务端打包的jar包的绝对路径 ], "env": {} } } }

2.6启动类

package com.jiazhong.mingxing.ai.client; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class AiSiliconflowGlmMcpStdioClient { public static void main(String[] args) { SpringApplication.run(AiSiliconflowGlmMcpStdioClient.class,args); } }
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/1/30 0:19:06

SiameseUIE效果对比:自定义模式vs通用规则抽取精度分析

SiameseUIE效果对比&#xff1a;自定义模式vs通用规则抽取精度分析 1. 模型概述与测试背景 SiameseUIE是一种基于孪生网络结构的信息抽取模型&#xff0c;专门用于从非结构化文本中提取特定类型的实体信息。本测试将重点对比该模型在两种不同抽取模式下的表现&#xff1a; 自…

作者头像 李华
网站建设 2026/1/29 17:01:34

大气层系统技术探索指南:从核心原理到深度应用

大气层系统技术探索指南&#xff1a;从核心原理到深度应用 【免费下载链接】Atmosphere-stable 大气层整合包系统稳定版 项目地址: https://gitcode.com/gh_mirrors/at/Atmosphere-stable 核心价值&#xff1a;为什么选择大气层系统 大气层&#xff08;Atmosphere&…

作者头像 李华
网站建设 2026/1/30 0:17:25

ChatTTS主观听感测试:百人盲测结果公布

ChatTTS主观听感测试&#xff1a;百人盲测结果公布 1. 测试背景与方法 ChatTTS作为当前开源领域最先进的语音合成模型之一&#xff0c;其独特的拟真效果在开发者社区引发了广泛讨论。为了客观评估其真实表现&#xff0c;我们组织了这次百人规模的盲测实验。 测试采用双盲设计…

作者头像 李华
网站建设 2026/1/29 21:41:21

Atmosphere系统完全掌握:从入门到精通的实用指南

Atmosphere系统完全掌握&#xff1a;从入门到精通的实用指南 【免费下载链接】Atmosphere-stable 大气层整合包系统稳定版 项目地址: https://gitcode.com/gh_mirrors/at/Atmosphere-stable 副标题&#xff1a;让Switch玩家轻松实现系统破解与优化 是否遇到过Switch主机…

作者头像 李华
网站建设 2026/1/29 21:15:48

Linux系统维护利器:自定义开机启动脚本

Linux系统维护利器&#xff1a;自定义开机启动脚本 在日常运维和开发环境中&#xff0c;我们常常需要让某些服务、监控程序或初始化任务在系统启动时自动运行。比如部署一个本地调试环境、启动数据采集进程、挂载特定目录&#xff0c;或者执行一些配置检查——这些操作如果每次…

作者头像 李华
网站建设 2026/1/29 10:55:49

opencode技能管理插件:个性化AI助手搭建指南

opencode技能管理插件&#xff1a;个性化AI助手搭建指南 1. 为什么你需要一个“会成长”的AI编程助手&#xff1f; 你有没有过这样的体验&#xff1a; 写代码时反复问同一个问题&#xff0c;比如“怎么用Python读取Excel并跳过空行&#xff1f;”每次都要重新描述项目结构、…

作者头像 李华