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:

相关推荐
老友@25 分钟前
分布式事务完全演进链:从单体事务到 TCC 、Saga 与最终一致性
分布式·后端·系统架构·事务·数据一致性
Coder_Boy_1 小时前
基于SpringAI的在线考试系统-数据库设计核心业务方案
java·数据库·spring boot·ddd·tdd
java1234_小锋1 小时前
Spring里AutoWired与Resource区别?
java·后端·spring
风象南1 小时前
Spring Boot 定时任务多实例互斥执行
java·spring boot·后端
崎岖Qiu1 小时前
【深度剖析】:结合 Spring Bean 的生命周期理解 @PostConstruct 的原理
java·笔记·后端·spring·javaee
毕设源码-郭学长2 小时前
【开题答辩全过程】以 基于Springboot旅游景点管理系统的设计与实现为例,包含答辩的问题和答案
java·spring boot·后端
猿小羽2 小时前
深度实战:Spring AI 与 MCP(Model Context Protocol)构建下一代 AI Agent
java·大模型·llm·ai agent·spring ai·开发者工具·mcp
Y_033 小时前
SpringBoot+VUE3的图书管理系统
vue.js·spring boot·毕业设计·数据可视化
像少年啦飞驰点、3 小时前
零基础入门 Spring Boot:从‘Hello World’到可部署微服务的完整学习路径
java·spring boot·web开发·编程入门·后端教程
方安乐3 小时前
杂记:Quart和Flask比较
后端·python·flask