|
40 | 40 | import org.springframework.ai.mcp.spec.McpSchema.PaginatedRequest; |
41 | 41 | import org.springframework.ai.mcp.spec.McpSchema.Root; |
42 | 42 | import org.springframework.ai.mcp.spec.McpTransport; |
| 43 | +import org.springframework.ai.mcp.util.Utils; |
43 | 44 |
|
44 | 45 | /** |
45 | 46 | * The Model Context Protocol (MCP) client implementation that provides asynchronous |
@@ -85,29 +86,44 @@ public McpAsyncClient(McpTransport transport, Duration requestTimeout, |
85 | 86 | List<Consumer<List<McpSchema.Resource>>> resourcesChangeConsumers, |
86 | 87 | List<Consumer<List<McpSchema.Prompt>>> promptsChangeConsumers) { |
87 | 88 |
|
| 89 | + // Request Handlers |
88 | 90 | Map<String, RequestHandler> requestHanlers = new HashMap<>(); |
89 | 91 |
|
| 92 | + // Roots List Request Handler |
90 | 93 | if (rootsListProviders != null && !rootsListProviders.isEmpty()) { |
91 | 94 | requestHanlers.put("roots/list", rootsListRequestHandler(rootsListProviders)); |
92 | 95 | this.rootCapabilities = new McpSchema.ClientCapabilities.RootCapabilities(rootsListChangedNotification); |
93 | 96 | } |
94 | 97 |
|
| 98 | + // Notification Handlers |
95 | 99 | Map<String, NotificationHandler> notificationHandlers = new HashMap<>(); |
96 | 100 |
|
97 | | - if (toolsChangeConsumers != null && !toolsChangeConsumers.isEmpty()) { |
98 | | - notificationHandlers.put("notifications/tools/list_changed", |
99 | | - toolsChangeNotificationHandler(toolsChangeConsumers)); |
| 101 | + // Tools Change Notification |
| 102 | + List<Consumer<List<McpSchema.Tool>>> toolsChangeConsumersFinal = new ArrayList<>(); |
| 103 | + toolsChangeConsumersFinal.add((notification) -> logger.info("Tools changed: {}", notification)); |
| 104 | + if (!Utils.isEmpty(toolsChangeConsumers)) { |
| 105 | + toolsChangeConsumersFinal.addAll(toolsChangeConsumers); |
100 | 106 | } |
101 | | - |
102 | | - if (resourcesChangeConsumers != null && !resourcesChangeConsumers.isEmpty()) { |
103 | | - notificationHandlers.put("notifications/resources/list_changed", |
104 | | - resourcesChangeNotificationHandler(resourcesChangeConsumers)); |
| 107 | + notificationHandlers.put("notifications/tools/list_changed", |
| 108 | + toolsChangeNotificationHandler(toolsChangeConsumersFinal)); |
| 109 | + |
| 110 | + // Resources Change Notification |
| 111 | + List<Consumer<List<McpSchema.Resource>>> resourcesChangeConsumersFinal = new ArrayList<>(); |
| 112 | + resourcesChangeConsumersFinal.add((notification) -> logger.info("Resources changed: {}", notification)); |
| 113 | + if (!Utils.isEmpty(resourcesChangeConsumers)) { |
| 114 | + resourcesChangeConsumersFinal.addAll(resourcesChangeConsumers); |
105 | 115 | } |
106 | | - |
107 | | - if (promptsChangeConsumers != null && !promptsChangeConsumers.isEmpty()) { |
108 | | - notificationHandlers.put("notifications/prompts/list_changed", |
109 | | - promptsChangeNotificationHandler(promptsChangeConsumers)); |
| 116 | + notificationHandlers.put("notifications/resources/list_changed", |
| 117 | + resourcesChangeNotificationHandler(resourcesChangeConsumersFinal)); |
| 118 | + |
| 119 | + // Prompts Change Notification |
| 120 | + List<Consumer<List<McpSchema.Prompt>>> promptsChangeConsumersFinal = new ArrayList<>(); |
| 121 | + promptsChangeConsumersFinal.add((notification) -> logger.info("Prompts changed: {}", notification)); |
| 122 | + if (!Utils.isEmpty(promptsChangeConsumers)) { |
| 123 | + promptsChangeConsumersFinal.addAll(promptsChangeConsumers); |
110 | 124 | } |
| 125 | + notificationHandlers.put("notifications/prompts/list_changed", |
| 126 | + promptsChangeNotificationHandler(promptsChangeConsumersFinal)); |
111 | 127 |
|
112 | 128 | this.mcpSession = new DefaultMcpSession(requestTimeout, transport, requestHanlers, notificationHandlers); |
113 | 129 |
|
|
0 commit comments