Spring AI 1.0.0-M5 版本框架支持 MCP 协议了,对于 MCP 相关的内容,请关注专栏# Claude MCP 模型上下文协议 进行学习,该专栏会不定期更新相关内容,本文主要介绍如何使用 Spring AI 实现 MCP Client 以及 MCP Server等内容。
什么是Mcp
Model Context Protocol 是Anthropic 于2024年11月重磅开源的「模型上下文协议」MCP。其是一种开放的通信协议,是人工智能领域的 “USB 接口”,在大模型和其他数据源(数据、工具、开发环境等)之间建立了双向、并且更加安全的连接。
Mcp 将LLM的数据孤岛被彻底打破,LLM应用和外部数据源、工具都将无缝集成。目标是实现LLM应用程序与外部数据源和工具之间的无缝集成。
Spring AI 如何集成 MCP 协议
Spring AI 提供 Mcp Client 、 Mcp Server 开箱即用 Starters 和 Mcp Utils 相关工具。
Mcp Client Boot Starters
对于 Mcp Client,Spring AI 提供了如下两个 Starter 集成 MCP Client;
- spring-ai-mcp-client-spring-boot-starter: 实现基于 STDIO 和 HTTP 的 SSE 传输协议的 Mcp Client
- spring-ai-mcp-client-webflux-spring-boot-starter: 实现基于 WebFlux 的 SSE 传输协议的 Mcp Client
Mcp Client Boot Starter 支持的能力如下;
- 管理多个客户端实例
- 自动初始化客户端,spring.ai.mcp.client.enabled 开关可以控制是否自动初始化
- 支持多种传输方式,stdio、sse
- 与 Spring AI 工具执行框架无缝结合,Spring AI 框架支持工具执行
- Mcp Client 生命周期管理、在应用程序上下文关闭时自动清理资源。
- 支持 Mcp Client 扩展,定制客户端, 比如;
- 请求配置,如:超时时间等。
- 文件系统权限访问控制。
- 服务端资源、工具、提示变更通知、日志处理等
Mcp Server Boot Starters
对于 Mcp Server, Spring AI 提供了如下三个 Starter 集成 Mcp Server;
- spring-ai-mcp-server-spring-boot-starter: 实现支持 STDIO 传输协议的 Mcp Server
- spring-ai-mcp-server-webmvc-spring-boot-starter: 实现基于 webmvc 的 SSE 传输协议的 Mcp Server
- spring-ai-mcp-server-webflux-spring-boot-starter: 实现基于 webflux 的 SSE 传输协议的 Mcp Server
根据业务场景,可以选择其中的 Mcp Server Starter 来实现一个 Mcp Server
Mcp Server Boot Starter 支持的能力如下;
- 自动配置 MCP 服务端组件 (资源、工具、提示词)。
- 支持同步和异步两种服务模式。
- 灵活的工具、资源和提示注册和更改通知功能。
目前有很多现成的 Mcp Server:
在本文示例代码将选择 server-filesystem server 演示如何使用它。
Mcp Utilities
Mcp 实用程序为将模型上下文协议与 Spring AI 应用程序集成提供了基础支持。实现了 Spring AI 的工具系统和 Mcp 服务器之间的无缝通信,支持同步和异步作。
ToolCallback
使 MCP 工具适配 Spring AI 的工具接口,同时支持同步和异步执行。
ToolCallbackProvider
从 MCP 客户端发现并获取 MCP 工具。
Spring Mcp Client 实现示例
基于 ollama qwen2.5:latest + @modelcontextprotocol/server-filesystem 实现一个本地智能文件管理服务
maven 依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-mcp-client-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-ollama-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<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>
配置文件
spring.ai.ollama.base-url=http://localhost:11434
spring.ai.ollama.chat.model=qwen2.5:latest
# MCP Client Configuration
spring.ai.mcp.client.enabled=true
spring.ai.mcp.client.name=mcp-client
spring.ai.mcp.client.version=1.0.0
spring.ai.mcp.client.type=SYNC
spring.ai.mcp.client.request-timeout=30s
spring.ai.mcp.client.stdio.servers-configuration=classpath:/mcp-servers-config.json
mcp-servers-config.json
{
"mcpServers": {
"filesystem": {
"command": "npx",
"args": [
"-y",
"@modelcontextprotocol/server-filesystem",
"."
]
}
}
}
特别说明:对于 args 的第三个参数,可以指定你想要访问的文件路径,可以是多个,比如: "args": [ "-y", "@modelcontextprotocol/server-filesystem", ".", "/Users" ]
代码实现
package com.ivy.mcp;
import jakarta.annotation.Resource;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.mcp.SyncMcpToolCallbackProvider;
import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@RestController
public static class ChatController {
@Resource
private OllamaChatModel ollamaChatModel;
@Resource
private SyncMcpToolCallbackProvider toolCallbackProvider;
@GetMapping("/chat")
public String call(@RequestParam String input) {
ChatClient chatClient = ChatClient.builder(ollamaChatModel)
.defaultTools(toolCallbackProvider.getToolCallbacks())
.build();
return chatClient.prompt(input).call().content();
}
}
}
测试用例
特别说明:在启动时有些同学可能会报错,大部分原因是因为本地没有安装 npx,自行安装即可。
启动应用程序:localhost:8080/chat?input=
- 有哪些工具可以使用
- 帮我创建一个文件夹 mcp
- 帮我在文件夹mcp下创建一个 test.txt 文件,并写入 hello mcp!
- 帮我将 mcp/test.txt 中 hello mcp 改为 Hello MCP!
通过运行如上测试用例观察运行效果是否符合要求。在测试过程中我使用的本地大模型,但是受限于电脑配置,运行速度比较慢,大家也可以换成别的大模型。
总结
本文主要对 Spring AI 框架集成 Mcp 的一些能力,并实现一个 Mcp Client 调用现成的 @modelcontextprotocol/server-filesystem 实现一个智能本地文件管理服务。
后续还要实现两种场景;
- 集成多个 Mcp Server
- 自己实现一个 Mcp Server [支持 stdio 和 sse ]
本文实例代码可以参考 github.com/Fj-ivy/clau…
参考文章: