STDIO 和 SSE MCP 伺服器
STDIO 和 SSE MCP 伺服器支援多種傳輸機制,每種機制都有其專用的啟動器。
STDIO MCP 伺服器
透過 STDIO 伺服器傳輸提供完整的 MCP 伺服器功能支援。
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-mcp-server</artifactId>
</dependency>
-
適用於命令列和桌面工具
-
無需額外 Web 依賴
-
基本伺服器元件的配置
-
工具、資源和提示規範的處理
-
伺服器功能和變更通知的管理
-
支援同步和非同步伺服器實現
SSE WebMVC 伺服器
透過基於 Spring MVC 的 SSE(伺服器傳送事件)伺服器傳輸和可選的 STDIO 傳輸提供完整的 MCP 伺服器功能支援。
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-mcp-server-webmvc</artifactId>
</dependency>
-
使用 Spring MVC 的基於 HTTP 的傳輸 (
WebMvcSseServerTransportProvider) -
自動配置的 SSE 端點
-
可選的
STDIO傳輸(透過設定spring.ai.mcp.server.stdio=true啟用) -
包含
spring-boot-starter-web和mcp-spring-webmvc依賴項
SSE WebFlux 伺服器
透過基於 Spring WebFlux 的 SSE(伺服器傳送事件)伺服器傳輸和可選的 STDIO 傳輸提供完整的 MCP 伺服器功能支援。
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-mcp-server-webflux</artifactId>
</dependency>
該啟動器啟用 McpWebFluxServerAutoConfiguration 和 McpServerAutoConfiguration 自動配置以提供
-
使用 Spring WebFlux 的反應式傳輸 (
WebFluxSseServerTransportProvider) -
自動配置的反應式 SSE 端點
-
可選的
STDIO傳輸(透過設定spring.ai.mcp.server.stdio=true啟用) -
包含
spring-boot-starter-webflux和mcp-spring-webflux依賴項
|
由於 Spring Boot 的預設行為,當 classpath 中同時存在 |
配置屬性
通用屬性
所有通用屬性都以 spring.ai.mcp.server 作為字首
| 財產 | 描述 | 預設值 |
|---|---|---|
|
啟用/停用 MCP 伺服器 |
|
|
啟用/停用Spring AI ToolCallbacks到MCP工具規範的轉換 |
|
|
啟用/停用 STDIO 傳輸 |
|
|
用於識別的伺服器名稱 |
|
|
伺服器版本 |
|
|
可選說明,為客戶端提供如何與此伺服器互動的指導 |
|
|
伺服器型別(同步/非同步) |
|
|
啟用/停用資源能力 |
|
|
啟用/停用工具能力 |
|
|
啟用/停用提示能力 |
|
|
啟用/停用完成能力 |
|
|
啟用資源變更通知 |
|
|
啟用提示變更通知 |
|
|
啟用工具變更通知 |
|
|
每個工具名稱的可選響應 MIME 型別。例如, |
|
|
等待伺服器響應的持續時間,超時則請求失敗。適用於透過客戶端發出的所有請求,包括工具呼叫、資源訪問和提示操作 |
|
MCP註解屬性
MCP伺服器註解提供了一種使用Java註解實現MCP伺服器處理程式的宣告性方法。
伺服器mcp-annotations屬性以 spring.ai.mcp.server.annotation-scanner 為字首
| 財產 | 描述 | 預設值 |
|---|---|---|
|
啟用/停用MCP伺服器註解的自動掃描 |
|
SSE 屬性
所有 SSE 屬性都以 spring.ai.mcp.server 作為字首
| 財產 | 描述 | 預設值 |
|---|---|---|
|
用於 Web 傳輸的自定義 SSE 訊息端點路徑,供客戶端傳送訊息 |
|
|
用於 Web 傳輸的自定義 SSE 端點路徑 |
|
|
可選 URL 字首。例如, |
|
|
連線保活間隔 |
|
出於向後相容性原因,SSE 屬性沒有額外的字尾(如 .sse)。 |
功能和能力
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,允許將上下文資訊傳遞給工具呼叫。它包含一個 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("Failed to generate system info", e);
}
});
return List.of(resourceSpecification);
}
提示
提供了一種標準化方式,供伺服器向客戶端公開提示模板。
-
變更通知支援
-
模板版本控制
-
同步/非同步提示規範之間的自動轉換
-
透過Spring bean自動生成提示規範
@Bean
public List<McpServerFeatures.SyncPromptSpecification> myPrompts() {
var prompt = new McpSchema.Prompt("greeting", "A friendly greeting prompt",
List.of(new McpSchema.PromptArgument("name", "The name to greet", 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 + "! How can I assist you today?"));
return new GetPromptResult("A personalized greeting message", List.of(userMessage));
});
return List.of(promptSpecification);
}
補全
提供了一種標準化方式,供伺服器向客戶端公開完成能力。
-
支援同步和非同步完成規範
-
透過Spring bean自動註冊
@Bean
public List<McpServerFeatures.SyncCompletionSpecification> myCompletions() {
var completion = new McpServerFeatures.SyncCompletionSpecification(
new McpSchema.PromptReference(
"ref/prompt", "code-completion", "Provides code completion suggestions"),
(exchange, request) -> {
// Implementation that returns completion suggestions
return new McpSchema.CompleteResult(List.of("python", "pytorch", "pyside"), 10, true);
}
);
return List.of(completion);
}
日誌
提供伺服器向客戶端傳送結構化日誌訊息的標準化方式。在工具、資源、提示或補全呼叫處理程式中,使用提供的 McpSyncServerExchange/McpAsyncServerExchange exchange 物件傳送日誌訊息
(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) -> {
// Handle log messages
});
進度
提供伺服器向客戶端傳送進度更新的標準化方式。在工具、資源、提示或補全呼叫處理程式中,使用提供的 McpSyncServerExchange/McpAsyncServerExchange exchange 物件傳送進度通知
(exchange, request) -> {
exchange.progressNotification(ProgressNotification.builder()
.progressToken("test-progress-token")
.progress(0.25)
.total(1.0)
.message("tool call in progress")
.build());
}
MCP 客戶端可以接收進度通知並相應地更新其 UI。為此,它需要註冊一個進度消費者。
mcpClientSpec.progressConsumer((McpSchema.ProgressNotification progress) -> {
// Handle progress notifications
});
根列表變更
當根發生變化時,支援 listChanged 的客戶端會發送根變化通知。
-
支援監控根變化
-
自動轉換為響應式應用程式的非同步消費者
-
透過 Spring bean 可選註冊
@Bean
public BiConsumer<McpSyncServerExchange, List<McpSchema.Root>> rootsChangeHandler() {
return (exchange, roots) -> {
logger.info("Registering root resources: {}", roots);
};
}
Ping
伺服器的 Ping 機制,用於驗證其客戶端是否仍然存活。在工具、資源、提示或補全呼叫處理程式中,使用提供的 McpSyncServerExchange/McpAsyncServerExchange exchange 物件傳送 ping 訊息
(exchange, request) -> {
exchange.ping();
}
使用示例
標準 STDIO 伺服器配置
# Using spring-ai-starter-mcp-server
spring:
ai:
mcp:
server:
name: stdio-mcp-server
version: 1.0.0
type: SYNC
WebMVC 伺服器配置
# Using spring-ai-starter-mcp-server-webmvc
spring:
ai:
mcp:
server:
name: webmvc-mcp-server
version: 1.0.0
type: SYNC
instructions: "This server provides weather information tools and resources"
capabilities:
tool: true
resource: true
prompt: true
completion: true
# sse properties
sse-message-endpoint: /mcp/messages
keep-alive-interval: 30s
WebFlux 伺服器配置
# Using spring-ai-starter-mcp-server-webflux
spring:
ai:
mcp:
server:
name: webflux-mcp-server
version: 1.0.0
type: ASYNC # Recommended for reactive applications
instructions: "This reactive server provides weather information tools and resources"
capabilities:
tool: true
resource: true
prompt: true
completion: true
# sse properties
sse-message-endpoint: /mcp/messages
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();
}
}
自動配置將自動註冊工具回撥作為MCP工具。您可以有多個生成ToolCallbacks的bean,自動配置將合併它們。
示例應用程式
-
天氣伺服器 (WebFlux) - 帶有 WebFlux 傳輸的 Spring AI MCP 伺服器啟動器
-
天氣伺服器 (STDIO) - 帶有 STDIO 傳輸的 Spring AI MCP 伺服器啟動器
-
天氣伺服器手動配置 - 不使用自動配置但使用 Java SDK 手動配置伺服器的 Spring AI MCP 伺服器啟動器