news 2026/5/9 12:03:37

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/5/9 10:29:36

短视频配音新选择:GLM-TTS打造个性化旁白

短视频配音新选择&#xff1a;GLM-TTS打造个性化旁白 在短视频日均产出超千万条的今天&#xff0c;一条优质内容的成败&#xff0c;往往只差3秒——不是画面不够炫&#xff0c;而是旁白不够“对味”。你是否也经历过&#xff1a;找配音员反复修改语气、预算有限只能用机械音、…

作者头像 李华
网站建设 2026/4/30 2:58:47

革新图像创作流程:SD-PPP实现ComfyUI与Photoshop的无缝协作

革新图像创作流程&#xff1a;SD-PPP实现ComfyUI与Photoshop的无缝协作 【免费下载链接】sd-ppp Getting/sending picture from/to Photoshop in ComfyUI or SD 项目地址: https://gitcode.com/gh_mirrors/sd/sd-ppp 在数字创作领域&#xff0c;AI图像协作工具正成为连接…

作者头像 李华
网站建设 2026/5/2 7:40:10

实时语音生成:GLM-TTS流式推理体验

实时语音生成&#xff1a;GLM-TTS流式推理体验 你有没有试过——只用手机录下10秒自己的声音&#xff0c;5秒后就听见AI用完全一样的语气、节奏、甚至微微的鼻音&#xff0c;念出一段从未听过的文案&#xff1f;不是机械朗读&#xff0c;不是千篇一律的播音腔&#xff0c;而是…

作者头像 李华
网站建设 2026/5/9 7:22:07

VibeVoice实时语音合成教程:服务健康检查与自动重启配置

VibeVoice实时语音合成教程&#xff1a;服务健康检查与自动重启配置 1. 项目概述 VibeVoice是一款基于微软开源VibeVoice-Realtime-0.5B模型构建的实时文本转语音(TTS)Web应用。这个轻量级模型专为实时语音合成优化&#xff0c;具有低延迟、流式处理和多种音色选择等特点&…

作者头像 李华
网站建设 2026/4/19 10:55:27

无需技术背景!InstructPix2Pix镜像让修图像聊天一样简单

无需技术背景&#xff01;InstructPix2Pix镜像让修图像聊天一样简单 你有没有过这样的时刻&#xff1a; 想把朋友圈那张阳光灿烂的旅行照&#xff0c;改成雨中漫步的文艺感&#xff1b; 想给客户提案里的产品图加个“悬浮在星空背景中”的酷炫效果&#xff1b; 又或者&#xf…

作者头像 李华
网站建设 2026/4/29 10:38:47

YOLO11低光照优化:暗光环境检测性能提升方案

YOLO11低光照优化&#xff1a;暗光环境检测性能提升方案 在安防监控、夜间自动驾驶、工业巡检等实际场景中&#xff0c;图像质量常因光照不足而严重退化——细节模糊、噪声显著、对比度低下&#xff0c;导致传统目标检测模型性能断崖式下降。YOLO系列作为实时检测的标杆&#…

作者头像 李华