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:

相关推荐
why1515 小时前
微服务商城-商品微服务
数据库·后端·golang
結城7 小时前
mybatisX的使用,简化springboot的开发,不用再写entity、mapper以及service了!
java·spring boot·后端
Bruk.Liu7 小时前
《Minio 分片上传实现(基于Spring Boot)》
前端·spring boot·minio
星辰离彬7 小时前
Java 与 MySQL 性能优化:MySQL 慢 SQL 诊断与分析方法详解
java·spring boot·后端·sql·mysql·性能优化
q_19132846958 小时前
基于Springboot+Vue的办公管理系统
java·vue.js·spring boot·后端·intellij idea
陪我一起学编程9 小时前
关于nvm与node.js
vue.js·后端·npm·node.js
舒一笑9 小时前
基于KubeSphere平台快速搭建单节点向量数据库Milvus
后端
JavaBuild9 小时前
时隔半年,拾笔分享:来自一个大龄程序员的迷茫自问
后端·程序员·创业
周全全10 小时前
基于 Vue 和 Spring Boot 实现滑块验证码的机器验证
前端·vue.js·spring boot
coder_pig10 小时前
【Trae + 掘金MCP】不写代码,靠嘴遁花0.5h定制公号排版工具
aigc·mcp·trae