|
17 | 17 | package org.springframework.ai.mcp; |
18 | 18 |
|
19 | 19 | import java.util.List; |
| 20 | +import java.util.Set; |
20 | 21 | import java.util.function.BiPredicate; |
21 | 22 |
|
22 | 23 | import io.modelcontextprotocol.client.McpSyncClient; |
@@ -115,6 +116,33 @@ public SyncMcpToolCallbackProvider(McpSyncClient... mcpClients) { |
115 | 116 | this(List.of(mcpClients)); |
116 | 117 | } |
117 | 118 |
|
| 119 | + /** |
| 120 | + * Creates a new {@code SyncMcpToolCallbackProvider} instance that includes only |
| 121 | + * clients from the specified allowed servers. |
| 122 | + * <p> |
| 123 | + * This constructor: |
| 124 | + * <ol> |
| 125 | + * <li>Filters the provided MCP clients to only those matching allowed server |
| 126 | + * names</li> |
| 127 | + * <li>Retains all tools from the selected clients (no additional tool filtering)</li> |
| 128 | + * <li>Ensures no null parameters are passed</li> |
| 129 | + * </ol> |
| 130 | + * @param mcpClients complete list of available MCP clients |
| 131 | + * @param allowedServerNames set of server names to include (case-sensitive) |
| 132 | + * @throws IllegalArgumentException if parameters are null or empty |
| 133 | + * @since 1.1.0 |
| 134 | + */ |
| 135 | + public SyncMcpToolCallbackProvider(List<McpSyncClient> mcpClients, Set<String> allowedServerNames) { |
| 136 | + Assert.notNull(mcpClients, "MCP clients list must not be null"); |
| 137 | + Assert.notNull(allowedServerNames, "Allowed server names set must not be null"); |
| 138 | + Assert.notEmpty(allowedServerNames, "Allowed server names set must not be empty"); |
| 139 | + |
| 140 | + this.mcpClients = mcpClients.stream() |
| 141 | + .filter(client -> allowedServerNames.contains(client.getServerInfo().name())) |
| 142 | + .toList(); |
| 143 | + this.toolFilter = (client, tool) -> true; // No additional filtering |
| 144 | + } |
| 145 | + |
118 | 146 | /** |
119 | 147 | * Discovers and returns all available tools from all connected MCP servers. |
120 | 148 | * <p> |
|
0 commit comments