Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 20 additions & 12 deletions core/src/main/java/com/google/adk/tools/BaseToolset.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import io.reactivex.rxjava3.core.Flowable;
import java.util.List;
import java.util.Optional;
import javax.annotation.Nullable;

/** Base interface for toolsets. */
public interface BaseToolset extends AutoCloseable {
Expand All @@ -43,28 +44,35 @@ public interface BaseToolset extends AutoCloseable {
void close() throws Exception;

/**
* Helper method to be used by implementers that returns true if the given tool is in the provided
* list of tools of if testing against the given ToolPredicate returns true (otherwise false).
* Checks if a tool should be selected based on a filter.
*
* @param tool The tool to check.
* @param toolFilter An Optional containing either a ToolPredicate or a List of tool names.
* @param readonlyContext The current context.
* @return true if the tool is selected.
* @param toolFilter A ToolPredicate, a List of tool names, or null.
* @param readonlyContext The context for checking the tool, or null.
*/
default boolean isToolSelected(
BaseTool tool, Optional<Object> toolFilter, Optional<ReadonlyContext> readonlyContext) {
if (toolFilter.isEmpty()) {
BaseTool tool, @Nullable Object toolFilter, @Nullable ReadonlyContext readonlyContext) {
if (toolFilter == null) {
return true;
}
Object filter = toolFilter.get();
if (filter instanceof ToolPredicate toolPredicate) {

if (toolFilter instanceof ToolPredicate toolPredicate) {
return toolPredicate.test(tool, readonlyContext);
}
if (filter instanceof List) {
@SuppressWarnings("unchecked")
List<String> toolNames = (List<String>) filter;

if (toolFilter instanceof List<?> toolNames) {
return toolNames.contains(tool.name());
}

return false;
}

/**
* @deprecated Use {@link #isToolSelected(BaseTool, Object, ReadonlyContext)} instead.
*/
@Deprecated
default boolean isToolSelected(
BaseTool tool, Optional<Object> toolFilter, Optional<ReadonlyContext> readonlyContext) {
return isToolSelected(tool, toolFilter.orElse(null), readonlyContext.orElse(null));
}
}
14 changes: 14 additions & 0 deletions core/src/main/java/com/google/adk/tools/ToolPredicate.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.google.adk.agents.ReadonlyContext;
import java.util.Optional;
import javax.annotation.Nullable;

/**
* Functional interface to decide whether a tool should be exposed to the LLM based on the current
Expand All @@ -31,6 +32,19 @@ public interface ToolPredicate {
* @param tool The tool to check.
* @param readonlyContext The current context.
* @return true if the tool should be selected, false otherwise.
* @deprecated Use {@link #test(BaseTool, ReadonlyContext)} instead.
*/
@Deprecated
boolean test(BaseTool tool, Optional<ReadonlyContext> readonlyContext);

/**
* Decides if the given tool is selected.
*
* @param tool The tool to check.
* @param readonlyContext The current context.
* @return true if the tool should be selected, false otherwise.
*/
default boolean test(BaseTool tool, @Nullable ReadonlyContext readonlyContext) {
return test(tool, Optional.ofNullable(readonlyContext));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,7 @@ public Flowable<BaseTool> getTools(ReadonlyContext readonlyContext) {
.map(
tools ->
tools.stream()
.filter(
tool ->
isToolSelected(tool, toolFilter, Optional.ofNullable(readonlyContext)))
.filter(tool -> isToolSelected(tool, toolFilter.orElse(null), readonlyContext))
.toList())
.onErrorResumeNext(
err -> {
Expand Down
4 changes: 1 addition & 3 deletions core/src/main/java/com/google/adk/tools/mcp/McpToolset.java
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,7 @@ public Flowable<BaseTool> getTools(ReadonlyContext readonlyContext) {
new McpTool(
tool, this.mcpSession, this.mcpSessionManager, this.objectMapper))
.filter(
tool ->
isToolSelected(
tool, toolFilter, Optional.ofNullable(readonlyContext))));
tool -> isToolSelected(tool, toolFilter.orElse(null), readonlyContext)));
})
.retryWhen(
errorObservable ->
Expand Down