MCP 客户端启动Starters

Spring AI MCP(模型上下文协议)客户端启动Starters为 Spring Boot 应用中的 MCP 客户端功能提供自动配置。 它支持同步和异步客户端实现,并支持多种传输选项。spring-doc.cadn.net.cn

MCP 客户端启动Starters提供:spring-doc.cadn.net.cn

首先

标准MCP客户端

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-starter-mcp-client</artifactId>
</dependency>

标准Starters通过STDIO(进行中),SSE,可流式HTTP无状态流式HTTP运输。 SSE和可流式Http传输采用了基于JDK的Http客户端传输实现。 每次连接到MCP服务器都会创建一个新的MCP客户端实例。 你可以选择其中任何一个同步异步MCP客户端(注意:不能混合使用同步和异步客户端)。 对于生产部署,我们建议使用基于WebFlux的SSE和StreamableHttp连接,配合Spring-AI-starter-mcp-client-webflux.spring-doc.cadn.net.cn

WebFlux客户端

WebFlux 起始器提供与标准起始器类似的功能,但采用基于 WebFlux 的 Streamable-Http、无状态 Streamable-Http 和 SSE 传输实现。spring-doc.cadn.net.cn

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-starter-mcp-client-webflux</artifactId>
</dependency>

配置属性

常见性质

常见的性质前缀为spring.ai.mcp.client:spring-doc.cadn.net.cn

属性 描述 默认值

启用spring-doc.cadn.net.cn

启用/禁用MCP客户端spring-doc.cadn.net.cn

truespring-doc.cadn.net.cn

名称spring-doc.cadn.net.cn

MCP客户端实例名称spring-doc.cadn.net.cn

Spring-ai-mcp-clientspring-doc.cadn.net.cn

版本spring-doc.cadn.net.cn

MCP 客户端实例的版本spring-doc.cadn.net.cn

1.0.0spring-doc.cadn.net.cn

初始 化spring-doc.cadn.net.cn

是否在创建时初始化客户端spring-doc.cadn.net.cn

truespring-doc.cadn.net.cn

请求超时spring-doc.cadn.net.cn

MCP客户端请求的超时持续时间spring-doc.cadn.net.cn

20多岁spring-doc.cadn.net.cn

类型spring-doc.cadn.net.cn

客户端类型(同步或非同步)。所有客户端必须是同步或异步;不支持混合spring-doc.cadn.net.cn

同步spring-doc.cadn.net.cn

根变更通知spring-doc.cadn.net.cn

启用/禁用所有客户端的根更改通知spring-doc.cadn.net.cn

truespring-doc.cadn.net.cn

toolcallback.enabledspring-doc.cadn.net.cn

启用/禁用MCP工具回调与Spring AI工具执行框架的集成spring-doc.cadn.net.cn

truespring-doc.cadn.net.cn

MCP 注释属性

MCP 客户端注释提供了一种声明式方式,利用 Java 注释实现 MCP 客户端处理器。 客户端mcp注释属性前缀为Spring.ai.mcp.client.annotation-scanner:spring-doc.cadn.net.cn

属性 描述 默认值

启用spring-doc.cadn.net.cn

启用/禁用MCP客户端注释自动扫描spring-doc.cadn.net.cn

truespring-doc.cadn.net.cn

Stdio交通资产

标准I/O传输的属性前缀为spring.ai.mcp.client.stdio:spring-doc.cadn.net.cn

属性 描述 默认值

服务器配置spring-doc.cadn.net.cn

包含 MCP 服务器配置的 JSON 格式资源spring-doc.cadn.net.cn

-spring-doc.cadn.net.cn

连接spring-doc.cadn.net.cn

命名stdio连接配置的映射spring-doc.cadn.net.cn

-spring-doc.cadn.net.cn

连接。[name].commandspring-doc.cadn.net.cn

MCP服务器执行命令spring-doc.cadn.net.cn

-spring-doc.cadn.net.cn

连接。[name].argsspring-doc.cadn.net.cn

命令参数列表spring-doc.cadn.net.cn

-spring-doc.cadn.net.cn

