ARTICLE DETAIL

资讯详情

深耕网站建设与运营推广的一线实战洞察。

Spring AI 实战:MCP 协议原理、Server 与 Client 完整搭建全流程【实战案例】

Spring AI 实战:MCP 协议原理、Server 与 Client 完整搭建全流程【实战案例】 在之前文已经介绍了 Agent 的Tools工具调用能力并基于 Spring AI 的 Tool 机制为智能行程规划 Agent 集成了天气查询和景点推荐两大工具。通过为 Agent 赋予工具调用能力使其能够主动获取实时信息、执行实际操作从而让 Agent 从能说会道进化为能说会做。然而随着 Agent 应用场景的复杂化一个关键问题逐渐浮现工具的接入方式缺乏统一标准。不同的工具提供方各有各的接口规范、各有各的调用方式开发者需要为每个工具单独编写适配代码。更麻烦的是当一个 Agent 需要对接多个外部系统时工具的注册、发现、调用、管理都变得极其繁琐。这是因为当前的 Tool 机制虽然解决了让 Agent 能调用工具的问题但未解决工具如何标准化的问题。本文将在此基础上进一步介绍MCPModel Context Protocol协议这是一种标准化的工具接入协议让 Agent 与外部工具的交互从定制开发升级为即插即用真正实现工具生态的开放与共享。一、为什么 Agent 需要 MCP 协议1.1 MCP 协议解决的核心问题MCPModel Context Protocol是由 Anthropic 公司提出的一种标准化协议用于规范 AI 模型与外部工具之间的交互方式。其核心目标是让工具的接入从定制开发变为即插即用。通过 MCP工具提供方只需按照协议规范实现一次工具服务称为 MCP Server所有支持 MCP 的 AI 应用称为 MCP Client都可以直接调用无需任何定制开发。这就像 USB 接口标准——任何 USB 设备都可以插到任何电脑上使用无需为每台电脑单独开发驱动程序。MCP 协议带来以下核心价值在标准化层面MCP 定义了统一的工具描述格式、调用协议、数据传输规范工具提供方和消费方遵循同一标准即可互通。在解耦层面工具的开发与使用完全解耦。工具团队专注实现 MCP ServerAgent 团队只需引入 MCP Client双方无需协调接口细节。在生态共享层面MCP Server 可以发布到公共仓库供所有 Agent 使用。开发者可以直接使用社区提供的现成工具无需重复开发。在动态发现层面MCP Client 可以动态发现 MCP Server 提供的工具列表、工具描述、参数 schema无需预先硬编码。1.2 典型应用场景场景MCP 优势实现效果企业内部工具集成统一标准一次开发多处使用各业务系统的工具可被所有 Agent 调用开源工具生态工具共享避免重复造轮子社区提供的 MCP Server 可直接引入使用多 Agent 协作标准协议无缝对接不同 Agent 可共享同一组 MCP ServerSaaS 工具接入第三方提供 MCP Server用户开箱即用企业无需开发直接配置即可使用二、核心概念MCP 协议架构2.1 什么是 MCPMCPModel Context Protocol是一种基于 JSON-RPC 2.0 的应用层协议专门用于 AI 模型与外部工具之间的通信。其核心设计理念是将工具服务化——工具不再是一段代码而是一个独立运行的服务通过标准协议对外暴露能力。一从架构角度理解MCP 定义了两个核心角色MCP Server工具服务端提供工具能力的服务。Server 负责实现具体的工具逻辑并通过 MCP 协议暴露工具列表、工具描述、调用接口。一个 MCP Server 可以提供多个工具。MCP Client工具客户端消费工具能力的 AI 应用。Client 负责连接 MCP Server发现可用工具将工具注册到 Agent并在 Agent 调用工具时转发请求到 Server。2从通信角度理解MCP 支持两种传输方式标准输入输出StdioMCP Server 作为本地进程运行Client 通过标准输入输出与 Server 通信。适用于工具与 Agent 在同一进程或同一机器的场景延迟低、部署简单。HTTP/SSEMCP Server 作为网络服务运行Client 通过 HTTP 请求与 Server 通信支持服务器推送事件SSE。适用于工具独立部署、跨网络访问的场景扩展性强。2.2 MCP 协议的核心要素一个完整的 MCP 服务包含以下核心要素工具ToolMCP Server 提供的具体能力单元。每个工具包含名称、描述、输入参数 schema、执行逻辑。与前文的 Tool 概念类似但 MCP 工具由 Server 统一管理。资源ResourceMCP Server 可以暴露的数据源如文件、数据库、API响应。Agent 可以读取资源内容获取上下文信息。资源是只读的用于提供背景知识。提示词PromptMCP Server 可以提供的预定义提示词模板。Agent 可以使用这些模板快速构建特定任务的系统提示提高提示工程效率。采样SamplingMCP Server 可以请求 Client 代表其调用大模型。这允许 Server 实现复杂的 Agentic 行为如多轮推理、自我反思。2.3 MCP 与传统 Tool 的区别虽然 MCP 的工具概念与前文的Tool类似但二者在架构层面有本质区别对比项传统 ToolToolMCP Tool定义方式Java 方法 注解独立服务 协议描述部署方式与 Agent 同进程独立部署网络访问发现方式编译时注册运行时动态发现复用性项目内复用跨项目、跨团队复用维护成本修改需重新编译部署独立升级无需重启 Agent生态共享无法共享可发布到公共仓库可以用一个形象的比喻传统 Tool 就像私有工具箱每个 Agent 都要自己准备工具MCP Tool 就像公共工具库所有 Agent 共享同一套工具服务按需调用。2.4 MCP 协议的通信流程MCP 协议的完整通信流程如下第一步连接建立。MCP Client 连接 MCP Server发送初始化请求协商协议版本和 capabilities能力集。Server 返回支持的功能列表如工具、资源、提示词。第二步工具发现。Client 向 Server 请求工具列表tools/listServer 返回所有可用工具的名称、描述、参数 schema。Client 将这些工具动态注册到 Agent。第三步工具调用。Agent 在对话过程中决定调用某个工具Client 将调用请求转发给 Servertools/call包含工具名称和参数。Server 执行工具逻辑返回执行结果。第四步结果处理。Client 将工具结果包装为模型可理解的格式追加到对话上下文继续推理。整个过程由 MCP Client 自动编排开发者只需配置 Server 地址无需关心底层通信细节。关于MCP的详细讲解可以查看之前介绍MCP的文章《一文拆解 MCPAI 大模型领域爆火的标准化交互协议全解析真实案例》《MCP 协议不空谈Cherry Studio 配置高德地图MCP搭建 AI 行程助手的真实案例》三、Spring AI 实现 MCP 的原理3.1 核心组件Spring AI 从 1.0.0 版本开始提供 MCP 支持包含以下核心组件McpClientSpring AI 提供的 MCP 客户端实现负责连接 MCP Server、发现工具、转发调用请求。支持 Stdio 和 HTTP/SSE 两种传输方式。McpSyncClient / McpAsyncClient同步和异步两种调用模式。同步模式简单直观适合大多数场景异步模式支持高并发适合生产环境。McpToolProvider将 MCP Server 提供的工具适配为 Spring AI 的 Tool 格式注册到 ChatClient。开发者无需手动转换工具描述。McpServerSpring AI 提供的 MCP 服务端实现帮助开发者快速构建 MCP Server。支持注解式定义工具自动生成协议描述。3.2 工作机制MCP ClientSpring AI 作为 MCP Client 的工作机制分为四个阶段第一阶段配置连接。开发者在配置文件中指定 MCP Server 的连接信息如进程命令、HTTP 地址。Spring AI 在应用启动时建立连接完成协议握手。第二阶段工具发现。Spring AI 自动向 MCP Server 请求工具列表解析工具描述名称、描述、参数 schema转换为内部的 ToolDefinition 格式。第三阶段工具注册。McpToolProvider 将发现的工具注册到 ChatClientAgent 在对话过程中可以直接调用这些工具调用方式与前文的Tool完全一致。第四阶段调用转发。当 Agent 调用 MCP 工具时Spring AI 将调用请求通过 MCP 协议转发给 Server等待 Server 执行完成并返回结果再将结果回传给 Agent。3.3 工作机制MCP ServerSpring AI 作为 MCP Server 的工作机制分为三个阶段第一阶段定义工具。开发者使用Tool注解定义工具方法或使用Resource注解定义资源。Spring AI 自动解析这些定义生成 MCP 协议要求的工具描述。第二阶段启动服务。Spring AI 启动 MCP Server监听指定端口HTTP 方式或等待标准输入Stdio 方式准备接收 Client 请求。第三阶段处理请求。Server 接收 Client 的工具列表请求或工具调用请求根据请求内容执行对应的工具方法返回执行结果。整个过程遵循 JSON-RPC 2.0 规范。3.4 MCP 工具描述的协议格式MCP 协议采用 JSON Schema 进行工具能力描述其整体格式与传统 Tool 定义相近并在原有基础上做了能力扩展。典型的 MCP 工具结构定义规则如下名称字段描述工具名称name工具名工具功能描述description功能描述入参结构inputSchema入参扩展字段annotations可用于标识工具特性相较于普通的 ToolMCP 工具新增了annotations扩展字段可用于标识工具特性具体Json 格式如下代码语言javascriptAI代码解释{ name: get_weather, description: 查询指定城市的当前天气情况支持选择温度单位。, inputSchema: { type: object, properties: { city: { type: string, description: 城市名称例如 Beijing 或 Shanghai }, unit: { type: string, description: 温度单位可选值为 celsius 或 fahrenheit, enum: [celsius, fahrenheit] } }, required: [city] } }四、【案例】构建MCP Client 和 MCP Server 实现旅游规划MCP4.1 案例目标延续前面的智能行程规划 Agent 案例。Agent 已具备记忆能力和工具调用能力。本次案例的目标是构建 MCP Server将前文的天气查询和景点推荐工具改造为 MCP Server作为独立服务运行通过 MCP 协议暴露工具能力。实现 MCP Client为 Agent 集成 MCP Client动态发现 MCP Server 提供的工具并在对话过程中调用这些工具。实现效果Agent 不再直接依赖 Java 代码定义的工具而是通过 MCP 协议动态发现并调用 MCP Server 提供的工具。当工具需要升级时只需重启 MCP ServerAgent 无需任何改动。4.2 技术选型技术模块选项方案开发框架Spring Boot 3.5.3 Spring AI 1.0.0大模型智谱 AIChatGLM-4-Flash核心依赖Spring Web、Spring AI MCP、LombokMCP ServerSpring AI MCP Server WebMVCHTTP 方式MCP ClientSpring AI MCP ClientHTTP/SSE 方式辅助能力继承前文的记忆能力和工具调用能力4.3 项目初始化与配置项目结构如下代码语言javascriptAI代码解释Weiz-SpringAI-MCP/ ├── pom.xml 父工程统一版本 ├── Weiz-SpringAI-MCP-Server/ MCP 服务端 └── Weiz-SpringAI-MCP-Client/ MCP 客户端Agent1. 创建 MCP Server 项目项目名称Weiz-SpringAI-MCP-ServerJDK 版本17Spring Boot 版本3.5.3核心依赖Spring Web、Spring AI MCP Server、Lombok2. 配置 MCP Server pom.xml 依赖代码语言javascriptAI代码解释?xml version1.0 encodingUTF-8? project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd modelVersion4.0.0/modelVersion parent groupIdcom.example/groupId artifactIdWeiz-SpringAI-MCP/artifactId version0.0.1-SNAPSHOT/version /parent artifactIdWeiz-SpringAI-MCP-Server/artifactId nameWeiz-SpringAI-MCP-Server/name descriptionMCP Server 提供天气查询和景点推荐工具/description properties java.version17/java.version maven.compiler.source17/maven.compiler.source maven.compiler.target17/maven.compiler.target /properties dependencies !-- Spring Web 依赖 -- dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-web/artifactId /dependency !-- MCP Server 依赖 -- dependency groupIdorg.springframework.ai/groupId artifactIdspring-ai-starter-mcp-server-webmvc/artifactId /dependency dependency groupIdorg.projectlombok/groupId artifactIdlombok/artifactId /dependency dependency groupIdcom.fasterxml.jackson.core/groupId artifactIdjackson-databind/artifactId /dependency dependency groupIdorg.projectlombok/groupId artifactIdlombok/artifactId optionaltrue/optional /dependency /dependencies build plugins plugin groupIdorg.springframework.boot/groupId artifactIdspring-boot-maven-plugin/artifactId /plugin /plugins /build /project3. 配置 MCP Server application.properties代码语言javascriptAI代码解释# spring.application.nameWeiz-SpringAI-MCP-Server server.port8081 # MCP Server spring.ai.mcp.server.typeSYNC spring.ai.mcp.server.nametrip-tools-server spring.ai.mcp.server.version1.0.0 spring.ai.mcp.server.stiofalse spring.ai.mcp.server.sse-message-endpoint/mcp/message spring.ai.mcp.server.sse-endpoint/mcp/sse # logging.level.org.springframework.aiDEBUG logging.level.com.exampleDEBUG4. 创建 MCP Client 项目项目名称Weiz-SpringAI-MCP-ClientJDK 版本17Spring Boot 版本3.5.3核心依赖Spring Web、Spring AI MCP Client、Spring AI 智谱 AI Starter、Lombok5. 配置 MCP Client pom.xml 依赖代码语言javascriptAI代码解释?xml version1.0 encodingUTF-8? project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd modelVersion4.0.0/modelVersion parent groupIdcom.example/groupId artifactIdWeiz-SpringAI-MCP/artifactId version0.0.1-SNAPSHOT/version /parent artifactIdWeiz-SpringAI-MCP-Client/artifactId nameWeiz-SpringAI-MCP-Client/name descriptionMCP Client 集成 MCP Server 工具到 Agent/description dependencies dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-web/artifactId /dependency !-- 智谱 AI -- dependency groupIdorg.springframework.ai/groupId artifactIdspring-ai-starter-model-zhipuai/artifactId /dependency !-- MCP Client 依赖 -- dependency groupIdorg.springframework.ai/groupId artifactIdspring-ai-starter-mcp-client/artifactId /dependency dependency groupIdorg.projectlombok/groupId artifactIdlombok/artifactId optionaltrue/optional /dependency /dependencies /project6. 配置 MCP Client application.properties代码语言javascriptAI代码解释# spring.application.nameWeiz-SpringAI-MCP-Client server.port8080 # AI spring.ai.zhipuai.api-key858b5b317ac7449b9e3dc9c75baa8ecf.t3KDMKdKERWDQPed spring.ai.zhipuai.base-urlhttps://open.bigmodel.cn/api/paas spring.ai.zhipuai.chat.options.modelGLM-4-Flash spring.ai.zhipuai.chat.options.temperature0.7 # MCP Client HTTP/SSE spring.ai.mcp.client.sse.connections.trip-tools.urlhttp://localhost:8081 spring.ai.mcp.client.typeSYNC # logging.level.org.springframework.aiDEBUG logging.level.com.exampleDEBUG4.4 核心开发构建 MCP Server1. 定义天气查询工具代码语言javascriptAI代码解释package com.example.weizspringai.mcp; import org.springframework.ai.tool.annotation.Tool; import org.springframework.ai.tool.annotation.ToolParam; import org.springframework.stereotype.Component; /** * 天气查询 MCP 工具 * 通过 MCP 协议暴露供 Client 调用 */ Component public class WeatherMcpTool { Tool(description 获取指定城市的实时天气信息返回天气状况、温度范围、空气质量、出行建议) public String getWeather( ToolParam(description 城市名称支持中文如北京、上海、广州、成都、西安、杭州) String city ) { // 模拟天气 API 调用实际项目替换为真实 API return simulateWeatherApi(city); } private String simulateWeatherApi(String city) { String weatherData; if (city.contains(北京)) { weatherData 晴气温 18-25°C空气质量优紫外线较强建议防晒; } else if (city.contains(上海)) { weatherData 多云气温 20-28°C空气质量良有轻微雾霾; } else if (city.contains(广州)) { weatherData 小雨气温 22-26°C湿度 85%建议携带雨具; } else if (city.contains(成都)) { weatherData 阴天气温 16-22°C空气质量良适合室内外活动; } else if (city.contains(西安)) { weatherData 晴朗气温 15-23°C空气质量良适合户外观光; } else if (city.contains(杭州)) { weatherData 多云转晴气温 19-27°C空气质量优西湖游览佳日; } else { weatherData 晴气温 20-25°C空气质量良适合出行; } return String.format( {\city\: \%s\, \weather\: \%s\, \source\: \实时天气数据\}, city, weatherData ); } }2. 定义景点推荐工具代码语言javascriptAI代码解释package com.example.weizspringai.mcp; import org.springframework.ai.tool.annotation.Tool; import org.springframework.ai.tool.annotation.ToolParam; import org.springframework.stereotype.Component; import java.util.List; import java.util.Map; import java.util.stream.Collectors; /** * 景点推荐 MCP 工具 * 通过 MCP 协议暴露供 Client 调用 */ Component public class AttractionMcpTool { // 模拟景点数据库与前文一致 private static final MapString, ListAttractionInfo ATTRACTIONS_DB Map.of( 北京, List.of( new AttractionInfo(故宫, 人文, 5.0, 3-4小时, 世界文化遗产明清皇家宫殿), new AttractionInfo(长城八达岭, 人文, 5.0, 半天, 世界七大奇迹之一), new AttractionInfo(颐和园, 人文, 4.8, 2-3小时, 皇家园林博物馆), new AttractionInfo(天坛, 人文, 4.7, 2小时, 明清祭天场所), new AttractionInfo(北京环球影城, 娱乐, 4.9, 全天, 顶级主题乐园) ), 上海, List.of( new AttractionInfo(外滩, 人文, 4.9, 1-2小时, 万国建筑博览群), new AttractionInfo(上海迪士尼, 娱乐, 4.8, 全天, 亚洲顶级迪士尼乐园), new AttractionInfo(豫园, 人文, 4.6, 2小时, 江南古典园林), new AttractionInfo(东方明珠, 人文, 4.7, 2小时, 上海地标建筑) ), 成都, List.of( new AttractionInfo(大熊猫繁育研究基地, 自然, 4.9, 半天, 近距离观赏大熊猫), new AttractionInfo(宽窄巷子, 人文, 4.7, 2-3小时, 成都历史文化街区), new AttractionInfo(都江堰, 人文, 4.8, 半天, 世界文化遗产水利工程), new AttractionInfo(青城山, 自然, 4.7, 全天, 道教名山天然氧吧) ), 西安, List.of( new AttractionInfo(兵马俑, 人文, 5.0, 半天, 世界第八大奇迹), new AttractionInfo(大雁塔, 人文, 4.8, 2小时, 唐代佛教建筑), new AttractionInfo(古城墙, 人文, 4.7, 2-3小时, 中国现存最完整古城墙), new AttractionInfo(大唐不夜城, 人文, 4.8, 晚上, 沉浸式唐文化体验) ), 杭州, List.of( new AttractionInfo(西湖, 自然, 5.0, 半天, 世界文化遗产人间天堂), new AttractionInfo(灵隐寺, 人文, 4.7, 2小时, 千年古刹), new AttractionInfo(宋城, 娱乐, 4.6, 全天, 大型演艺主题公园), new AttractionInfo(西溪湿地, 自然, 4.5, 半天, 城市湿地公园) ) ); Tool(description 根据城市和景点类型偏好推荐热门景点返回景点名称、类型、评分、建议游览时长、景点简介) public String recommendAttractions( ToolParam(description 城市名称如北京、上海、成都、西安、杭州) String city, ToolParam(description 景点类型偏好人文、自然、娱乐不填则推荐全部类型, required false) String preference ) { ListAttractionInfo attractions ATTRACTIONS_DB.getOrDefault(city, List.of()); if (attractions.isEmpty()) { return String.format({\error\: \暂无 %s 的景点数据请尝试其他城市\}, city); } if (preference ! null !preference.isEmpty()) { attractions attractions.stream() .filter(a - a.type.contains(preference) || preference.contains(a.type)) .collect(Collectors.toList()); } StringBuilder result new StringBuilder(); result.append({\city\: \).append(city).append(\, ); if (preference ! null !preference.isEmpty()) { result.append(\preference\: \).append(preference).append(\, ); } result.append(\attractions\: [); for (int i 0; i attractions.size(); i) { AttractionInfo a attractions.get(i); result.append(String.format( {\name\: \%s\, \type\: \%s\, \rating\: \%s\, \duration\: \%s\, \intro\: \%s\}, a.name, a.type, a.rating, a.duration, a.intro )); if (i attractions.size() - 1) { result.append(, ); } } result.append(], \count\: ).append(attractions.size()).append(}); return result.toString(); } private static class AttractionInfo { String name; String type; String rating; String duration; String intro; AttractionInfo(String name, String type, String rating, String duration, String intro) { this.name name; this.type type; this.rating rating; this.duration duration; this.intro intro; } } }3. 配置 MCP Server 自动注册工具代码语言javascriptAI代码解释package com.example.weizspringai.config; import com.example.weizspringai.mcp.AttractionMcpTool; import com.example.weizspringai.mcp.WeatherMcpTool; import org.springframework.ai.tool.ToolCallbackProvider; import org.springframework.ai.tool.method.MethodToolCallbackProvider; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * MCP Server 配置 * 自动注册工具到 MCP Server */ Configuration public class McpServerConfig { Bean public ToolCallbackProvider weatherToolProvider(WeatherMcpTool weatherMcpTool) { return MethodToolCallbackProvider.builder() .toolObjects(weatherMcpTool) .build(); } Bean public ToolCallbackProvider attractionToolProvider(AttractionMcpTool attractionMcpTool) { return MethodToolCallbackProvider.builder() .toolObjects(attractionMcpTool) .build(); } }4.5 核心开发实现 MCP Client1. 配置 MCP Client 连接MCP Client 的连接信息已在application.properties中配置Spring AI 会自动建立连接。代码语言javascriptAI代码解释# spring.application.nameWeiz-SpringAI-MCP-Client server.port8080 # AI spring.ai.zhipuai.api-key858b5b317ac7449b9e3dc9c75baa8ecf.t3KDMKdKERWDQPed spring.ai.zhipuai.base-urlhttps://open.bigmodel.cn/api/paas spring.ai.zhipuai.chat.options.modelGLM-4-Flash spring.ai.zhipuai.chat.options.temperature0.7 # MCP Client HTTP/SSE spring.ai.mcp.client.sse.connections.trip-tools.urlhttp://localhost:8081 spring.ai.mcp.client.typeSYNC # logging.level.org.springframework.aiDEBUG logging.level.com.exampleDEBUG2. 创建 Agent 服务代码语言javascriptAI代码解释package com.example.weizspringai.service; import lombok.RequiredArgsConstructor; import org.springframework.ai.chat.client.ChatClient; import org.springframework.ai.mcp.SyncMcpToolCallbackProvider; import org.springframework.stereotype.Service; /** * 基于 MCP 的智能行程规划 Agent * 通过 MCP Client 动态发现并调用 MCP Server 提供的工具 */ Service RequiredArgsConstructor public class McpTripAgentService { private final ChatClient.Builder chatClientBuilder; private final SyncMcpToolCallbackProvider toolCallbackProvider; /** * 带 MCP 工具调用的行程规划 * param demand 用户出行需求 * return 结合实时数据的个性化行程规划 */ public String planTripWithMcpTools(String demand) { // 定义 Agent 行为规则 String systemPrompt 你是一个具备工具调用能力的智能行程规划 Agent核心规则如下 1. 当用户询问天气时必须调用 getWeather 工具获取实时天气数据 2. 当用户需要景点推荐时必须调用 recommendAttractions 工具获取景点信息 3. 结合天气数据和景点信息生成完整的行程规划建议 4. 行程安排要考虑天气因素如雨天推荐室内景点 5. 所有实时信息必须通过工具获取严禁编造数据。 回复风格简洁专业突出实用信息。 ; // 构建 ChatClient 并自动注册 MCP 工具 ChatClient chatClient chatClientBuilder .defaultSystem(systemPrompt) .defaultTools(toolCallbackProvider.getToolCallbacks()) .build(); // 调用大模型 return chatClient.prompt() .user(demand) .call() .content(); } }3. 创建 Controller 接口代码语言javascriptAI代码解释package com.example.weizspringai.controller; import com.example.weizspringai.service.McpTripAgentService; import lombok.RequiredArgsConstructor; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.util.Map; /** * MCP Agent 接口 */ RestController RequestMapping(/agent/mcp) RequiredArgsConstructor public class McpAgentController { private final McpTripAgentService mcptripAgentService; /** * 带 MCP 工具调用的行程规划接口 * param demand 用户出行需求 * return 智能行程规划结果 */ GetMapping(/plan) public MapString, String planTrip(RequestParam(demand) String demand) { String tripPlan mcptripAgentService.planTripWithMcpTools(demand); return Map.of( userDemand, demand, tripPlan, tripPlan, agentType, 基于 MCP 的智能行程规划 Agent ); } }五、系统测试与验证5.1 测试 1验证 MCP Server 工具列表启动 MCP Server 后访问工具列表接口验证工具是否正确注册。返回结果包含两个工具WeatherMcpTool天气查询和 AttractionMcpTool景点推荐工具描述和参数 schema 与定义一致。测试通过验证了 MCP Server 正确暴露了工具能力。5.2 测试 2查询天气验证 MCP Client 调用天气工具启动 MCP Client 后访问接口http://localhost:8080/agent/mcp/plan?demand北京明天天气怎么样适合出行吗Agent 通过 MCP Client 调用 MCP Server 的getWeather工具返回结果代码语言javascriptAI代码解释{ userDemand: 北京明天天气怎么样适合出行吗, agentType: 基于 MCP 的智能行程规划 Agent, tripPlan: 北京明天天气晴朗气温在18-25°C之间空气质量优紫外线较强建议外出时做好防晒措施。适合出行。 }Agent 基于工具返回数据生成回答。测试通过验证了 MCP Client 正确转发调用请求到 MCP Server。5.3 测试 3推荐景点验证 MCP Client 调用景点工具访问接口http://localhost:8080/agent/mcp/plan?demand推荐几个北京的人文景点最好评分高的Agent 通过 MCP Client 调用 MCP Server 的recommendAttractions工具返回结果代码语言javascriptAI代码解释{ userDemand: 推荐几个北京的人文景点最好评分高的, agentType: 基于 MCP 的智能行程规划 Agent, tripPlan: 根据您的要求我为您推荐了几个北京的人文景点其中包括故宫、长城八达岭、颐和园和天坛。这些景点都是世界文化遗产具有很高的历史和文化价值。故宫是世界文化遗产明清皇家宫殿建议游览时长为3-4小时长城八达岭是世界七大奇迹之一建议游览时长为半天颐和园是皇家园林博物馆建议游览时长为2-3小时天坛是明清祭天场所建议游览时长为2小时。您可以根据自己的兴趣和时间进行选择。 }测试通过验证了 MCP 协议的参数传递和结果解析正确性。5.4 测试 4综合场景多工具组合调用访问接口http://localhost:8080/agent/mcp/plan?demand帮我规划北京周末两天游先看看天气再推荐几个人文景点生成完整行程Agent 识别到这是一个综合请求通过 MCP Client 自动先后调用 MCP Server 的两个工具。首先调用getWeather工具获取北京天气数据然后调用recommendAttractions工具获取北京人文景点列表最后综合两部分数据生成完整行程规划。代码语言javascriptAI代码解释{ userDemand: 帮我规划北京周末两天游先看看天气再推荐几个人文景点生成完整行程, agentType: 基于 MCP 的智能行程规划 Agent, tripPlan: 北京周末两天游行程规划如下\n\n第一天\n- 上午参观故宫建议游览3-4小时了解明清皇家宫殿的历史和文化。\n- 下午游览长城八达岭半天时间足够感受世界七大奇迹之一的壮丽。\n\n第二天\n- 上午游览颐和园建议游览2-3小时欣赏皇家园林的美丽风光。\n- 下午参观天坛建议游览2小时了解明清祭天的历史和文化。\n\n请注意北京天气晴朗气温18-25°C紫外线较强建议外出时做好防晒措施。 }测试通过验证了 MCP 协议支持多工具组合调用Agent 通过 MCP Client 综合多源数据生成完整回答的能力与前文的传统 Tool 方式完全一致。5.5 测试结果分析能力项验证结果说明MCP Server 工具注册✅ 通过MCP Server 正确暴露 getWeather 和 recommendAttractions 两个工具MCP Client 工具发现✅ 通过MCP Client 自动发现并注册 MCP Server 的所有工具天气工具调用✅ 通过Agent 通过 MCP 调用天气工具返回实时数据景点工具调用✅ 通过Agent 通过 MCP 调用景点工具返回精准推荐多工具组合调用✅ 通过单次请求中通过 MCP 同时调用两个工具与前文 Tool 效果一致✅ 通过MCP 工具的调用效果与传统 Tool 方式完全一致通过测试可以验证MCP 协议在不改变 Agent 使用体验的前提下实现了工具的标准化接入。Agent 通过 MCP Client 调用工具的方式与前文通过Tool注解直接调用工具的方式完全一致对上层业务透明。总结本文基于 MCP 协议完成智能行程规划 Agent 的工具标准化接入。全文核心围绕三方面展开阐释 MCP 协议核心概念介绍 Spring AI 依托原生能力自动完成 MCP 连接建立、工具发现与调用转发开发者仅需配置服务端地址、定义 Agent 行为规则即可结合实战案例掌握基于 Spring AI 搭建 MCP 服务的完整方法。同时梳理了 Agent 能力演进脉络基础 Agent通过系统提示词定义行为逻辑→ 记忆型 Agent依托 ChatMemory 留存用户偏好→ 工具型 Agent通过 Tool 注解调用外部能力、获取实时数据→ MCP 协议型 Agent借助标准化协议实现跨系统工具共享复用。后续将进一步深挖 Agent 高级特性详解多 Agent 协作系统的落地实现多个子 Agent 分工承担专属业务任务依托 MCP 协议实现工具与数据互通共享协同承接复杂业务场景助力搭建可正式投产落地的企业。
返回列表