MCP:基于 Spring AI Mcp 实现 webmvc/webflux sse Mcp Server

# MCP:基于 Spring AI Mcp 实现 Stdio Mcp Server 文章中详细介绍了如何实现一个 Mcp Server,包括如何提供工具、资源以及提示词模板,本文将直入主题,如何webmvc/webflux sse Mcp Server。

与 Stdio 实现不同点

依赖包不一样

对于webmvc,则需要引入;

pom 复制代码
<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-mcp-server-webmvc-spring-boot-starter</artifactId>
</dependency>

其中该包包含主要的依赖:spring-boot-starter-webmcp-spring-webmvc

对于webflux,则需要引入

pom 复制代码
<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-mcp-server-webflux-spring-boot-starter</artifactId>
</dependency>

其中该包包含主要的依赖:spring-boot-starter-webfluxmcp-spring-webflux

配置文件配置项

对于application.properties 配置项也基本上一致,但是稍微有一些不同,对于 webflux 方式建议将

go 复制代码
spring.ai.mcp.server.type=ASYNC // 设置为异步

调用方式不一样

对于 webmvc 调用方式

java 复制代码
package com.ivy.mcp.sse.client;


import io.modelcontextprotocol.client.McpClient;
import io.modelcontextprotocol.client.transport.HttpClientSseClientTransport;
import io.modelcontextprotocol.spec.McpSchema;

import java.util.Map;

public class ClientWebmvc {

    public static void main(String[] args) {

        var transport = new HttpClientSseClientTransport("http://localhost:8080");
        try (var client = McpClient.sync(transport).build()) {

            client.initialize();

            McpSchema.ListToolsResult toolsList = client.listTools();
            System.out.println("Available Tools = " + toolsList);

            McpSchema.CallToolResult sumResult = client.callTool(new McpSchema.CallToolRequest("add",
                    Map.of("a", 1, "b", 2)));
            System.out.println("add a+ b =  " + sumResult.content().get(0));


            McpSchema.CallToolResult currentTimResult = client.callTool(new McpSchema.CallToolRequest("getCurrentTime", Map.of()));
            System.out.println("current time Response = " + currentTimResult);
        }
    }

}

使用 HttpClientSseClientTransport 传输协议,并且直接指定host即可调用。

对于 webflux 调用方式如下;

java 复制代码
package com.ivy.mcp.sse.client;


import io.modelcontextprotocol.client.McpClient;
import io.modelcontextprotocol.client.transport.WebFluxSseClientTransport;
import io.modelcontextprotocol.spec.McpSchema;
import org.springframework.web.reactive.function.client.WebClient;

import java.util.Map;

public class ClientWebflux {

    public static void main(String[] args) {

        var transport = new WebFluxSseClientTransport(WebClient.builder().baseUrl("http://localhost:8080"));
        try (var client = McpClient.sync(transport).build()) {

            client.initialize();

            McpSchema.ListToolsResult toolsList = client.listTools();
            System.out.println("Available Tools = " + toolsList);

            McpSchema.CallToolResult sumResult = client.callTool(new McpSchema.CallToolRequest("add",
                    Map.of("a", 1, "b", 2)));
            System.out.println("add a+ b =  " + sumResult.content().get(0));


            McpSchema.CallToolResult currentTimResult = client.callTool(new McpSchema.CallToolRequest("getCurrentTime", Map.of()));
            System.out.println("current time Response = " + currentTimResult);
        }
    }

}

使用的 WebFluxSseClientTransport 传输协议,并且使用 WebClient作为调用客户端。

webmvc vs webflux

想必大家一定了解 Spring webmvc 和 Spring webflux 的区别以及其存在的优劣势。从性能、吞吐量、资源消耗等上看 webflux 更优,但是其实现略微复杂,并且要求其它配合的中间件也的需要支持 webflux。

大家有兴趣的可以去学习并且使用一下 Spring Webflux。只有亲身使用并且体验才能真正体会到两者的差异性,在面对技术选型上才能更加从容。

回归到如何选择的问题上,则需要根据自身的业务特点和场景、团队成员的技术等等因素出发。

文末总结

文本简单的介绍了如何实现一个 webmvc/webflux sse 的 Mcp Server,由于Spring AI 做了封装,使用起来还是非常简单的。大家可以自行下载源码本地运行看看效果。

webmvc:

webflux:

相关推荐
z26373056112 小时前
springboot继承使用mybatis-plus举例相关配置,包括分页插件以及封装分页类
spring boot·后端·mybatis
追逐时光者5 小时前
分享一个纯净无广、原版操作系统、开发人员工具、服务器等资源免费下载的网站
后端·github
JavaPub-rodert6 小时前
golang 的 goroutine 和 channel
开发语言·后端·golang
GoGeekBaird8 小时前
69天探索操作系统-第54天:嵌入式操作系统内核设计 - 最小内核实现
后端·操作系统
鱼樱前端8 小时前
Java Jdbc相关知识点汇总
java·后端
流烟默9 小时前
编写脚本在Linux下启动、停止SpringBoot工程
linux·spring boot·shell
canonical_entropy9 小时前
NopReport示例-动态Sheet和动态列
java·后端·excel
南山十一少9 小时前
Spring Boot + Spring Integration整合MQTT打造双向通信客户端
spring boot·物联网
kkk哥9 小时前
基于springboot的母婴商城系统(018)
java·spring boot·后端