连接。[name].envspring-doc.cadn.net.cn

服务器进程环境变量映射spring-doc.cadn.net.cn

-spring-doc.cadn.net.cn

示例配置:spring-doc.cadn.net.cn

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-doc.cadn.net.cn

spring:
  ai:
    mcp:
      client:
        stdio:
          servers-configuration: classpath:mcp-servers.json

Claude 桌面格式如下:spring-doc.cadn.net.cn

{
  "mcpServers": {
    "filesystem": {
      "command": "npx",
      "args": [
        "-y",
        "@modelcontextprotocol/server-filesystem",
        "/Users/username/Desktop",
        "/Users/username/Downloads"
      ]
    }
  }
}

Windows STDIO 配置

在Windows上,像这样的命令NPX,NPM节点批处理文件的形式实现 (.cmd),而不是本地可执行文件。爪哇的ProcessBuilder无法直接执行批处理文件,并且需要cmd.exe /c包装纸。

为什么Windows需要特殊处理

当Java的ProcessBuilder(内部使用StdioClientTransport)尝试在Windows上生成进程,只能执行:spring-doc.cadn.net.cn

Windows批处理文件,比如npx.cmd,npm.cmd,甚至python.cmd(来自 Microsoft Store 的)要求cmd.exe用贝壳来处决他们。spring-doc.cadn.net.cn

解决方案:包装cmd.exe

将批处理文件命令包裹为cmd.exe /c:spring-doc.cadn.net.cn

Windows配置:spring-doc.cadn.net.cn

{
  "mcpServers": {
    "filesystem": {
      "command": "cmd.exe",
      "args": [
        "/c",
        "npx",
        "-y",
        "@modelcontextprotocol/server-filesystem",
        "C:\\Users\\username\\Desktop"
      ]
    }
  }
}

Linux/macOS 配置:spring-doc.cadn.net.cn

{
  "mcpServers": {
    "filesystem": {
      "command": "npx",
      "args": [
        "-y",
        "@modelcontextprotocol/server-filesystem",
        "/Users/username/Desktop"
      ]
    }
  }
}

跨平台程序化配置

对于需要跨平台运行且不需单独配置文件的应用程序,请在 Spring Boot 应用中使用作系统检测功能:spring-doc.cadn.net.cn

@Bean(destroyMethod = "close")
@ConditionalOnMissingBean(McpSyncClient.class)
public McpSyncClient mcpClient() {
    ServerParameters stdioParams;

    if (isWindows()) {
        // Windows: cmd.exe /c npx approach
        var winArgs = new ArrayList<>(Arrays.asList(
            "/c", "npx", "-y", "@modelcontextprotocol/server-filesystem", "target"));
        stdioParams = ServerParameters.builder("cmd.exe")
                .args(winArgs)
                .build();
    } else {
        // Linux/Mac: direct npx approach
        stdioParams = ServerParameters.builder("npx")
                .args("-y", "@modelcontextprotocol/server-filesystem", "target")
                .build();
    }

    return McpClient.sync(new StdioClientTransport(stdioParams, McpJsonMapper.createDefault()))
            .requestTimeout(Duration.ofSeconds(10))
            .build()
            .initialize();
}

private static boolean isWindows() {
    return System.getProperty("os.name").toLowerCase().contains("win");
}
使用程序化配置时@Bean@ConditionalOnMissingBean(McpSyncClient.class)以避免与 JSON 文件自动配置冲突。

路径考虑

相对路径(推荐便于携带):spring-doc.cadn.net.cn

{
  "command": "cmd.exe",
  "args": ["/c", "npx", "-y", "@modelcontextprotocol/server-filesystem", "target"]
}

MCP 服务器根据应用程序的工作目录解析相对路径。spring-doc.cadn.net.cn

绝对路径(Windows需要反斜线或逃脱前斜线):spring-doc.cadn.net.cn

{
  "command": "cmd.exe",
  "args": ["/c", "npx", "-y", "@modelcontextprotocol/server-filesystem", "C:\\Users\\username\\project\\target"]
}

