MCP客户端注解
MCP客户端引导启动器会自动检测并注册带注释的方法,以处理各种MCP客户端操作:
@McpLogging - 处理来自MCP服务器的日志消息通知
@McpSampling - 处理来自MCP服务器的采样请求,以完成LLM(大型语言模型)任务
@McpElicitation - 处理启发请求,以从用户那里收集更多信息
@McpProgress - 处理长时间运行操作的进度通知
@McpToolListChanged - 当服务器工具列表发生变化时,处理相关通知
@McpResourceListChanged - 处理服务器资源列表发生更改时的通知
@McpPromptListChanged - 处理服务器提示列表发生更改时的通知
示例用法:
@Componentpublic 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); }}
这些注解既支持同步实现,也支持异步实现,并且可以通过clients参数为特定客户端进行配置:
@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());}
用法示例
将适当的 starter 依赖项添加到您的项目中,并在 application.properties 或 application.yml 中配置客户端:
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客户端Bean将自动配置并可用于注入:
@Autowiredprivate List<McpSyncClient> mcpSyncClients; // For sync client// OR@Autowiredprivate List<McpAsyncClient> mcpAsyncClients; // For async client
当启用工具回调(默认行为)时,所有MCP客户端注册的MCP工具都将作为ToolCallbackProvider实例提供:
@Autowiredprivate SyncMcpToolCallbackProvider toolCallbackProvider;ToolCallback[] toolCallbacks = toolCallbackProvider.getToolCallbacks();
示例应用
Brave Web Search Chatbot - 一种使用模型上下文协议与网络搜索服务器进行交互的聊天机器人。https://github.com/spring-projects/spring-ai-examples/tree/main/model-context-protocol/web-search/brave-chatbot
Default MCP Client Starter - 使用默认的spring-ai-starter-mcp-client MCP客户端引导启动器的简单示例。https://github.com/spring-projects/spring-ai-examples/tree/main/model-context-protocol/client-starter/starter-default-client
WebFlux MCP Client Starter - 使用spring-ai-starter-mcp-client-webflux MCP客户端引导启动器的简单示例。https://github.com/spring-projects/spring-ai-examples/tree/main/model-context-protocol/client-starter/starter-webflux-client