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

无状态可流式HTTP MCP服务器

无状态的可流式HTTP MCP服务器旨在简化部署,请求之间不维护会话状态。 这类服务器非常适合微服务架构和云原生部署。spring-doc.cadn.net.cn

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

无状态 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

配置属性

常见属性

所有公共属性都以0前缀spring-doc.cadn.net.cn

属性 描述 默认

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 服务器注解提供了一种声明式方法,用于通过 Java 注解实现 MCP 服务器处理器。spring-doc.cadn.net.cn

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

属性 描述 默认值

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

属性 描述 默认

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 服务器Starters允许服务器向客户端公开工具、资源和提示。 它会根据服务器类型,自动将注册为 Spring Bean 的自定义能力处理器转换为同步/异步规范:spring-doc.cadn.net.cn

工具

允许服务器公开可由语言模型调用的工具。MCP 服务器Starters提供: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 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-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