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

相关推荐
傻瓜搬砖人7 小时前
Spring集成Web环境
java·spring·maven
Java小生不才9 小时前
Spring AI文生音
java·人工智能·spring
二哈赛车手10 小时前
新人笔记---实现简易版的rag的bm25检索(利用ES),以及RAG上传时的ES与向量数据库双写
java·数据库·笔记·spring·elasticsearch·ai
蜜獾云10 小时前
rocketmq traceId重复问题
spring·rocketmq·java-rocketmq
直奔標竿10 小时前
Java开发者AI转型第二十五课!Spring AI 个人知识库实战(四)——RAG来源追溯落地,拒绝AI幻觉
java·开发语言·人工智能·spring boot·后端·spring
薪火铺子12 小时前
Spring Security 6.x 实战指南
java·后端·spring
BING_Algorithm12 小时前
一文搞定 AOP 所有核心知识点
spring boot·后端·spring
Cyan_RA912 小时前
SpringMVC 请求数据绑定与参数映射 详解
java·后端·spring·mvc·springmvc·映射请求数据
Java成神之路-14 小时前
多 Filter、多 Interceptor 执行优先级控制方案
spring·java web
java1234_小锋14 小时前
Spring AI 2.0 开发Java Agent智能体 - Spring AI项目调用本地Ollama模型
java·人工智能·spring·spring ai2.0