Add Kotlin snippet for filtering a toolset's tools - #2114
Conversation
The Toolsets section had no Kotlin. ToolFilter and ToolPredicate arrived in
adk-kotlin 0.7.0 and give Kotlin something the other languages do not have: a
filter that receives the ReadonlyContext, so a toolset's tool list can depend on
session state or the current user.
The Python tab in the Simple Math Toolset example only gestures at this, in a
commented-out branch inside get_tools(). This snippet implements it, showing all
three states: no filter selects everything, allowList selects by name, and a
Predicate decides per invocation.
Placed in its own subsection rather than as a fourth tab on the Simple Math
Toolset example. That group is one worked example explained by five bullets --
an agent, a greet tool, name prefixing, a tool_context.state write and close() --
and a filtering snippet satisfies none of them. Kotlin cannot satisfy the prefix
bullet at all, since BaseTool.name is a val and adk-kotlin has no prefix
mechanism. A tab there would have left readers with four bullets that do not
describe the code above them.
Transcluded, so CI compiles and lints it. Verified beyond compiling: the tools
are generated by KSP, filtering returns the expected sets for all three cases,
and the tool bodies run -- addNumbers(7,3) -> {result=10}.
✅ Deploy Preview for adk-docs-preview ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
wikaaaaa
left a comment
There was a problem hiding this comment.
Thanks, I checked the snippet against adk-kotlin v0.7.0 and the code is correct.
Just a comment to the PRs justification -- "gives Kotlin a capability the other languages don't have" isn't accurate. Context-aware tool filtering exists in other languages, ex. Python
Out of scope for this PR, but none of the other snippets in this file include Kotlin — should we add them?
… to ADK documentation
|
Thanks @wikaaaaa — both points addressed, and you were right on the first one. On "a capability the other languages don't have"Wrong, and I've corrected it. Python's More importantly, that claim had leaked into the page, not just the description. A single-language badge asserts exclusivity in this repo — the Python-only precedent I cited in the description ( Fixed by scoping the heading to the API rather than the concept: "Filtering which tools a toolset exposes with On the other snippets — done, all of themThe page had six language tab groups and not one had Kotlin. All six do now:
All transcluded, so CI compiles and lints them. Also fixed the These are execution-verified, not just compiled. I wired a temporary harness with a real
What I deliberately left out
Heads up that this outgrew the PR title, which I'll update along with the description. Happy to split the five backfilled snippets into their own PR if you'd rather review them separately from the |
…ilter # Conflicts: # tools/kotlin-snippets/files_to_test.txt
Summary
The Toolsets section of
docs/tools-custom/index.mdhad no Kotlin content.ToolFilterandToolPredicatelanded in adk-kotlin 0.7.0 and give Kotlin acapability the other languages don't have: a filter that receives the
ReadonlyContext, so a toolset's tool list can depend on session state or thecurrent user.
Purely additive — 15 docs lines, one new transcluded
.kt, one registration line.Why it's a new subsection rather than a fourth tab
The obvious home was the existing Simple Math Toolset tab group. It isn't the
right one. That group is a single worked example explained by five bullets — an
agent, a
greet_tool, name prefixing, atool_context.statewrite, andclose(). A filtering snippet satisfies none of them, and Kotlin cannotsatisfy the prefix bullet at all:
BaseTool.nameis avaland adk-kotlin has noprefix mechanism. A Kotlin tab there would have left readers with four bullets
that don't describe the code above them.
So it lives in
### Filtering which tools a toolset exposes, with prose thatmatches the code.
What the snippet adds over the other tabs
The Python tab gestures at context-aware selection but never implements it:
The Kotlin snippet implements exactly that, showing all three states: a null
filter selects everything,
ToolFilter.allowListselects by name, andToolFilter.Predicatedecides per invocation from theReadonlyContext.Notes for reviewers
deliberate — Python and TypeScript have no
ToolFilterequivalent, the conceptis Kotlin's. Single-language section badges are precedented
(
docs/agents/llm-agents.md:426-428carries a Python-only one). It will stillread as asymmetric; say the word if you'd rather it were framed differently.
documents:
McpToolsetConfig.toolFilterwentList<String>?(v0.6.0) →ToolFilter?(v0.7.0).future change.
## Toolsetsis untouched. It carries a staletitle="…available for Python and TypeScript"despite a Java tab existing, butthat predates this PR.
Verification
./gradlew compileKotlinclean; ktlint clean;verify_snippets.pyL0–L6 pass;check_kotlin_snippets.shpasses.expected sets, and the tool bodies execute:
Not exercised: no LLM call — the toolset was never attached to a live agent.