需要cmd.exe的常见Windows批处理文件

参考实现

请参阅 Spring AI 示例——文件系统,了解一个完整的跨平台 MCP 客户端实现,能够自动检测作系统并适当配置客户端。spring-doc.cadn.net.cn

可流式HTTP传输属性

用于连接可流式HTTP和无状态流式HTTP的MCP服务器。spring-doc.cadn.net.cn

可流式HTTP传输的属性前缀为spring.ai.mcp.client.streamable-http:spring-doc.cadn.net.cn

属性 描述 默认值

连接spring-doc.cadn.net.cn

命名的 Streamable-HTTP 连接配置映射spring-doc.cadn.net.cn

-spring-doc.cadn.net.cn

连接。[name].urlspring-doc.cadn.net.cn

用于与MCP服务器进行Streamable-Http通信的基础URL端点spring-doc.cadn.net.cn

-spring-doc.cadn.net.cn

连接。[name].端点spring-doc.cadn.net.cn

用于连接的Streamable-HTTP端点(作为URL后缀)spring-doc.cadn.net.cn

/MCPspring-doc.cadn.net.cn

示例配置:spring-doc.cadn.net.cn

spring:
  ai:
    mcp:
      client:
        streamable-http:
          connections:
            server1:
              url: http://localhost:8080
            server2:
              url: http://otherserver:8081
              endpoint: /custom-sse

SSE运输资产

服务器发送事件(SSE)传输的属性前缀为spring.ai.mcp.client.sse:spring-doc.cadn.net.cn

属性 描述 默认值

连接spring-doc.cadn.net.cn

命名SSE连接配置映射spring-doc.cadn.net.cn

-spring-doc.cadn.net.cn

连接。[name].urlspring-doc.cadn.net.cn

用于与MCP服务器的SSE通信的基础URL端点spring-doc.cadn.net.cn

-spring-doc.cadn.net.cn

连接。[name].sse-端点spring-doc.cadn.net.cn

连接时使用的SSE端点(作为URL后缀)spring-doc.cadn.net.cn

/ssespring-doc.cadn.net.cn

示例配置:spring-doc.cadn.net.cn

spring:
  ai:
    mcp:
      client:
        sse:
          connections:
            # Simple configuration using default /sse endpoint
            server1:
              url: http://localhost:8080
            # Custom SSE endpoint
            server2:
              url: http://otherserver:8081
              sse-endpoint: /custom-sse
            # Complex URL with path and token (like MCP Hub)
            mcp-hub:
              url: http://localhost:3000
              sse-endpoint: /mcp-hub/sse/cf9ec4527e3c4a2cbb149a85ea45ab01
            # SSE endpoint with query parameters
            api-server:
              url: https://api.example.com
              sse-endpoint: /v1/mcp/events?token=abc123&format=json

URL拆分指南

当你拥有完整的SSE URL后,将其拆分为基础URL和端点路径:spring-doc.cadn.net.cn

完整网址 配置

http://localhost:3000/mcp-hub/sse/token123spring-doc.cadn.net.cn

网址:localhost:3000
SSE-endpoint: /mcp-hub/sse/token123spring-doc.cadn.net.cn

https://api.service.com/v2/events?key=secretspring-doc.cadn.net.cn

网址:api.service.com
SSE-endpoint: /v2/events?key=secretspring-doc.cadn.net.cn

http://localhost:8080/ssespring-doc.cadn.net.cn

网址:localhost:8080
SSE-端点: /sse(或因默认而省略)spring-doc.cadn.net.cn

排查SSE连接

404 未找到错误:spring-doc.cadn.net.cn

可流式 HTTP 传输属性

可流式Http传输的属性前缀为spring.ai.mcp.client.streamable-http:spring-doc.cadn.net.cn

属性 描述 默认值

连接spring-doc.cadn.net.cn

命名的可流式 Http 连接配置映射spring-doc.cadn.net.cn

-spring-doc.cadn.net.cn

连接。[name].urlspring-doc.cadn.net.cn

用于与MCP服务器进行Streamable-Http通信的基础URL端点spring-doc.cadn.net.cn

