Spring AI 实战 MCP 工具, 客户端服务端实现完整示例

以下是基于 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>
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值