Spring AI MCP(模型上下文协议)客户端启动器为Spring Boot应用程序中的MCP客户端功能提供了自动配置。它支持具有各种传输选项的同步和异步客户端实现。
MCP客户端引导启动器提供:
启动器
标准MCP客户端
<dependency> <groupId>org.springframework.ai</groupId> <artifactId>spring-ai-starter-mcp-client</artifactId></dependency>
标准启动器通过STDIO(进程内)、SSE、Streamable-HTTP和无状态Streamable-HTTP传输方式同时连接一个或多个MCP服务器。SSE和Streamable-HTTP传输方式使用基于JDK HttpClient的传输实现。每次连接到MCP服务器都会创建一个新的MCP客户端实例。您可以选择SYNC或ASYNC MCP客户端(注意:不能混合使用同步和异步客户端)。对于生产部署,我们建议使用基于WebFlux的SSE和StreamableHttp连接,并配合使用spring-ai-starter-mcp-client-webflux。
WebFlux 客户端
WebFlux启动器提供了与标准启动器类似的功能,但使用的是基于WebFlux的Streamable-Http、Stateless Streamable-Http和SSE传输实现。
<dependency> <groupId>org.springframework.ai</groupId> <artifactId>spring-ai-starter-mcp-client-webflux</artifactId></dependency>
配置属性
公共属性
公共属性前缀为 spring.ai.mcp.client:
| 属性 | 描述 | 默认值 |
|---|
enabled
| 启用/禁用MCP客户端 | true
|
name
| MCP客户端实例名称 | spring-ai-mcp-client
|
version
| MCP客户端实例版本 | 1.0.0
|
initialized
| 创建时是否初始化客户 | true
|
request-timeout
| MCP客户端请求的持续时间 | 20s
|
type
| 客户端类型(SYNC或ASYNC)。所有客户端必须为sync或mixing;不支持混合 | SYNC
|
root-change-notification
| 为所有客户端启用/禁用根目录更改通知 | true
|
toolcallback.enabled
| 启用/禁用MCP工具回调与Spring AI的工具执行框架集成 | true
|
MCP 注解属性
MCP客户端注解提供了一种使用Java注解实现MCP客户端处理程序的声明式方法。客户端的mcp-annotations属性以spring.ai.mcp.client.annotation-scanner为前缀:
| 属性 | 描述 | 默认值 |
|---|
enabled
| 启用/禁用MCP客户端注释自动扫描 | true
|
标准I/O传输属性
标准I/O传输的属性以spring.ai.mcp.client.stdio为前缀:
| 属性 | 描述 | 默认值 |
|---|
servers-configuration
| 包含JSON格式的MCP服务器配置的资源 | - |
connections
| 命名stdio连接配置Map | - |
connections.[name].command
| 为MCP服务器执行的命令 | - |
connections.[name].args
| 命令参数列表 | - |
connections.[name].env
| 服务器进程环境变量映射 | - |
示例配置:
spring: ai: mcp: client: stdio: root-change-notification: true connections: server1: command: /path/to/server args: - --port=8080 - --mode=production env: API_KEY: your-api-key DEBUG: "true"
或者,您可以使用Claude Desktop格式的外部JSON文件来配置stdio连接:
spring: ai: mcp: client: stdio: servers-configuration: classpath:mcp-servers.json
Claude桌面格式如下:
{ "mcpServers": { "filesystem": { "command": "npx", "args": [ "-y", "@modelcontextprotocol/server-filesystem", "/Users/username/Desktop", "/Users/username/Downloads" ] } }}