-spring-doc.cadn.net.cn

连接。[name].端点spring-doc.cadn.net.cn

用于连接的Streamable-HTTP端点(作为URL后缀)spring-doc.cadn.net.cn

/MCPspring-doc.cadn.net.cn

示例配置:spring-doc.cadn.net.cn

spring:
  ai:
    mcp:
      client:
        streamable-http:
          connections:
            server1:
              url: http://localhost:8080
            server2:
              url: http://otherserver:8081
              endpoint: /custom-sse

特征

同步/异步客户端类型

起始程序支持两种客户端:spring-doc.cadn.net.cn

  • 同步 - 默认客户端类型(spring.ai.mcp.client.type=SYNC),适用于传统的请求-响应模式,并带有阻塞作spring-doc.cadn.net.cn

注意:SYNC客户端只注册同步MCP注释方法。异步方法将被忽略。spring-doc.cadn.net.cn

  • 异步——适合具有非阻塞作的响应式应用,配置为spring.ai.mcp.client.type=ASYNCspring-doc.cadn.net.cn

注意:ASYNC客户端只会注册异步MCP注释方法。同步方法将被忽略。spring-doc.cadn.net.cn

客户端定制

自动配置通过回调接口提供广泛的客户规格定制功能。这些自定义工具允许你配置MCP客户端行为的各个方面,从请求超时到事件处理和消息处理。spring-doc.cadn.net.cn

定制类型

以下可定制选项:spring-doc.cadn.net.cn

客户端可以通过设置最低日志等级来控制日志冗长度spring-doc.cadn.net.cn

客户端定制示例

你可以实现其中任何一种McpSyncClientCustomizer对于同步客户端或McpAsyncClientCustomizer对于异步客户端,取决于你的应用需求。spring-doc.cadn.net.cn

@Component
public class CustomMcpSyncClientCustomizer implements McpSyncClientCustomizer {
    @Override
    public void customize(String serverConfigurationName, McpClient.SyncSpec spec) {

        // Customize the request timeout configuration
        spec.requestTimeout(Duration.ofSeconds(30));

        // Sets the root URIs that this client can access.
        spec.roots(roots);

        // Sets a custom sampling handler for processing message creation requests.
        spec.sampling((CreateMessageRequest messageRequest) -> {
            // Handle sampling
            CreateMessageResult result = ...
            return result;
        });

        // Sets a custom elicitation handler for processing elicitation requests.
        spec.elicitation((ElicitRequest request) -> {
          // handle elicitation
          return new ElicitResult(ElicitResult.Action.ACCEPT, Map.of("message", request.message()));
        });

        // Adds a consumer to be notified when progress notifications are received.
        spec.progressConsumer((ProgressNotification progress) -> {
         // Handle progress notifications
        });

        // Adds a consumer to be notified when the available tools change, such as tools
        // being added or removed.
        spec.toolsChangeConsumer((List<McpSchema.Tool> tools) -> {
            // Handle tools change
        });

        // Adds a consumer to be notified when the available resources change, such as resources
        // being added or removed.
        spec.resourcesChangeConsumer((List<McpSchema.Resource> resources) -> {
            // Handle resources change
        });

        // Adds a consumer to be notified when the available prompts change, such as prompts
        // being added or removed.
        spec.promptsChangeConsumer((List<McpSchema.Prompt> prompts) -> {
            // Handle prompts change
        });

        // Adds a consumer to be notified when logging messages are received from the server.
        spec.loggingConsumer((McpSchema.LoggingMessageNotification log) -> {
            // Handle log messages
        });
    }
}
@Component
public class CustomMcpAsyncClientCustomizer implements McpAsyncClientCustomizer {
    @Override
    public void customize(String serverConfigurationName, McpClient.AsyncSpec spec) {
        // Customize the async client configuration
        spec.requestTimeout(Duration.ofSeconds(30));
    }
}

serverConfigurationName参数是应用定制器的服务器配置名称,也是创建MCP客户端的对象。spring-doc.cadn.net.cn

