spring ai 初始化工具,代码链分析

以如下依赖为示例:

复制代码
<dependency>
            <groupId>org.springframework.ai</groupId>
            <artifactId>spring-ai-starter-mcp-server-webflux</artifactId>
            <version>1.1.0</version>
        </dependency>

yaml配置

复制代码
spring:
  ai:
    mcp:
      server:
        protocol: STATELESS
        name: xxx-streamable-mcp-server
        version: 1.0.0
        type: SYNC
        instructions: "xxx"
        resource-change-notification: true
        tool-change-notification: true
        prompt-change-notification: true

StatelessToolCallbackConverterAutoConfiguration

无状态工具执行服务自动装配类

初始化工具tool

org.springframework.ai.mcp.server.common.autoconfigure.StatelessToolCallbackConverterAutoConfiguration#syncTools

将tool作为参数,创建McpStatelessAsyncServer

org.springframework.ai.mcp.server.common.autoconfigure.McpServerStatelessAutoConfiguration#mcpStatelessSyncServer

io.modelcontextprotocol.server.McpServer.StatelessSyncSpecification#build

io.modelcontextprotocol.server.McpStatelessAsyncServer#McpStatelessAsyncServer(设置handler)

查询工具列表处理类

io.modelcontextprotocol.server.McpStatelessAsyncServer#toolsListRequestHandler

McpServerStatelessWebFluxAutoConfiguration

McpServerStatelessWebFluxAutoConfiguration 作用是将指定的endpoint暴露出去

复制代码
@AutoConfiguration(
    before = {McpServerStatelessAutoConfiguration.class}
)
@ConditionalOnClass({McpSchema.class})
@EnableConfigurationProperties({McpServerStreamableHttpProperties.class})
@Conditional({McpServerStdioDisabledCondition.class, McpServerStatelessAutoConfiguration.EnabledStatelessServerCondition.class})
public class McpServerStatelessWebFluxAutoConfiguration {
    @Bean
    @ConditionalOnMissingBean
    public WebFluxStatelessServerTransport webFluxStatelessServerTransport(@Qualifier("mcpServerObjectMapper") ObjectMapper objectMapper, McpServerStreamableHttpProperties serverProperties) {
        return WebFluxStatelessServerTransport.builder().jsonMapper(new JacksonMcpJsonMapper(objectMapper)).messageEndpoint(serverProperties.getMcpEndpoint()).build();
    }

    @Bean
    @ConditionalOnMissingBean(
        name = {"webFluxStatelessServerRouterFunction"}
    )
    public RouterFunction<?> webFluxStatelessServerRouterFunction(WebFluxStatelessServerTransport webFluxStatelessTransport) {
        return webFluxStatelessTransport.getRouterFunction();
    }
}

RouterFunction用的很巧妙

todo~~

相关推荐
蚰蜒螟13 分钟前
一次 Spring AOP 与定时任务引发的死锁排查实录
java·spring·firefox
Java爱好狂.31 分钟前
Java就业需要学习哪些内容?
spring·程序员·springboot·架构师·java面试·java面试题·java八股文
listening7772 小时前
HarmonyOS 6.1 元服务深度优化:从“秒开”到“常驻”的极致体验
java·开发语言·spring
空中湖7 小时前
Spring AI Agent 编排:ReAct 模式 + 多 Agent 协作实战
人工智能·spring·react.js
yio_yin1 天前
Spring事务管理(数据一致性)
数据库·sql·spring
dear_bi_MyOnly1 天前
【SpringBoot配置文件】
java·spring boot·后端·学习·spring·java-ee·学习方法
our_times1 天前
2026年Java开发者破局指南:Spring AI 2.0 与 Agent 开发实战
java·人工智能·spring
wear工程师1 天前
@Transactional 标了却没开事务?先看这次调用有没有经过代理
java·spring
爱学习的小可爱卢1 天前
SpringCloud——微服务实战:OpenFeign与Nacos服务调用详解
spring·spring cloud·微服务
静水楼台x1 天前
spring security
java·数据库·spring