Skip to content

Commit d2a443f

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

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.ai.mcp;
1818

1919
import java.util.List;
20+
import java.util.Set;
2021
import java.util.function.BiPredicate;
2122

2223
import io.modelcontextprotocol.client.McpSyncClient;
@@ -115,6 +116,33 @@ public SyncMcpToolCallbackProvider(McpSyncClient... mcpClients) {
115116
this(List.of(mcpClients));
116117
}
117118

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+
118146
/**
119147
* Discovers and returns all available tools from all connected MCP servers.
120148
* <p>

0 commit comments

Comments
 (0)