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~~

相关推荐
廋到被风吹走20 小时前
【Spring】AOP深度解析:代理机制、拦截器链与事务失效全解
java·spring·缓存
步步为营DotNet21 小时前
深度探索.NET 中ValueTask:优化异步性能的轻量级利器
java·spring·.net
heartbeat..21 小时前
Spring 声明式事务:原理、使用及失效场景详解
java·spring·面试·事务
何中应21 小时前
@Autowrited和@Resource注解的区别及使用场景
java·开发语言·spring boot·后端·spring
这就是佬们吗21 小时前
Windows 的 CMD 网络环境:解决终端无法联网与更新的终极指南
java·windows·git·python·spring·maven
heartbeat..1 天前
Spring AOP 全面详解(通俗易懂 + 核心知识点 + 完整案例)
java·数据库·spring·aop
Jing_jing_X1 天前
AI分析不同阶层思维 二:Spring 的事务在什么情况下会失效?
java·spring·架构·提升·薪资
程序猿零零漆1 天前
Spring之旅 - 记录学习 Spring 框架的过程和经验(十一)基于XML方式、注解的声明式事务控制、Spring整合Web环境
xml·学习·spring
短剑重铸之日1 天前
《SpringBoot4.0初识》第五篇:实战代码
java·后端·spring·springboot4.0
heartbeat..1 天前
Spring MVC 全面详解(Java 主流 Web 开发框架)
java·网络·spring·mvc·web