MCP客户端自动配置会自动检测并应用应用上下文中的任何自定义器。spring-doc.cadn.net.cn

交通支持

自动配置支持多种传输类型:spring-doc.cadn.net.cn

  • 标准I/O(标准)(由Spring-AI-starter-mcp-clientSpring-AI-starter-mcp-client-webflux)spring-doc.cadn.net.cn

  • (Http客户端)HTTP/SSE 和 Streamable-HTTP(由Spring-AI-starter-mcp-client)spring-doc.cadn.net.cn

  • (网络流)HTTP/SSE 和 Streamable-HTTP(由Spring-AI-starter-mcp-client-webflux)spring-doc.cadn.net.cn

工具过滤

MCP 客户端启动Starters支持通过McpToolFilter接口。这允许你根据自定义条件(如MCP连接信息或工具属性)选择性地包含或排除工具。spring-doc.cadn.net.cn

要实现工具过滤,可以创建一个实现McpToolFilter接口:spring-doc.cadn.net.cn

@Component
public class CustomMcpToolFilter implements McpToolFilter {

    @Override
    public boolean test(McpConnectionInfo connectionInfo, McpSchema.Tool tool) {
        // Filter logic based on connection information and tool properties
        // Return true to include the tool, false to exclude it

        // Example: Exclude tools from a specific client
        if (connectionInfo.clientInfo().name().equals("restricted-client")) {
            return false;
        }

        // Example: Only include tools with specific names
        if (tool.name().startsWith("allowed_")) {
            return true;
        }

        // Example: Filter based on tool description or other properties
        if (tool.description() != null &&
            tool.description().contains("experimental")) {
            return false;
        }

        return true; // Include all other tools by default
    }
}

McpConnectionInfoRecord提供以下访问权限:spring-doc.cadn.net.cn

该过滤器会自动检测并应用于同步和异步MCP工具回调提供者。 如果没有提供自定义过滤器,所有发现的工具默认都会包含在内。spring-doc.cadn.net.cn

注:只有一个McpToolFilterBEAN 应在应用上下文中定义。 如果需要多个Filter,则将它们合并为单一的复合Filter实现。spring-doc.cadn.net.cn

工具名称前缀生成

MCP 客户端启动Starters支持通过McpToolNamePrefixGenerator接口。此功能通过在工具名称中添加独特的前缀,有助于避免在整合多个 MCP 服务器工具时出现命名冲突。spring-doc.cadn.net.cn

默认情况下,如果没有自定义McpToolNamePrefixGenerator豆子是提供的,发酵者使用DefaultMcpToolNamePrefixGenerator这确保了所有MCP客户端连接中工具名称的唯一。默认生成器:spring-doc.cadn.net.cn

  • 跟踪所有现有连接和工具名称,确保唯一性spring-doc.cadn.net.cn

  • 格式化工具名称通过用下划线替换非字母数字字符(例如,我的工具成为my_tool)spring-doc.cadn.net.cn

  • 当不同连接中检测到重复工具名称时,会添加计数器前缀(例如,alt_1_toolName,alt_2_toolName)spring-doc.cadn.net.cn

  • 它是线程安全的,并且保持幂等性——客户端、服务器、工具的相同组合总是获得相同的唯一名称spring-doc.cadn.net.cn

  • 确保最终名字不超过64个字符(如有需要,从开头截断)spring-doc.cadn.net.cn

例如: * 工具的首次出现搜索搜索* 工具的第二次出现搜索来自不同的连接→alt_1_search* 带有特殊字符的工具我的——特制工具my_special_toolspring-doc.cadn.net.cn

您可以通过提供自己的实现来自定义这种行为:spring-doc.cadn.net.cn

@Component
public class CustomToolNamePrefixGenerator implements McpToolNamePrefixGenerator {

    @Override
    public String prefixedToolName(McpConnectionInfo connectionInfo, Tool tool) {
        // Custom logic to generate prefixed tool names

        // Example: Use server name and version as prefix
        String serverName = connectionInfo.initializeResult().serverInfo().name();
        String serverVersion = connectionInfo.initializeResult().serverInfo().version();
        return serverName + "_v" + serverVersion.replace(".", "_") + "_" + tool.name();
    }
}

