写点什么

SpringBoot3 使用 SolonMCP 开发 MCP

  • 2025-05-19
    福建
  • 本文字数:3029 字

    阅读完需:约 10 分钟

之前发了个 “《SpringBoot2 可以使用 SolonMCP 开发 MCP(江湖救急)》”。然后,有人问:SpringBoot3 能不能用 SolonMCP?


其实 SpringBoot3 可以使用 Spring AI 或者 Spring AI Alibaba(都有 MCP 功能)。

既然问了,就再发一个文。另外 SpringBoot3 使用 SolonMPC 和 SpringBoot2 的情况,差不多。只一个依赖包有不同。


1、SolonMCP 简介


SolonMCP(全称:solon-ai-mcp)是 solon ai 的一个扩展。支持内嵌到 jfinal,vert.x,springboot2,springboot3 等框架使用。


Maven 主要依赖包:


<dependency>    <groupId>org.noear</groupId>    <artifactId>solon-ai-mcp</artifactId></dependency>
复制代码


2、MCP 服务端开发


2.1、添加入口类 webapp.HelloApp


@SpringBootApplicationpublic class HelloApp {    public static void main(String[] args) {        SpringApplication.run(HelloApp.class, args);    }
复制代码


2.2、添加个空接口 webapp.mcpserver.IMcpServerEndpoint


用于识别端点组件类


public interface IMcpServerEndpoint { }
复制代码


2.3、添加 webapp.mcpserver.McpServerConfig


拖管 solon 的生命周期。收集 IMcpServerEndpoint 组件,并转为 McpServerEndpointProvider


@Configurationpublic class McpServerConfig {    @PostConstruct    public void start() {        Solon.start(McpServerConfig.class, new String[]{"--cfg=mcpserver.yml"});    }
@PreDestroy public void stop() { if (Solon.app() != null) { Solon.stopBlock(false, Solon.cfg().stopDelay()); } }
@Bean public McpServerConfig init(List<IMcpServerEndpoint> serverEndpoints) { for (IMcpServerEndpoint serverEndpoint : serverEndpoints) { //这里注意一下,如果有代理的话需要用 AnnotationUtils 获取注解 McpServerEndpoint anno = AnnotationUtils.findAnnotation(serverEndpoint.getClass(), McpServerEndpoint.class);
if (anno == null) { continue; }
McpServerEndpointProvider serverEndpointProvider = McpServerEndpointProvider.builder() .from(serverEndpoint.getClass(), anno) .build();
serverEndpointProvider.addTool(new MethodToolProvider(serverEndpoint)); serverEndpointProvider.addResource(new MethodResourceProvider(serverEndpoint)); serverEndpointProvider.addPrompt(new MethodPromptProvider(serverEndpoint));
serverEndpointProvider.postStart();
//可以再把 serverEndpointProvider 手动转入 SpringBoot 容器 }
//为了能让这个 init 能正常运行 return this; }
@Bean public FilterRegistrationBean mcpServerFilter() { FilterRegistrationBean<SolonServletFilter> filter = new FilterRegistrationBean<>(); filter.setName("SolonFilter"); filter.addUrlPatterns("/mcp/*"); filter.setFilter(new SolonServletFilter()); return filter; }}
复制代码


2.4、添加 webapp.mcpserver.tool.McpServer(实现 Handler、IPlugin 接口)


这里是重点了,添加 mcp server 端点(支持多个端点)。这里是正常的 SpringBoot 组件开发了。


@Component //注意这个注解别用错了(solon 里也有同名的)@McpServerEndpoint(name="demo1", sseEndpoint = "/mcp/demo1/sse")public class McpServerTool implements IMcpServerEndpoint {    //    // 建议开启编译参数:-parameters (否则,最好再配置参数的 name)    //    @ToolMapping(description = "查询天气预报")    public String getWeather(@Param(description = "城市位置") String location) {        return "晴,14度";    }
@ResourceMapping(uri = "config://app-version", description = "获取应用版本号") public String getAppVersion() { return "v3.2.0"; }
@ResourceMapping(uri = "db://users/{user_id}/email", description = "根据用户ID查询邮箱") public String getEmail(@Param(description = "用户Id") String user_id) { return user_id + "@example.com"; }
@PromptMapping(description = "生成关于某个主题的提问") public Collection<ChatMessage> askQuestion(@Param(description = "主题") String topic) { return Arrays.asList( ChatMessage.ofUser("请解释一下'" + topic + "'的概念?") ); }}
复制代码


2.5、编译后运行


或者开发时,直接运行 HelloApp:main 方法


3、MCP 客户端开发


客户端简单些


public class McpClientTest {    public static void main(String[] args) throws Exception {        McpClientProvider toolProvider = McpClientProvider.builder()                .apiUrl("http://localhost:8080/mcp/sse")                .build();
//工具调用 Map<String, Object> map = Collections.singletonMap("location", "杭州"); String rst = toolProvider.callToolAsText("getWeather", map).getContent(); System.out.println(rst); assert "晴,14度".equals(rst); //资源读取 resourceContent = toolProvider.readResourceAsText("config://app-version").getContent(); System.out.println(resourceContent); }}
复制代码


4、MCP 客户端作为 LLM(ChatModel) 的工具集使用


也比较简单。使用 ollama 做为 llm 提供者,方便本地测试。


public class McpClientTest {    private static final String apiUrl = "http://127.0.0.1:11434/api/chat";    private static final String provider = "ollama";    private static final String model = "qwen2.5:1.5b"; //"llama3.2";//deepseek-r1:1.5b;        public static void main(String[] args) throws Exception {        //构建 mcp client        McpClientProvider toolProvider = McpClientProvider.builder()                .apiUrl("http://localhost:8080/mcp/sse")                .build();
//构建 llm 接口 ChatModel chatModel = ChatModel.of(apiUrl) .provider(provider) .model(model) .defaultToolsAdd(toolProvider) //添加默认工具(这是 mcp client) .build(); //请求 ChatResponse resp = chatModel.prompt("杭州今天的天气怎么样?") .call();
System.out.println(resp.getMessage()); }}
复制代码


5、SpringBoot3 和 SpringBoot2 使用 SolonMCP 有什么区别?


仅一个 servlet 的依赖包不同(由 java-ee 改名引起的)。SpringBoot3 使用 solon-web-servlet-jakarta 依赖包;SpringBoot2 则使用 solon-web-servlet-jakarta 依赖包。


文章转载自:带刺的坐椅

原文链接:https://www.cnblogs.com/noear/p/18879996

体验地址:http://www.jnpfsoft.com/?from=001YH

用户头像

还未添加个人签名 2023-06-19 加入

还未添加个人简介

评论

发布
暂无评论
SpringBoot3 使用 SolonMCP 开发 MCP_Spring Boot_不在线第一只蜗牛_InfoQ写作社区