此版本仍在开发中,尚未被视为稳定版本。最新的快照版本请使用 Spring AI 1.0.0-SNAPSHOT! |
MCP 服务器启动Starters
Spring AI MCP(模型上下文协议)服务器Starters提供了在 Spring Boot 应用程序中设置 MCP 服务器的自动配置。它支持将 MCP 服务器功能与 Spring Boot 的自动配置系统无缝集成。
MCP Server Boot Starter 提供:
-
自动配置 MCP 服务器组件
-
支持同步和异步作模式
-
多个传输层选项
-
灵活的工具、资源和提示注册
-
更改通知功能
首先
根据您的运输要求选择以下Starters之一:
标准 MCP 服务器
完整的 MCP 服务器功能支持STDIO
服务器传输。
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-mcp-server-spring-boot-starter</artifactId>
</dependency>
-
适用于命令行和桌面工具
-
不需要额外的 Web 依赖项
Starters会激活McpServerAutoConfiguration
auto-configuration 负责:
-
配置基本 Server 组件
-
处理工具、资源和提示注册
-
管理 Server 功能和更改通知
-
提供同步和异步服务器实现
WebMVC 服务器传输
完整的 MCP 服务器功能支持SSE
(Server-Sent Events) 基于 Spring MVC 的服务器传输和可选的STDIO
运输。
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-mcp-server-webmvc-spring-boot-starter</artifactId>
</dependency>
Starters会激活McpWebMvcServerAutoConfiguration
和McpServerAutoConfiguration
auto-configurations 提供:
-
使用 Spring MVC 的基于 HTTP 的传输 (
WebMvcSseServerTransport
) -
自动配置的 SSE 终端节点
-
自选
STDIO
传输(通过设置spring.ai.mcp.server.stdio=true
) -
包括
spring-boot-starter-web
和mcp-spring-webmvc
依赖
WebFlux 服务器传输
完整的 MCP 服务器功能支持SSE
(Server-Sent Events) 基于 Spring WebFlux 的服务器传输和可选的STDIO
运输。
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-mcp-server-webflux-spring-boot-starter</artifactId>
</dependency>
Starters会激活McpWebFluxServerAutoConfiguration
和McpServerAutoConfiguration
auto-configurations 提供:
-
使用 Spring WebFlux (
WebFluxSseServerTransport
) -
自动配置的反应式 SSE 终端节点
-
自选
STDIO
传输(通过设置spring.ai.mcp.server.stdio=true
) -
包括
spring-boot-starter-webflux
和mcp-spring-webflux
依赖
配置属性
所有属性都以spring.ai.mcp.server
:
财产 | 描述 | 违约 |
---|---|---|
|
启用/禁用 MCP 服务器 |
|
|
启用/禁用 stdio 传输 |
|
|
用于标识的服务器名称 |
|
|
服务器版本 |
|
|
服务器类型 (SYNC/ASYNC) |
|
|
启用资源更改通知 |
|
|
启用工具更改通知 |
|
|
启用提示更改通知 |
|
|
Web 传输的 SSE 终端节点路径 |
|
Sync/Async 服务器类型
-
同步服务器 - 使用
McpSyncServer
. 它专为应用程序中的简单请求-响应模式而设计。 要启用此服务器类型,请将spring.ai.mcp.server.type=SYNC
在您的配置中。 激活后,它会自动处理同步工具注册的配置。 -
异步服务器 - 异步服务器实现使用
McpAsyncServer
并针对非阻塞作进行了优化。 要启用此服务器类型,请使用spring.ai.mcp.server.type=ASYNC
. 此服务器类型使用内置的 Project Reactor 支持自动设置异步工具注册。
交通方式
MCP 服务器支持三种传输机制,每种机制都有其专用的Starters:
-
标准输入/输出 (STDIO) -
spring-ai-mcp-server-spring-boot-starter
-
Spring MVC(服务器发送的事件)-
spring-ai-mcp-server-webmvc-spring-boot-starter
-
Spring WebFlux(反应式 SSE)-
spring-ai-mcp-server-webflux-spring-boot-starter
特性和功能
MCP Server Boot Starter 允许服务器向客户端公开工具、资源和提示。 它根据服务器类型自动将注册为 Spring bean 的自定义功能处理程序转换为同步/异步注册:
工具
允许服务器公开可由语言模型调用的工具。MCP Server Boot Starter 提供:
-
更改通知支持
-
工具会根据服务器类型自动转换为同步/异步注册
-
通过 Spring bean 自动注册工具:
@Bean
public ToolCallbackProvider myTools(...) {
List<ToolCallback> tools = ...
return ToolCallbackProvider.from(tools);
}
或使用低级 API:
@Bean
public List<McpServerFeatures.SyncToolRegistration> myTools(...) {
List<McpServerFeatures.SyncToolRegistration> tools = ...
return tools;
}
资源管理
为服务器提供一种标准化的方法,以便向客户端公开资源。
-
静态和动态资源注册
-
可选更改通知
-
支持资源模板
-
同步/异步资源注册之间的自动转换
-
通过 Spring bean 自动注册资源:
@Bean
public List<McpServerFeatures.SyncResourceRegistration> myResources(...) {
var systemInfoResource = new McpSchema.Resource(...);
var resourceRegistration = new McpServerFeatures.SyncResourceRegistration(systemInfoResource, 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("Failed to generate system info", e);
}
});
return List.of(resourceRegistration);
}
及时管理
为服务器提供一种标准化的方法,以便向客户端公开提示模板。
-
更改通知支持
-
模板版本控制
-
同步/异步提示注册之间的自动转换
-
通过 Spring bean 自动提示注册:
@Bean
public List<McpServerFeatures.SyncPromptRegistration> myPrompts() {
var prompt = new McpSchema.Prompt("greeting", "A friendly greeting prompt",
List.of(new McpSchema.PromptArgument("name", "The name to greet", true)));
var promptRegistration = new McpServerFeatures.SyncPromptRegistration(prompt, getPromptRequest -> {
String nameArgument = (String) getPromptRequest.arguments().get("name");
if (nameArgument == null) { nameArgument = "friend"; }
var userMessage = new PromptMessage(Role.USER, new TextContent("Hello " + nameArgument + "! How can I assist you today?"));
return new GetPromptResult("A personalized greeting message", List.of(userMessage));
});
return List.of(promptRegistration);
}
根更改使用者
当根发生变化时,支持listChanged
发送 Root Change 通知。
-
支持监控根更改
-
自动转换为响应式应用程序的异步使用者
-
通过 Spring bean 进行可选注册
@Bean
public Consumer<List<McpSchema.Root>> rootsChangeConsumer() {
return roots -> {
logger.info("Registering root resources: {}", roots);
};
}
使用示例
标准 STDIO 服务器配置
# Using spring-ai-mcp-server-spring-boot-starter
spring:
ai:
mcp:
server:
name: stdio-mcp-server
version: 1.0.0
type: SYNC
WebMVC 服务器配置
# Using spring-ai-mcp-server-webmvc-spring-boot-starter
spring:
ai:
mcp:
server:
name: webmvc-mcp-server
version: 1.0.0
type: SYNC
sse-message-endpoint: /mcp/messages
WebFlux 服务器配置
# Using spring-ai-mcp-server-webflux-spring-boot-starter
spring:
ai:
mcp:
server:
name: webflux-mcp-server
version: 1.0.0
type: ASYNC # Recommended for reactive applications
sse-message-endpoint: /mcp/messages
使用 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();
}
}
自动配置会自动将工具回调注册为 MCP 工具。 你可以让多个 bean 生成 ToolCallbacks。自动配置将合并它们。
示例应用
-
天气服务器 (WebFlux) - 具有 WebFlux 传输的 Spring AI MCP 服务器Starters。
-
天气服务器 (STDIO) - 具有 STDIO 传输的 Spring AI MCP 服务器启动Starters。
-
Book Library Server (WebFlux) - 具有 WebFlux 传输的 Spring AI MCP 服务器引导Starters。
-
天气服务器手动配置 - Spring AI MCP 服务器启动Starters,它不使用自动配置,而是使用 Java SDK 手动配置服务器。