spring ai 初始化工具,代码链分析
org.springframework.ai.mcp.server.common.autoconfigure.StatelessToolCallbackConverterAutoConfiguration#syncToolsio.modelcontextprotocol.server.McpServer.StatelessSyncSpecification#buildio.modelcontext
以如下依赖为示例:
<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~~
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐



所有评论(0)