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

相关推荐
鬼先生_sir18 分钟前
SpringCloud-Sentinel(熔断降级 & 流量控制)
spring·spring cloud·sentinel
me83236 分钟前
【Java】Spring MVC接口执行流程详解:从前端请求到参数封装全解析(前端到底是怎么和后端交互的?)
java·spring·mvc
cheems952744 分钟前
[SpringMVC] 加法计算器
spring
云烟成雨TD1 小时前
Spring AI 1.x 系列【28】基于内存和 MySQL 的多轮对话实现案例
java·人工智能·spring
cheems95273 小时前
[SpringMVC] Spring MVC 留言板开发实战
java·spring·mvc
武超杰3 小时前
SpringBoot 整合 Spring Security 实现权限控制
spring boot·后端·spring
云烟成雨TD3 小时前
Spring AI 1.x 系列【27】Chat Memory API:让 LLM 拥有上下文记忆能力
java·人工智能·spring
weixin_704266053 小时前
项目总结一
java·前端·spring boot·后端·spring
沃尔威武3 小时前
Spring Cloud Gateway实战:微服务API网关从零到一
java·spring·微服务
陌殇殇3 小时前
003 Spring AI Alibaba框架整合百炼大模型平台 — Memory会话记忆、Tool工具、RAG增强检索、ReAct智能体
人工智能·spring·ai