STDIO 和 SSE MCP 服务器
Streamable-HTTP MCP 服务器
Streamable HTTP 传输 允许 MCP 服务器作为独立进程运行,通过 HTTP POST 和 GET 请求处理多个客户端连接,并可选择使用 Server-Sent Events (SSE) 流式传输多条服务器消息。它取代了 SSE 传输。
这些服务器在 2025-03-26 版本规范中引入,非常适合需要向客户端通知工具、资源或提示模板动态变化的应用。
spring.ai.mcp.server.protocol=STREAMABLEStreamable-HTTP WebMVC 服务器
使用 spring-ai-starter-mcp-server-webmvc 依赖:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-mcp-server-webmvc</artifactId>
</dependency>
并将属性 spring.ai.mcp.server.protocol 设置为 STREAMABLE。
- 基于 Spring MVC Streamable 传输的完整 MCP 服务器功能
- 支持工具(Tools)、资源(Resources)、提示模板(Prompts)、补全(Completion)、日志(Logging)、进度(Progress)、Ping 以及根目录变更(Root Changes)
- 持久连接管理
Streamable-HTTP WebFlux 服务器
使用 spring-ai-starter-mcp-server-webflux 依赖:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-mcp-server-webflux</artifactId>
</dependency>
并将 spring.ai.mcp.server.protocol 属性设置为 STREAMABLE。
- 基于 WebFlux Streamable 传输的响应式 MCP 服务器
- 支持工具(Tools)、资源(Resources)、提示模板(Prompts)、补全(Completion)、日志(Logging)、进度(Progress)、Ping 以及根目录变更(Root Changes)
- 非阻塞的持久连接管理
配置属性
通用属性
所有通用属性均以 spring.ai.mcp.server 为前缀:
| 属性 | 描述 | 默认值 |
|---|---|---|
enabled | 启用/禁用 Streamable MCP 服务器 | true |
protocol | MCP 服务器协议 | 必须设置为 STREAMABLE 以启用 Streamable 服务器 |
tool-callback-converter | 启用/禁用将 Spring AI ToolCallbacks 转换为 MCP 工具规范 | true |
name | 服务器标识名称 | mcp-server |
version | 服务器版本 | 1.0.0 |
instructions | 可选,用于指导客户端如何与服务器交互 | null |
type | 服务器类型(SYNC/ASYNC) | SYNC |
capabilities.resource | 启用/禁用资源能力 | true |
capabilities.tool | 启用/禁用工具能力 | true |
capabilities.prompt | 启用/禁用提示模板能力 | true |
capabilities.completion | 启用/禁用补全能力 | true |
resource-change-notification | 启用资源变更通知 | true |
prompt-change-notification | 启用提示模板变更通知 | true |
tool-change-notification | 启用工具变更通知 | true |
tool-response-mime-type | 每个工具名称的响应 MIME 类型 | - |
request-timeout | 请求超时时间 | 20 秒 |
MCP 注解属性
MCP 服务器注解提供了一种使用 Java 注解声明式实现 MCP 服务器处理器的方法。
服务器的 mcp-annotations 配置属性以 spring.ai.mcp.server.annotation-scanner 为前缀:
| 属性 | 描述 | 默认值 |
|---|---|---|
enabled | 启用/禁用 MCP 服务器注解的自动扫描 | true |
Streamable-HTTP 属性
所有 Streamable-HTTP 属性都以 spring.ai.mcp.server.streamable-http 为前缀:
| 属性 | 描述 | 默认值 |
|---|---|---|
mcp-endpoint | 自定义 MCP 端点路径 | /mcp |
keep-alive-interval | 连接保持活动的时间间隔 | null(禁用) |
disallow-delete | 禁止删除操作 | false |
功能与能力
MCP 服务器支持四种主要能力类型,可单独启用或禁用:
- 工具(Tools) —— 通过
spring.ai.mcp.server.capabilities.tool=true|false启用或禁用工具能力 - 资源(Resources) —— 通过
spring.ai.mcp.server.capabilities.resource=true|false启用或禁用资源能力 - 提示(Prompts) —— 通过
spring.ai.mcp.server.capabilities.prompt=true|false启用或禁用提示能力 - 完成(Completions) —— 通过
spring.ai.mcp.server.capabilities.completion=true|false启用或禁用完成能力
默认情况下,所有能力均已启用。禁用某项能力将阻止服务器注册并向客户端暴露对应功能。
MCP Server Boot Starter 允许服务器向客户端暴露工具、资源和提示。它会根据服务器类型自动将注册为 Spring Bean 的自定义能力处理器转换为同步或异步规范。
工具
允许服务器暴露可被语言模型调用的工具。MCP Server Boot Starter 提供以下功能:
- 变更通知支持
- 根据服务器类型自动将 Spring AI 工具 转换为同步/异步规范
- 通过 Spring Bean 自动注册工具规范示例:
@Bean
public ToolCallbackProvider myTools(...) {
List<ToolCallback> tools = ...
return ToolCallbackProvider.from(tools);
}
或使用底层 API:
@Bean
public List<McpServerFeatures.SyncToolSpecification> myTools(...) {
List<McpServerFeatures.SyncToolSpecification> tools = ...
return tools;
}
自动配置功能 会自动检测并注册所有工具回调,包括:
- 单个
ToolCallbackBean ToolCallbackBean 列表ToolCallbackProviderBean
工具按名称去重,重复的工具名称只使用第一次出现的实例。
tool-callback-converter 设置为 false 来禁用所有工具回调的自动检测和注册。工具上下文支持
支持 ToolContext,允许在调用工具时传递上下文信息。ToolContext 中包含一个 McpSyncServerExchange 实例,通过 exchange 键访问,可使用 McpToolUtils.getMcpExchange(toolContext) 获取。
示例 用法包括 exchange.loggingNotification(...) 和 exchange.createMessage(...)。
资源
提供了一种标准化方式,使服务器可以向客户端暴露资源。
- 支持静态和动态资源规范
- 可选的变更通知
- 支持资源模板
- 自动在同步/异步资源规范之间进行转换
通过 Spring Bean 自动注册资源示例:
@Bean
public List<McpServerFeatures.SyncResourceSpecification> myResources(...) {
var systemInfoResource = new McpSchema.Resource(...);
var resourceSpecification = new McpServerFeatures.SyncResourceSpecification(systemInfoResource, (exchange, request) -> {
try {
var systemInfo = Map.of(...);
String jsonContent = new ObjectMapper().writeValueAsString(systemInfo);
return new McpSchema.ReadResourceResult(
List.of(new McpSchema.TextResourceContents(request.uri(), "application/json", jsonContent)));
}
catch (Exception e) {
throw new RuntimeException("生成系统信息失败", e);
}
});
return List.of(resourceSpecification);
}
提示模板
提供了一种标准化方式,使服务器可以向客户端暴露提示(Prompt)模板。
- 支持变更通知
- 支持模板版本管理
- 自动在同步/异步提示规范之间进行转换
通过 Spring Bean 自动注册提示示例
@Bean
public List<McpServerFeatures.SyncPromptSpecification> myPrompts() {
var prompt = new McpSchema.Prompt("greeting", "友好的问候提示",
List.of(new McpSchema.PromptArgument("name", "需要问候的名字", true)));
var promptSpecification = new McpServerFeatures.SyncPromptSpecification(prompt, (exchange, getPromptRequest) -> {
String nameArgument = (String) getPromptRequest.arguments().get("name");
if (nameArgument == null) { nameArgument = "friend"; }
var userMessage = new PromptMessage(Role.USER, new TextContent("Hello " + nameArgument + "! 我今天能为你做些什么?"));
return new GetPromptResult("个性化问候消息", List.of(userMessage));
});
return List.of(promptSpecification);
}
自动补全
提供了一种标准化方式,使服务器可以向客户端暴露补全(Completion)功能。
- 支持同步和异步补全规范
通过 Spring Bean 自动注册:
@Bean
public List<McpServerFeatures.SyncCompletionSpecification> myCompletions() {
var completion = new McpServerFeatures.SyncCompletionSpecification(
new McpSchema.PromptReference(
"ref/prompt", "code-completion", "提供代码补全建议"),
(exchange, request) -> {
// 实现逻辑,返回补全建议
return new McpSchema.CompleteResult(List.of("python", "pytorch", "pyside"), 10, true);
}
);
return List.of(completion);
}
日志记录
提供了一种标准化方式,使服务器可以向客户端发送结构化日志消息。在工具(Tool)、资源(Resource)、提示(Prompt)或补全(Completion)调用处理器中,可使用提供的 McpSyncServerExchange / McpAsyncServerExchange 对象发送日志消息:
(exchange, request) -> {
exchange.loggingNotification(LoggingMessageNotification.builder()
.level(LoggingLevel.INFO)
.logger("test-logger")
.data("This is a test log message")
.build());
}
在 MCP 客户端,可以注册 日志消费者 来处理这些消息:
mcpClientSpec.loggingConsumer((McpSchema.LoggingMessageNotification log) -> {
// 处理日志消息
});
进度通知
提供了一种标准化方式,使服务器可以向客户端发送进度更新。
在工具(Tool)、资源(Resource)、提示(Prompt)或补全(Completion)调用处理器中,可使用提供的 McpSyncServerExchange / McpAsyncServerExchange 对象发送进度通知:
(exchange, request) -> {
exchange.progressNotification(ProgressNotification.builder()
.progressToken("test-progress-token")
.progress(0.25)
.total(1.0)
.message("tool call in progress")
.build());
}
MCP 客户端可以接收进度通知并相应地更新其界面。为此,需要注册一个进度消费者:
mcpClientSpec.progressConsumer((McpSchema.ProgressNotification progress) -> {
// 处理进度通知
});
根目录列表变更
当根节点发生变化时,支持 listChanged 的客户端会发送根节点变更通知。
功能特点:
- 支持根节点变更的监控
- 对于响应式应用,可自动转换为异步消费者
- 可选的 Spring Bean 注册
示例注册方式:
@Bean
public BiConsumer<McpSyncServerExchange, List<McpSchema.Root>> rootsChangeHandler() {
return (exchange, roots) -> {
logger.info("注册根资源: {}", roots);
};
}
Ping
Ping 机制用于服务器验证其客户端是否仍然存活。在工具、资源、提示或完成调用处理器中,可使用提供的 McpSyncServerExchange / McpAsyncServerExchange 交换对象发送 ping 消息:
(exchange, request) -> {
exchange.ping();
}
保持连接
服务器可以选择性地定期向已连接的客户端发送 ping,以验证连接健康状态。
默认情况下,保持连接(keep-alive)功能是禁用的。要启用 keep-alive,请在配置中设置 keep-alive-interval 属性:
spring:
ai:
mcp:
server:
streamable-http:
keep-alive-interval: 30s
streamable-http 服务器,keep-alive 机制仅适用于 从服务器监听消息(SSE) 的连接。使用示例
Streamable HTTP 服务器配置
# Using spring-ai-starter-mcp-server-streamable-webmvc
spring:
ai:
mcp:
server:
protocol: STREAMABLE
name: streamable-mcp-server
version: 1.0.0
type: SYNC
instructions: "This streamable server provides real-time notifications"
resource-change-notification: true
tool-change-notification: true
prompt-change-notification: true
streamable-http:
mcp-endpoint: /api/mcp
keep-alive-interval: 30s
使用 MCP 服务器创建 Spring Boot 应用
@Service
public class WeatherService {
@Tool(description = "Get weather information by city name")
public String getWeather(String cityName) {
// Implementation
}
}
@SpringBootApplication
public class McpServerApplication {
private static final Logger logger = LoggerFactory.getLogger(McpServerApplication.class);
public static void main(String[] args) {
SpringApplication.run(McpServerApplication.class, args);
}
@Bean
public ToolCallbackProvider weatherTools(WeatherService weatherService) {
return MethodToolCallbackProvider.builder().toolObjects(weatherService).build();
}
}
自动配置会将工具回调(ToolCallbacks)自动注册为 MCP 工具。你可以有多个 Bean 生成 ToolCallbacks,自动配置会将它们合并。