Skip to content

Commit 3b78220

Browse files
authored
Update McpToolUtils.java
By default, a SyncMcpToolCallbackProvider binds all available MCP clients. However, in many real-world scenarios (e.g. multi-tenant applications or region-specific deployments), users may want to dynamically include only a subset of servers by name. This method enables: Runtime filtering of MCP clients based on supplied server names Construction of a ToolCallbackProvider limited to permitted clients Cleaner usage without manually filtering the list each time Signed-off-by: Sizhe Fan <132420031+paoxiaomooo@users.noreply.github.com>
1 parent cf94951 commit 3b78220

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,5 +364,30 @@ public static List<ToolCallback> getToolCallbacksFromAsyncClients(List<McpAsyncC
364364
private record Base64Wrapper(@JsonAlias("mimetype") @Nullable MimeType mimeType, @JsonAlias({
365365
"base64", "b64", "imageData" }) @Nullable String data) {
366366
}
367+
/**
368+
* Constructs a {@link ToolCallbackProvider} that includes only the specified MCP clients.
369+
* <p>
370+
* This method filters the provided list of {@link McpSyncClient} instances by comparing
371+
* their server names against the given set. Only clients with names present in
372+
* {@code allowedServerNames} will be included in the resulting provider.
373+
*
374+
* @param mcpSyncClients the complete list of available MCP synchronous clients
375+
* @param allowedServerNames a set of server names that are permitted
376+
* @return a {@link ToolCallbackProvider} backed by only the allowed clients
377+
* @throws IllegalArgumentException if either parameter is null
378+
*/
379+
public static ToolCallbackProvider buildProviderByServerNames(
380+
List<McpSyncClient> mcpSyncClients,
381+
Set<String> allowedServerNames
382+
) {
383+
if (mcpSyncClients == null || allowedServerNames == null) {
384+
throw new IllegalArgumentException("Client list and allowedServerNames must not be null");
385+
}
386+
List<McpSyncClient> selected = mcpSyncClients.stream()
387+
.filter(c -> allowedServerNames.contains(c.getServerInfo().name()))
388+
.toList();
389+
return new SyncMcpToolCallbackProvider(selected);
390+
}
391+
367392

368393
}

0 commit comments

Comments
 (0)