McpConnectionInfo记录提供了关于MCP连接的全面信息:spring-doc.cadn.net.cn

内置前缀生成器

该框架提供了若干内置前缀生成器:spring-doc.cadn.net.cn

  • DefaultMcpToolNamePrefixGenerator- 通过追踪重复工具并在需要时添加计数器前缀(如果没有自定义豆子,默认使用)确保唯一工具名称spring-doc.cadn.net.cn

  • McpToolNamePrefixGenerator.noPrefix()- 返回工具名称,不带任何前缀(如果多个服务器提供相同名称的工具,可能会引起冲突)spring-doc.cadn.net.cn

要完全禁用前缀并使用原始工具名(如果使用多个MCP服务器,不建议这样做),请将无前缀生成器注册为豆子:spring-doc.cadn.net.cn

@Configuration
public class McpConfiguration {

    @Bean
    public McpToolNamePrefixGenerator mcpToolNamePrefixGenerator() {
        return McpToolNamePrefixGenerator.noPrefix();
    }
}

前缀生成器通过 Spring 的对象提供者机制。 如果没有提供自定义生成器豆,则DefaultMcpToolNamePrefixGenerator自动使用。spring-doc.cadn.net.cn

使用McpToolNamePrefixGenerator.noPrefix()在多个MCP服务器中,重复的工具名称会导致非法州例外.默认DefaultMcpToolNamePrefixGenerator通过自动为重复工具名称添加唯一前缀来防止这种情况。

工具上下文到 MCP 元转换器

MCP 客户端启动Starters支持通过工具上下文ToMcp元转换器接口。 该功能允许您将额外的上下文信息(如用户ID、秘密Tokens)作为元数据传递给LLM生成的调用参数。spring-doc.cadn.net.cn

比如你可以通过MCPprogressToken(进步Tokens)在工具上下文中,将 MCP 进度流用于跟踪长期运行作的进展:spring-doc.cadn.net.cn

ChatModel chatModel = ...

String response = ChatClient.create(chatModel)
        .prompt("Tell me more about the customer with ID 42")
        .toolContext(Map.of("progressToken", "my-progress-token"))
        .call()
        .content();

默认情况下,如果没有提供自定义转换器豆,起始器会使用工具上下文ToMcpMetaConverter.defaultConverter()哪:spring-doc.cadn.net.cn

您可以通过提供自己的实现来自定义这种行为:spring-doc.cadn.net.cn

@Component
public class CustomToolContextToMcpMetaConverter implements ToolContextToMcpMetaConverter {

    @Override
    public Map<String, Object> convert(ToolContext toolContext) {
        if (toolContext == null || toolContext.getContext() == null) {
            return Map.of();
        }

        // Custom logic to convert tool context to MCP metadata
        Map<String, Object> metadata = new HashMap<>();

        // Example: Add custom prefix to all keys
        for (Map.Entry<String, Object> entry : toolContext.getContext().entrySet()) {
            if (entry.getValue() != null) {
                metadata.put("app_" + entry.getKey(), entry.getValue());
            }
        }

        // Example: Add additional metadata
        metadata.put("timestamp", System.currentTimeMillis());
        metadata.put("source", "spring-ai");

        return metadata;
    }
}

内置转换器

该框架内置了转换器:spring-doc.cadn.net.cn

  • 工具上下文ToMcpMetaConverter.defaultConverter()- 过滤MCP交换密钥和空值(如果没有提供自定义豆,默认使用)spring-doc.cadn.net.cn

  • ToolContextToMcpMetaConverter.noOp()- 返回空映射,有效禁用上下文到元数据的转换spring-doc.cadn.net.cn

要完全禁用上下文转元数据转换:spring-doc.cadn.net.cn

@Configuration
public class McpConfiguration {

    @Bean
    public ToolContextToMcpMetaConverter toolContextToMcpMetaConverter() {
        return ToolContextToMcpMetaConverter.noOp();
    }
}

