以下是基于 Spring AI 实现 MCP(Model Context Protocol)客户端与服务端的完整实战示例,涵盖环境准备、依赖配置、代码实现和服务调用流程。
✅ 一、背景说明
MCP(Model Context Protocol) 是一种用于连接大语言模型(LLM)与外部工具的标准协议。
Spring AI 提供了对 MCP 的完整支持,可用于构建:
- MCP Server:暴露业务能力(如天气查询、地图服务等)
- MCP Client:在 Spring 应用中调用远程或本地 MCP 服务
✅ 二、开发环境要求
| 组件 | 版本 |
|---|---|
| JDK | 17+(推荐 JDK 21) |
| Spring Boot | 3.3.x |
| Spring AI | 1.0.0-M6 或更高 |
| 构建工具 | Maven 或 Gradle |
✅ 三、MCP Server 实现(服务端)
1. 添加依赖(pom.xml)
<properties>
<spring-ai.version>1.0.0-M6</spring-ai.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bom</artifactId>
<version>${spring-ai.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- MCP Server WebFlux 支持(SSE 协议) -->
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-mcp-server-webflux</artifactId>

最低0.47元/天 解锁文章
2002

被折叠的 条评论
为什么被折叠?



