feat(capabilities): adopt `ToolSelector` across caps + 4 new tool-scoping caps

DO
22d ago
+2120-203

Summary

Adopts ToolSelector consistently across tool-modifying capabilities and toolsets, and adds four new tool-scoping capabilities.

New capabilities (all spec-serializable when using non-callable selectors):

  • FilterTools(tools=...) — filters which tools the model sees.
  • DeferToolLoading(tools=...) — marks tools for deferred loading (companion to ToolSearch, useful for tools coming from external toolsets where you can't set Tool(defer_loading=True)).
  • RequireToolApproval(tools=..., handler=...) — single capability that both marks tools for approval AND optionally resolves approvals inline via a per-call handler. Handler is scoped to the capability's own tools selector, so multiple RequireToolApproval instances with different selectors/handlers compose cleanly (others' approvals pass through).
  • SetToolDescription(tools=..., replace=... | append=... | prepend=...) — surgically modifies tool descriptions. Spec-serializable; key use case is LLM auto-optimization loops mutating agent specs.

ToolSelector added to existing capabilities/toolsets (all backwards-compatible):

  • ToolSearch.tools — union with the per-tool defer_loading flag (additive; default None preserves existing behavior).
  • PrefixTools.tools — only prefix matching tools; also wrapped is now optional so it can work standalone via get_wrapper_toolset.
  • PrepareTools.tools / PrepareOutputTools.tools — restrict which tools the prepare function sees; others pass through.
  • Toolset wrappers: FilteredToolset, PrefixedToolset, PreparedToolset, DeferredLoadingToolset, ApprovalRequiredToolset, IncludeReturnSchemasToolset, SetMetadataToolset all gained a tools: ToolSelector parameter.
  • Convenience methods on AbstractToolset: .filtered(), .prefixed(), .prepared(), .approval_required(), .defer_loading(), .include_return_schemas(), .with_metadata() updated. New .with_description().
  • WrapperCapability.wrapped is now optional (None default) so subclasses can work standalone and just modify agent-wide tools via get_wrapper_toolset.
  • SetToolMetadata / with_metadata now accept an explicit metadata dict alongside **kwargs (fixes the case where metadata was a desired key name).

Backward compatibility:

  • FilteredToolset(wrapped, callable) still works (callables are valid ToolSelectors).
  • FilteredToolset(wrapped, filter_func=...) keyword form is deprecated.
  • DeferredLoadingToolset(wrapped, tool_names=...) is deprecated in favor of tools=....
  • ApprovalRequiredToolset keeps approval_required_func (not deprecated) for per-call args-aware refinement; tools is additive for tool-level scoping.

Test plan

  • pytest tests/test_capabilities.py tests/test_toolsets.py tests/test_tool_search.py tests/test_tools.py — 964 passed locally (50 deselected on env-only issues: google-genai not installed, anthropic SDK version mismatch, python binary off PATH).
  • 22 new tests covering: tools with name list / metadata dict / callable on every updated toolset and capability; deprecation warnings; PrefixTools standalone + with wrapped + with selector; RequireToolApproval with/without handler, denial, compose across multiple instances; SetToolDescription replace/append/prepend/validation; ToolSearch.tools union semantics + selector-only mode + backwards-compat with None default.
  • All snapshot tests regenerated and pass.

Checklist

  • Any AI generated code has been reviewed line-by-line by the human PR author, who stands by it.
  • No breaking changes in accordance with the version policy.
  • PR title is fit for the release changelog.