news 2026/4/16 9:04:07

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/4/14 23:51:16

手把手教你用SiameseUIE实现无冗余实体抽取:从部署到实战

手把手教你用SiameseUIE实现无冗余实体抽取&#xff1a;从部署到实战 1. 为什么你需要一个“无冗余”的实体抽取工具&#xff1f; 你有没有遇到过这样的情况&#xff1a; 用传统NER模型抽人物和地点&#xff0c;结果把“杜甫在成”这种半截词也当成了地点&#xff1f;一段文…

作者头像 李华
网站建设 2026/4/11 15:24:33

MGeo模型复制推理脚本技巧:cp命令迁移至workspace工作区实操

MGeo模型复制推理脚本技巧&#xff1a;cp命令迁移至workspace工作区实操 1. 为什么要把推理脚本复制到workspace&#xff1f; 你刚部署完MGeo模型&#xff0c;打开Jupyter Notebook&#xff0c;准备跑一跑地址相似度匹配的推理脚本——结果发现/root/推理.py这个文件藏在系统…

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

Qwen3-Reranker-8B快速上手:32k长上下文重排序WebUI调用详解

Qwen3-Reranker-8B快速上手&#xff1a;32k长上下文重排序WebUI调用详解 1. 引言 你是否遇到过需要从海量文本中快速找到最相关内容的场景&#xff1f;Qwen3-Reranker-8B就是为解决这类问题而生的强大工具。本文将带你从零开始&#xff0c;快速掌握如何部署和使用这个支持32k…

作者头像 李华
网站建设 2026/4/12 2:17:16

ChatGLM3-6B-128K动态知识问答:Ollama部署后效果惊艳

ChatGLM3-6B-128K动态知识问答&#xff1a;Ollama部署后效果惊艳 1. 长文本处理新标杆&#xff1a;ChatGLM3-6B-128K ChatGLM3-6B-128K作为ChatGLM系列的最新成员&#xff0c;在原有6B版本基础上实现了长文本处理能力的重大突破。这个模型专门针对128K长度的上下文进行了优化…

作者头像 李华
网站建设 2026/3/12 12:20:35

从零开始:用FLUX.1-dev创作你的第一张AI艺术作品

从零开始&#xff1a;用FLUX.1-dev创作你的第一张AI艺术作品 你有没有试过在深夜灵光一闪&#xff0c;脑海里浮现出一幅画面——“雨夜东京街头&#xff0c;穿红裙的女子撑着透明伞&#xff0c;霓虹倒映在积水路面&#xff0c;远处悬浮列车掠过”——却苦于不会画画、找不到设…

作者头像 李华
网站建设 2026/4/13 11:25:15

小白必看!ChatGLM3-6B-128K快速入门指南:3步搭建AI对话系统

小白必看&#xff01;ChatGLM3-6B-128K快速入门指南&#xff1a;3步搭建AI对话系统 你是不是也遇到过这些情况&#xff1a;想试试国产大模型&#xff0c;但看到“环境配置”“CUDA版本”“LoRA微调”就头皮发麻&#xff1f;想部署一个能处理长文档的AI助手&#xff0c;却卡在第…

作者头像 李华