此版本仍在开发中,尚未被视为稳定版。如需最新的快照版本,请使用 Spring AI 1.1.3spring-doc.cadn.net.cn

无状态可流式 HTTP MCP 服务器

无状态可流式传输的 HTTP MCP 服务器专为简化部署而设计,无需在请求之间维护会话状态。 这些服务器非常适合微服务架构和云原生部署。spring-doc.cadn.net.cn

设置 spring.ai.mcp.server.protocol=STATELESS 属性
使用 Streamable-HTTP 客户端 连接到无状态服务器。
无状态服务器不支持向 MCP 客户端发送消息请求(例如:提示、采样、心跳检测)。

无状态 WebMVC 服务器

使用 spring-ai-starter-mcp-server-webmvc 依赖:spring-doc.cadn.net.cn

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-starter-mcp-server-webmvc</artifactId>
</dependency>

并将 spring.ai.mcp.server.protocol 属性设置为 STATELESSspring-doc.cadn.net.cn

spring.ai.mcp.server.protocol=STATELESS

无状态 WebFlux 服务器

使用 spring-ai-starter-mcp-server-webflux 依赖:spring-doc.cadn.net.cn

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-starter-mcp-server-webflux</artifactId>
</dependency>

并将 spring.ai.mcp.server.protocol 属性设置为 STATELESSspring-doc.cadn.net.cn

配置属性

常用属性

所有通用属性均以 spring.ai.mcp.server 为前缀:spring-doc.cadn.net.cn

<property> </property> <description> </description> 默认

enabledspring-doc.cadn.net.cn

启用/禁用无状态 MCP 服务器spring-doc.cadn.net.cn

truespring-doc.cadn.net.cn

protocolspring-doc.cadn.net.cn

MCP 服务器协议spring-doc.cadn.net.cn

必须设置为 STATELESS 以启用无状态服务器spring-doc.cadn.net.cn

tool-callback-converterspring-doc.cadn.net.cn

启用/禁用将 Spring AI ToolCallbacks 转换为 MCP Tool 规范的功能spring-doc.cadn.net.cn

truespring-doc.cadn.net.cn

namespring-doc.cadn.net.cn

用于识别的服务器名称spring-doc.cadn.net.cn

mcp-serverspring-doc.cadn.net.cn

versionspring-doc.cadn.net.cn

服务器版本spring-doc.cadn.net.cn

1.0.0spring-doc.cadn.net.cn

instructionsspring-doc.cadn.net.cn

客户端交互的可选说明spring-doc.cadn.net.cn

nullspring-doc.cadn.net.cn

typespring-doc.cadn.net.cn

服务器类型(同步/异步)spring-doc.cadn.net.cn

SYNCspring-doc.cadn.net.cn

capabilities.resourcespring-doc.cadn.net.cn

启用/禁用资源功能spring-doc.cadn.net.cn

truespring-doc.cadn.net.cn

capabilities.toolspring-doc.cadn.net.cn

启用/禁用工具功能spring-doc.cadn.net.cn

truespring-doc.cadn.net.cn

capabilities.promptspring-doc.cadn.net.cn

启用/禁用提示功能spring-doc.cadn.net.cn

truespring-doc.cadn.net.cn

capabilities.completionspring-doc.cadn.net.cn

启用/禁用代码补全功能spring-doc.cadn.net.cn

truespring-doc.cadn.net.cn

tool-response-mime-typespring-doc.cadn.net.cn

每个工具名称的响应 MIME 类型spring-doc.cadn.net.cn

-spring-doc.cadn.net.cn

request-timeoutspring-doc.cadn.net.cn

请求超时时长spring-doc.cadn.net.cn

20 secondsspring-doc.cadn.net.cn

MCP 注解属性

MCP Server Annotations 提供了一种使用 Java 注解以声明式方式实现 MCP 服务器处理程序的方法。spring-doc.cadn.net.cn

服务器 mcp-annotations 的属性以 spring.ai.mcp.server.annotation-scanner 为前缀:spring-doc.cadn.net.cn

<property> </property> <description> </description> 默认值

enabledspring-doc.cadn.net.cn

启用/禁用 MCP 服务器注解的自动扫描spring-doc.cadn.net.cn

truespring-doc.cadn.net.cn

无状态连接属性

所有连接属性均以 spring.ai.mcp.server.stateless 为前缀:spring-doc.cadn.net.cn

<property> </property> <description> </description> 默认

mcp-endpointspring-doc.cadn.net.cn

自定义 MCP 端点路径spring-doc.cadn.net.cn

/mcpspring-doc.cadn.net.cn

disallow-deletespring-doc.cadn.net.cn

禁止删除操作spring-doc.cadn.net.cn

falsespring-doc.cadn.net.cn

功能与能力

MCP Server Boot Starter 允许服务器向客户端暴露工具、资源和提示。 它会根据服务器类型,自动将注册为 Spring Bean 的自定义能力处理器转换为同步/异步规范:spring-doc.cadn.net.cn

工具

允许服务器暴露可由语言模型调用的工具。MCP Server Boot Starter 提供:spring-doc.cadn.net.cn

@Bean
public ToolCallbackProvider myTools(...) {
    List<ToolCallback> tools = ...
    return ToolCallbackProvider.from(tools);
}

或使用低级 API:spring-doc.cadn.net.cn

@Bean
public List<McpStatelessServerFeatures.SyncToolSpecification> myTools(...) {
    List<McpStatelessServerFeatures.SyncToolSpecification> tools = ...
    return tools;
}

自动配置将自动检测并注册来自以下位置的所有工具回调:spring-doc.cadn.net.cn

工具按名称去重,使用每个工具名称首次出现的实例。spring-doc.cadn.net.cn

您可以通过将 tool-callback-converter 设置为 false 来禁用所有工具回调的自动检测和注册。
工具上下文支持不适用于无状态服务器。

资源

为服务器向客户端暴露资源提供了一种标准化的方式。spring-doc.cadn.net.cn

@Bean
public List<McpStatelessServerFeatures.SyncResourceSpecification> myResources(...) {
    var systemInfoResource = new McpSchema.Resource(...);
    var resourceSpecification = new McpStatelessServerFeatures.SyncResourceSpecification(systemInfoResource, (context, request) -> {
        try {
            var systemInfo = Map.of(...);
            String jsonContent = new JsonMapper().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-doc.cadn.net.cn

@Bean
public List<McpStatelessServerFeatures.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 McpStatelessServerFeatures.SyncPromptSpecification(prompt, (context, 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-doc.cadn.net.cn

@Bean
public List<McpStatelessServerFeatures.SyncCompletionSpecification> myCompletions() {
    var completion = new McpStatelessServerFeatures.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);
}

使用示例

无状态服务器配置

spring:
  ai:
    mcp:
      server:
        protocol: STATELESS
        name: stateless-mcp-server
        version: 1.0.0
        type: ASYNC
        instructions: "This stateless server is optimized for cloud deployments"
        streamable-http:
          mcp-endpoint: /api/mcp

使用 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,自动配置会将它们合并。spring-doc.cadn.net.cn