2222import com .google .adk .tools .BaseTool ;
2323import com .google .adk .tools .BaseToolset ;
2424import com .google .adk .tools .NamedToolPredicate ;
25+ import com .google .adk .tools .ToolPredicate ;
26+ import com .google .common .base .Preconditions ;
2527import com .google .common .collect .ImmutableList ;
2628import com .google .errorprone .annotations .CanIgnoreReturnValue ;
2729import io .modelcontextprotocol .client .McpAsyncClient ;
3234import java .time .Duration ;
3335import java .util .List ;
3436import java .util .Objects ;
35- import java .util .Optional ;
3637import java .util .concurrent .atomic .AtomicReference ;
38+ import javax .annotation .Nullable ;
3739import org .slf4j .Logger ;
3840import org .slf4j .LoggerFactory ;
3941import reactor .core .publisher .Mono ;
@@ -59,14 +61,14 @@ public class McpAsyncToolset implements BaseToolset {
5961
6062 private final McpSessionManager mcpSessionManager ;
6163 private final ObjectMapper objectMapper ;
62- private final Optional < Object > toolFilter ;
64+ private final @ Nullable Object toolFilter ;
6365 private final AtomicReference <Mono <List <McpAsyncTool >>> mcpTools = new AtomicReference <>();
6466
6567 /** Builder for McpAsyncToolset */
6668 public static class Builder {
6769 private Object connectionParams = null ;
6870 private ObjectMapper objectMapper = null ;
69- private Optional < Object > toolFilter = null ;
71+ private @ Nullable Object toolFilter = null ;
7072
7173 @ CanIgnoreReturnValue
7274 public Builder connectionParams (ServerParameters connectionParams ) {
@@ -87,14 +89,14 @@ public Builder objectMapper(ObjectMapper objectMapper) {
8789 }
8890
8991 @ CanIgnoreReturnValue
90- public Builder toolFilter (Optional < Object > toolFilter ) {
91- this .toolFilter = toolFilter ;
92+ public Builder toolFilter (List < String > toolNames ) {
93+ this .toolFilter = new NamedToolPredicate ( Preconditions . checkNotNull ( toolNames )) ;
9294 return this ;
9395 }
9496
9597 @ CanIgnoreReturnValue
96- public Builder toolFilter (List < String > toolNames ) {
97- this .toolFilter = Optional . of ( new NamedToolPredicate ( toolNames )) ;
98+ public Builder toolFilter (@ Nullable ToolPredicate toolPredicate ) {
99+ this .toolFilter = toolPredicate ;
98100 return this ;
99101 }
100102
@@ -118,12 +120,12 @@ public McpAsyncToolset build() {
118120 *
119121 * @param connectionParams The SSE connection parameters to the MCP server.
120122 * @param objectMapper An ObjectMapper instance for parsing schemas.
121- * @param toolFilter An Optional containing either a ToolPredicate or a List of tool names.
123+ * @param toolFilter Either a ToolPredicate or a List of tool names.
122124 */
123- public McpAsyncToolset (
125+ McpAsyncToolset (
124126 SseServerParameters connectionParams ,
125127 ObjectMapper objectMapper ,
126- Optional < Object > toolFilter ) {
128+ @ Nullable Object toolFilter ) {
127129 Objects .requireNonNull (connectionParams );
128130 Objects .requireNonNull (objectMapper );
129131 this .objectMapper = objectMapper ;
@@ -136,41 +138,25 @@ public McpAsyncToolset(
136138 *
137139 * @param connectionParams The local server connection parameters to the MCP server.
138140 * @param objectMapper An ObjectMapper instance for parsing schemas.
139- * @param toolFilter An Optional containing either a ToolPredicate or a List of tool names.
141+ * @param toolFilter Either a ToolPredicate or a List of tool names or null .
140142 */
141- public McpAsyncToolset (
142- ServerParameters connectionParams , ObjectMapper objectMapper , Optional < Object > toolFilter ) {
143+ McpAsyncToolset (
144+ ServerParameters connectionParams , ObjectMapper objectMapper , @ Nullable Object toolFilter ) {
143145 Objects .requireNonNull (connectionParams );
144146 Objects .requireNonNull (objectMapper );
145147 this .objectMapper = objectMapper ;
146148 this .mcpSessionManager = new McpSessionManager (connectionParams );
147149 this .toolFilter = toolFilter ;
148150 }
149151
150- /**
151- * Initializes the McpAsyncToolset with a provided McpSessionManager.
152- *
153- * @param mcpSessionManager The session manager for MCP connections.
154- * @param objectMapper An ObjectMapper instance for parsing schemas.
155- * @param toolFilter An Optional containing either a ToolPredicate or a List of tool names.
156- */
157- public McpAsyncToolset (
158- McpSessionManager mcpSessionManager , ObjectMapper objectMapper , Optional <Object > toolFilter ) {
159- Objects .requireNonNull (mcpSessionManager );
160- Objects .requireNonNull (objectMapper );
161- this .objectMapper = objectMapper ;
162- this .mcpSessionManager = mcpSessionManager ;
163- this .toolFilter = toolFilter ;
164- }
165-
166152 @ Override
167153 public Flowable <BaseTool > getTools (ReadonlyContext readonlyContext ) {
168154 return Maybe .defer (() -> Maybe .fromCompletionStage (this .initAndGetTools ().toFuture ()))
169155 .defaultIfEmpty (ImmutableList .of ())
170156 .map (
171157 tools ->
172158 tools .stream ()
173- .filter (tool -> isToolSelected (tool , toolFilter . orElse ( null ) , readonlyContext ))
159+ .filter (tool -> isToolSelected (tool , toolFilter , readonlyContext ))
174160 .toList ())
175161 .onErrorResumeNext (
176162 err -> {
0 commit comments