转换器通过Spring的同步和异步MCP工具回调自动检测并应用对象提供者机制。 如果没有提供自定义转换器,默认转换器会自动使用。spring-doc.cadn.net.cn

禁用MCP工具回调自动配置

MCP ToolCallback 自动配置默认启用,但可以通过以下功能禁用spring.ai.mcp.client.toolcallback.enabled=false财产。spring-doc.cadn.net.cn

禁用时,不行ToolCallbackProviderBEAN 是通过现有的 MCP 工具创建的。spring-doc.cadn.net.cn

MCP 客户端注释

MCP 客户端启动Starters自动检测并注册处理各种 MCP 客户端作的注释方法:spring-doc.cadn.net.cn

@Component
public class McpClientHandlers {

    @McpLogging(clients = "server1")
    public void handleLoggingMessage(LoggingMessageNotification notification) {
        System.out.println("Received log: " + notification.level() +
                          " - " + notification.data());
    }

    @McpSampling(clients = "server1")
    public CreateMessageResult handleSamplingRequest(CreateMessageRequest request) {
        // Process the request and generate a response
        String response = generateLLMResponse(request);

        return CreateMessageResult.builder()
            .role(Role.ASSISTANT)
            .content(new TextContent(response))
            .model("gpt-4")
            .build();
    }

    @McpProgress(clients = "server1")
    public void handleProgressNotification(ProgressNotification notification) {
        double percentage = notification.progress() * 100;
        System.out.println(String.format("Progress: %.2f%% - %s",
            percentage, notification.message()));
    }

    @McpToolListChanged(clients = "server1")
    public void handleToolListChanged(List<McpSchema.Tool> updatedTools) {
        System.out.println("Tool list updated: " + updatedTools.size() + " tools available");
        // Update local tool registry
        toolRegistry.updateTools(updatedTools);
    }
}

注释支持同步和异步实现,并可针对特定客户端配置客户端参数:spring-doc.cadn.net.cn

@McpLogging(clients = "server1")
public void handleServer1Logs(LoggingMessageNotification notification) {
    // Handle logs from specific server
    logToFile("server1.log", notification);
}

@McpSampling(clients = "server1")
public Mono<CreateMessageResult> handleAsyncSampling(CreateMessageRequest request) {
    return Mono.fromCallable(() -> {
        String response = generateLLMResponse(request);
        return CreateMessageResult.builder()
            .role(Role.ASSISTANT)
            .content(new TextContent(response))
            .model("gpt-4")
            .build();
    }).subscribeOn(Schedulers.boundedElastic());
}

有关所有可用注释及其使用模式的详细信息,请参见MCP客户端注释文档。spring-doc.cadn.net.cn

使用示例

给你的项目添加合适的起始依赖,并配置客户端application.propertiesapplication.yml:spring-doc.cadn.net.cn

spring:
  ai:
    mcp:
      client:
        enabled: true
        name: my-mcp-client
        version: 1.0.0
        request-timeout: 30s
        type: SYNC  # or ASYNC for reactive applications
        sse:
          connections:
            server1:
              url: http://localhost:8080
            server2:
              url: http://otherserver:8081
        streamable-http:
          connections:
            server3:
              url: http://localhost:8083
              endpoint: /mcp
        stdio:
          root-change-notification: false
          connections:
            server1:
              command: /path/to/server
              args:
                - --port=8080
                - --mode=production
              env:
                API_KEY: your-api-key
                DEBUG: "true"

MCP客户端豆子将自动配置并可供注入:spring-doc.cadn.net.cn

@Autowired
private List<McpSyncClient> mcpSyncClients;  // For sync client

// OR

@Autowired
private List<McpAsyncClient> mcpAsyncClients;  // For async client

当工具回调启用(默认行为)时,所有MCP客户端注册的MCP工具将作为ToolCallbackProvider实例:spring-doc.cadn.net.cn

@Autowired
private SyncMcpToolCallbackProvider toolCallbackProvider;
ToolCallback[] toolCallbacks = toolCallbackProvider.getToolCallbacks();

应用示例