MCP 这个概念相信大家已经听了无数次了,但不同人会有不同的解释,你可能也是听得云里雾里的。
不过没关系,今天这篇内容会通过 Spring AI 给你实现一个 MCP 的 Client 和 Server 架构,让你彻底搞懂 MCP 的概念,以及学会 MCP 的开发技能。
MCP 是 Model Context Protocol,模型上下文协议,它是由 Anthropic(Claude 大模型母公司)提出的开放协议,用于大模型连接外部“数据源”的一种协议。
它可以通俗的理解为 Java 界的 Spring Cloud Openfeign,只不过 Openfeign 是用于微服务通讯的,而 MCP 用于大模型通讯的,但它们都是为了通讯获取某项数据的一种机制,如下图所示:
MCP 存在的意义是它解决了大模型时代最关键的三个问题:数据孤岛、开发低效和生态碎片化等问题。
大模型本身无法直接访问实时数据或本地资源(如数据库、文件系统),传统方式需要手动复制粘贴或定制接口。MCP 通过标准化协议,让大模型像“插USB”一样直接调用外部工具和数据源,例如:
在之前每个大模型(如 DeepSeek、ChatGPT)需要为每个工具单独开发接口(Function Calling),导致重复劳动,MCP 通过统一协议:
MCP 让大模型从“被动应答”变为“主动调用工具”,例如:
MCP 的诞生,相当于为AI世界建立了“通用语言”,让模型、数据和工具能高效协作,最终释放大模型的全部潜力。
MCP 架构分为以下 3 部分:
运行流程:
Spring AI MCP 是通过 Spring Boot 集成扩展了 MCP 的 Java SDK(开发工具),它同时提供了 Spring Boot 客户端和服务器的启动器,方便使用 Spring AI MCP 快速开发 AI 应用程序。
当前案例中,我们使用 MCP 实现一个天气查询小助手,其中包含的主要角色有:
具体交互流程如下:
MCP Server 主要实现步骤如下:
关键实现代码如下。
<dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-mcp-server-webflux</artifactId>
</dependency>
</dependencies>
<repositories>
<repository>
<name>Central Portal Snapshots</name>
<id>central-portal-snapshots</id>
<url>https://central.sonatype.com/repository/maven-snapshots/</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<releases>
<enabled>false</enabled>
</releases>
</repository>
</repositories>
MCP Server 依赖有三种类型:
MCP Server 包含以下配置信息:
配置项 | 描述 | 默认值 |
---|---|---|
enabled | 启用/禁用 MCP 服务器 | TRUE |
stdio | 启用/禁用 stdio 传输 | FALSE |
name | 用于标识的服务器名称 | mcp-server |
version | 服务器版本 | 1.0.0 |
type | 服务器类型 (SYNC/ASYNC) | SYNC |
resource-change-notification | 启用资源更改通知 | TRUE |
prompt-change-notification | 启用提示更改通知 | TRUE |
tool-change-notification | 启用工具更改通知 | TRUE |
tool-response-mime-type | (可选)每个工具名称的响应 MIME 类型。例如,将 mime 类型与工具名称相关联spring.ai.mcp.server.tool-response-mime-type.generateImage=image/pngimage/pnggenerateImage() | - |
sse-message-endpoint | Web 传输的 SSE 终端节点路径 | /mcp/message |
其中 MCP Server 又分为以下两种类型。
编写天气预报查询伪代码:
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.stereotype.Service;
import java.util.Map;
@Service
public class WeatherService {
@Tool(description = "根据城市名称获取天气预报")
public String getWeatherByCity(String city) {
Map<String, String> mockData = Map.of(
"西安", "晴天",
"北京", "小雨",
"上海", "大雨"
);
return mockData.getOrDefault(city, "抱歉:未查询到对应城市!");
}
}
@Bean
public ToolCallbackProvider weatherTools(WeatherService weatherService) {
return MethodToolCallbackProvider.builder().toolObjects(weatherService).build();
}
这样 MCP Server 就编写完成了。
MCP Client 主要实现步骤如下:
核心实现代码如下:
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ClientController {
@Autowired
private ChatClient chatClient;
@RequestMapping("/chat")
public String chat(@RequestParam(value = "msg",defaultValue = "今天天气如何?") String msg) {
String response = chatClient.prompt()
.user(msg)
.call()
.content();
System.out.println("响应结果: " + response);
return response;
}
}
最终执行结果如下:
因为 MCP Server 只配置了 3 个城市,所以查询结果和预期相符:
想要获取完整案例的同学加V:vipStone【备注MCP】
到这里使用 Spring AI 就实现了 MCP Client 和 Server 的调用了,可以看出 MCP 的推出只是为了增强大模型的能力的,有了 MCP 协议之后,任何大模型就可以调用任意实现了 MCP Server 的服务了,这样就无线扩充了大模型的能力,为 AI 的发展提供了标准的协议和便利的对接。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有