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:

相关推荐
苏打水com8 小时前
数据库进阶实战:从性能优化到分布式架构的核心突破
数据库·后端
西瓜er9 小时前
JAVA:Spring Boot 集成 FFmpeg 实现多媒体处理
java·spring boot·ffmpeg
间彧9 小时前
Spring Cloud Gateway与Kong或Nginx等API网关相比有哪些优劣势?
后端
间彧9 小时前
如何基于Spring Cloud Gateway实现灰度发布的具体配置示例?
后端
间彧9 小时前
在实际项目中如何设计一个高可用的Spring Cloud Gateway集群?
后端
间彧9 小时前
如何为Spring Cloud Gateway配置具体的负载均衡策略?
后端
间彧9 小时前
Spring Cloud Gateway详解与应用实战
后端
EnCi Zheng10 小时前
SpringBoot 配置文件完全指南-从入门到精通
java·spring boot·后端
烙印60110 小时前
Spring容器的心脏:深度解析refresh()方法(上)
java·后端·spring
Lisonseekpan11 小时前
Guava Cache 高性能本地缓存库详解与使用案例
java·spring boot·后端·缓存·guava