Spring AI(11)——SSE传输的MCP服务端
WebMVC的服务器传输
支持SSE(Server-Sent Events) 基于 Spring MVC 的服务器传输和可选的STDIO运输
导入jar
<dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-starter-mcp-server-webmvc</artifactId>
</dependency>
服务类
该类中定义需要对外提供的调用的函数方法,本例仅为测试使用,没有实际意义。
package com.renr.springainew.mcpserver;import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.stereotype.Service;@Slf4j
@Service
public class NameMcpServer {@Tool(description = "根据孩子的出生日期和性别起名")public String childName(@ToolParam(description = "出生日期") String birth,@ToolParam(description = "性别") String gender) {log.info(birth, gender);return "老任与码";}
}
其中@Tool用与描述方法的作用,@ToolParam用于描述参数的作用。这些内容描述的越详细,大模型选择该工具的可能性越大。
配置类
用于向MCP客户端公开函数工具
package com.renr.springainew.config;import com.renr.springainew.mcpserver.NameMcpServer;
import org.springframework.ai.tool.ToolCallbackProvider;
import org.springframework.ai.tool.method.MethodToolCallbackProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class MyServerConfig {@Beanpublic ToolCallbackProvider nameTool(NameMcpServer nameMcpServer) {return MethodToolCallbackProvider.builder().toolObjects(nameMcpServer).build();}}
MCP 服务器支持四种主要功能类型,可以单独启用或禁用:
- 工具 - 启用/禁用工具功能spring.ai.mcp.server.capabilities.tool=true|false
- 资源 - 启用/禁用资源功能spring.ai.mcp.server.capabilities.resource=true|false
- 提示 - 启用/禁用提示功能spring.ai.mcp.server.capabilities.prompt=true|false
- 完成 - 启用/禁用完成功能spring.ai.mcp.server.capabilities.completion=true|false
默认情况下,所有功能均处于启用状态。禁用功能将阻止服务器注册和向客户端公开相应的功能。
本例仅配置了函数工具
yml配置
spring:ai:mcp:server:name: name-mcp-serverversion: 1.0.0type: SYNCserver:port: 8088
启动服务端
MCP客户端连接服务端
MCP客户端的使用,参考Spring AI(9)——MCP客户端-CSDN博客
导入jar
<dependency>--><groupId>org.springframework.ai</groupId><artifactId>spring-ai-starter-mcp-client</artifactId>
</dependency>
客户端的yml配置
spring:ai:zhipuai:api-key: XXXXXXXXXchat:options:model: glm-4-flashtemperature: 0.7mcp:client:name: my-mcp-clientversion: 1.0.0request-timeout: 10stype: syncsse:connections:server2:url: http://localhost:8088sse-endpoint: /sse
启动客户端并测试
输出
WebFlux的服务器传输
支持SSE(Server-Sent Events) 基于 Spring WebFlux 的服务器传输和可选的STDIO运输
导入jar
<dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-starter-mcp-server-webflux</artifactId>
</dependency>
注意:由于使用WebFlux,springboot项目必须删除spring-boot-starter-web的依赖
yml配置
spring:ai:mcp:server:name: name-mcp-serverversion: 1.0.0type: SYNCserver:port: 8088
启动服务端
切记,pom中一定要删除spring-boot-starter-web的依赖
启动日志中可以看到,webflux使用netty做服务器
MCP客户端连接服务端
导入jar
<dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-starter-mcp-client-webflux</artifactId>
</dependency>
yml配置
spring:ai:zhipuai:api-key: XXXXXXXXXchat:options:model: glm-4-flashtemperature: 0.7mcp:client:name: my-mcp-clientversion: 1.0.0request-timeout: 10stype: syncsse:connections:server2:url: http://localhost:8088sse-endpoint: /sse
启动客户端测试
测试结果与使用WebMvc服务器的一致,不再赘述