特性
同步/异步客户端类型
启动器支持两种类型的客户端:
注意:SYNC客户端将仅注册带有同步MCP注释的方法。异步方法将被忽略。
注意:ASYNC客户端将仅注册带有异步MCP注释的方法。同步方法将被忽略。
客户定制
自动配置通过回调接口提供了广泛的客户端规范定制功能。这些定制器允许您配置MCP客户端行为的各种方面,从请求超时到事件处理和消息处理。
定制类型
以下为可用的自定义选项:
请求配置 - 设置自定义请求超时
自定义采样处理程序 - 服务器通过客户端向大型语言模型(LLM)请求LLM采样的标准化方式(补全或生成)。此流程允许客户端保持对模型访问、选择和权限的控制,同时使服务器能够利用AI功能,无需服务器API密钥。
文件系统(根目录)访问 - 客户端向服务器公开文件系统根目录的标准化方式。根目录定义了服务器在文件系统内可操作的边界,使服务器能够了解其有权访问的目录和文件。服务器可以从支持该功能的客户端请求根目录列表,并在列表发生变化时接收通知。
启发式处理程序 - 一种标准化的方式,供服务器在交互过程中通过客户端向用户请求额外信息。
事件处理程序 - 当特定服务器事件发生时,客户端的处理程序会收到通知:
工具变更通知 - 当可用服务器工具列表发生变化时
资源变更通知 - 当可用服务器资源列表发生变化时。
提示更改通知 - 当可用服务器提示列表发生变化时。
日志处理程序 - 服务器向客户端发送结构化日志消息的标准化方式。
进度处理程序 - 服务器向客户端发送结构化进度消息的标准化方式。
客户端可以通过设置最低日志级别来控制日志记录的详细程度。
客户定制示例
根据应用程序的需求,您可以为同步客户端实现McpSyncClientCustomizer,或为异步客户端实现McpAsyncClientCustomizer。
@Componentpublic 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 }); }}
Async
@Componentpublic 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客户端的服务器配置的名称。
MCP客户端自动配置功能会自动检测并应用在应用程序上下文中找到的任何自定义项。
传输支持
自动配置支持多种传输类型:
标准输入/输出(Stdio)(由spring-ai-starter-mcp-client和spring-ai-starter-mcp-client-webflux激活)
(HttpClient)HTTP/SSE和可流式HTTP(由spring-ai-starter-mcp-client激活)
(WebFlux) HTTP/SSE和可流式HTTP(由spring-ai-starter-mcp-client-webflux激活)
工具过滤
MCP客户端启动器支持通过McpToolFilter接口对发现的工具进行过滤。这允许您根据自定义条件(如MCP连接信息或工具属性)有选择性地包含或排除工具。
要实现工具过滤,请创建一个实现McpToolFilter接口的Bean:
@Componentpublic 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 }}
McpConnectionInfo 记录提供对以下内容的访问:
clientCapabilities - MCP客户端的能力
clientInfo - 关于MCP客户端的信息(名称和版本)
initializeResult - 来自MCP服务器的初始化结果
该过滤器会自动检测并应用于同步和异步的MCP工具回调提供程序。若未提供自定义过滤器,则默认情况下会包含所有发现的工具。
注意:在应用程序上下文中,应仅定义一个McpToolFilter bean。如果需要多个过滤器,请将它们合并为一个复合过滤器实现。