引言
Model Context Protocol (MCP) 正在成为连接大模型与外部工具的标准接口。Spring AI 通过 MCP Server 扩展,让 Java 开发者能轻松构建支持 MCP 协议的服务器端应用。本文将演示如何配置依赖、注册工具方法,并深入分析其底层自动配置逻辑。
依赖配置
MCP 官方提供了 Java SDK,同时 Spring AI 在此基础上封装了 Spring Boot Starter。根据项目使用的 Web 框架(WebFlux 或 WebMvc),选择对应的传输层依赖即可。
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.modelcontextprotocol.sdk</groupId>
<artifactId>mcp-bom</artifactId>
<version>0.8.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependency>
<groupId>io.modelcontextprotocol.sdk</groupId>
<artifactId>mcp</artifactId>
</dependency>
<!-- Spring WebFlux-based SSE client and server transport -->
<dependency>
<>io.modelcontextprotocol.sdk
mcp-spring-webflux
io.modelcontextprotocol.sdk
mcp-spring-webmvc


