无状态可流式 HTTP MCP 服务器

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

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

无状态WebMVC服务器

使用该Spring-AI-starter-MCP-Server-webmvcDependency: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属性到无 国籍.spring-doc.cadn.net.cn

spring.ai.mcp.server.protocol=STATELESS

无状态WebFlux服务器

使用该Spring-AI-starter-mcp-server-webfluxDependency: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属性到无 国籍.spring-doc.cadn.net.cn

配置属性

常见性质

所有 Common 属性前缀为spring.ai.mcp.server:spring-doc.cadn.net.cn

属性 描述 默认值

启用spring-doc.cadn.net.cn

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

truespring-doc.cadn.net.cn

协议spring-doc.cadn.net.cn

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

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

工具回调转换器spring-doc.cadn.net.cn

启用/禁用将 Spring AI ToolCallback 转换为 MCP 工具规范spring-doc.cadn.net.cn

truespring-doc.cadn.net.cn

名称spring-doc.cadn.net.cn

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

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

版本spring-doc.cadn.net.cn

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

1.0.0spring-doc.cadn.net.cn

指示spring-doc.cadn.net.cn

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

spring-doc.cadn.net.cn

类型spring-doc.cadn.net.cn

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

同步spring-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

能力。完成spring-doc.cadn.net.cn

启用/禁用完成功能spring-doc.cadn.net.cn

truespring-doc.cadn.net.cn

工具-反应-哑剧型spring-doc.cadn.net.cn

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

-spring-doc.cadn.net.cn

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

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

20秒spring-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

属性 描述 默认值

启用spring-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-端点spring-doc.cadn.net.cn

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

/MCPspring-doc.cadn.net.cn

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

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

falsespring-doc.cadn.net.cn

功能与功能

MCP 服务器启动Starters允许服务器向客户端暴露工具、资源和提示符。它自动将注册为 Spring Beans 的自定义能力处理程序转换为基于服务器类型的同步/异步规范: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

你可以通过设置工具回调转换器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工具。你可以有多个豆子生成ToolCallback,自动配置会将它们合并。spring-doc.cadn.net.cn