news 2026/6/10 11:43:15

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/6/10 10:55:20

小白也能懂的verl教程:快速搭建LLM后训练系统

小白也能懂的verl教程&#xff1a;快速搭建LLM后训练系统 1. 这不是又一个“高不可攀”的强化学习框架 你可能已经看过太多关于LLM强化学习的介绍&#xff1a;PPO、KL散度、奖励建模、Actor-Critic……每个词都像一堵墙&#xff0c;把想动手实践的人挡在外面。更别说还要配环…

作者头像 李华
网站建设 2026/6/10 10:57:29

Local AI MusicGen智能助手:自动化音效生成系统

Local AI MusicGen智能助手&#xff1a;自动化音效生成系统 1. 你的私人AI作曲家&#xff0c;现在就能在本地运行 &#x1f3b5; Local AI MusicGen 这不是云端等待排队的音乐生成服务&#xff0c;而是一个真正属于你自己的、离线可用的AI音乐工作台。它不依赖网络连接&#…

作者头像 李华
网站建设 2026/6/4 1:58:35

鼠标精准控制与游戏体验优化:Raw Accel专业设置指南

鼠标精准控制与游戏体验优化&#xff1a;Raw Accel专业设置指南 【免费下载链接】rawaccel kernel mode mouse accel 项目地址: https://gitcode.com/gh_mirrors/ra/rawaccel 在数字交互中&#xff0c;鼠标作为最直接的输入设备&#xff0c;其响应特性直接影响操作精度与…

作者头像 李华
网站建设 2026/6/10 8:14:46

提升Qwen3-0.6B训练稳定性,这几个参数很关键

提升Qwen3-0.6B训练稳定性&#xff0c;这几个参数很关键 在实际微调Qwen3-0.6B模型的过程中&#xff0c;很多开发者会遇到训练初期loss剧烈震荡、梯度爆炸、显存OOM、收敛缓慢甚至直接崩溃等问题。这些问题往往不是模型能力不足导致的&#xff0c;而是几个关键训练参数设置不当…

作者头像 李华