Skip to content

Commit 0c9dc0d

Browse files
authored
Update AsyncMcpToolCallbackProvider.java
Signed-off-by: Sizhe Fan <[email protected]>
1 parent d2a443f commit 0c9dc0d

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

mcp/common/src/main/java/org/springframework/ai/mcp/AsyncMcpToolCallbackProvider.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818

1919
import java.util.ArrayList;
2020
import java.util.List;
21+
import java.util.Set;
2122
import java.util.function.BiPredicate;
23+
import java.util.stream.Collectors;
2224

2325
import io.modelcontextprotocol.client.McpAsyncClient;
2426
import io.modelcontextprotocol.spec.McpSchema.Tool;
@@ -122,6 +124,34 @@ public AsyncMcpToolCallbackProvider(McpAsyncClient... mcpClients) {
122124
this(List.of(mcpClients));
123125
}
124126

127+
/**
128+
* Creates a new {@code AsyncMcpToolCallbackProvider} instance that includes only
129+
* clients from the specified allowed servers.
130+
* <p>
131+
* This constructor:
132+
* <ol>
133+
* <li>Filters the provided MCP clients to only those matching allowed server
134+
* names</li>
135+
* <li>Retains all tools from the selected clients (no additional tool filtering)</li>
136+
* <li>Ensures no null parameters are passed</li>
137+
* <li>Maintains full asynchronous operation capability</li>
138+
* </ol>
139+
* @param mcpClients complete list of available MCP async clients
140+
* @param allowedServerNames set of server names to include (case-sensitive)
141+
* @throws IllegalArgumentException if parameters are null or empty
142+
* @since 1.1.0
143+
*/
144+
public AsyncMcpToolCallbackProvider(List<McpAsyncClient> mcpClients, Set<String> allowedServerNames) {
145+
Assert.notNull(mcpClients, "MCP clients list must not be null");
146+
Assert.notNull(allowedServerNames, "Allowed server names set must not be null");
147+
Assert.notEmpty(allowedServerNames, "Allowed server names set must not be empty");
148+
149+
this.mcpClients = mcpClients.stream()
150+
.filter(client -> allowedServerNames.contains(client.getServerInfo().name()))
151+
.collect(Collectors.toList());
152+
this.toolFilter = (client, tool) -> true;
153+
}
154+
125155
/**
126156
* Discovers and returns all available tools from the configured MCP servers.
127157
* <p>

0 commit comments

Comments
 (0)