Add `ToolBridge`, which keeps the tool list fixed behind `tool_search`, `tool_describe` and `tool_call` so the prompt cache holds for large or changing tool sets

Merged
MP
17d ago
+1383-24

Adds ToolBridge to Pydantic AI Harness: a capability that replaces the agent's tools with three fixed dispatcher tools, tool_search(query), tool_describe(name) and tool_call(name, args_json). The tool definitions sent to the provider stay the same however many tools sit behind the bridge and however they change mid-run, so the cached prefix holds on every provider, including ones with no native deferral or reveal channel.

from pydantic_ai import Agent
from pydantic_ai_harness import ToolBridge

agent = Agent('anthropic:claude-opus-5-5', capabilities=[ToolBridge()])

How it works

  • ToolBridgeToolset wraps the agent's toolset. Tools the tools selector picks (default 'all') move behind the bridge, and the rest stay native.
  • tool_call runs the bridged tool through a nested ToolManager, the same path CodeMode uses for sandboxed calls. Argument validation, tool hooks, ModelRetry, HandleDeferredToolCalls, usage and instrumentation spans apply to it as to a direct call. An unknown name or invalid arguments ask the model to retry.
  • A bridged tool that raises ApprovalRequired or CallDeferred is resolved only by a HandleDeferredToolCalls handler, which sees the tool's own name. Without one, the run fails with a UserError that points to ToolBridge(tools=...), rather than deferring tool_call, so an approval of tool_call never covers whichever tool the model named. Tools under approval_required() therefore need to be kept native or paired with a handler.
  • Bridged tools with defer_loading=True can be found with tool_search, so ToolSearch has nothing left to reveal. tests/harness/test_history_append_only.py now has a ToolBridge scenario, and a test checks that the tool list stays the same while a dynamic toolset gains a tool mid-run.

Choices made (no maintainer input yet; please push back)

  • tool_call takes args_json: str, not a nested object. This follows the benchmark in the issue comments: with a free-form args: object, some Gemini models send null. An empty string is read as {}.
  • Some tools always stay native: tools that need approval or run externally (kind != 'function'), so approval requests name the real tool; tools with a tool_kind (framework control tools, read_file), which the run and capabilities such as ToolOutputLimits, Memory and compaction find by kind; unless_native fallbacks; and deferred tools that a capability owns, which core's capability loading reveals without moving the cached prefix. Tools that step aside for a kind (unless_tool_kind) also stay native, so core's filter still sees them.
  • Disclosure policy: every bridged tool can be searched, described and called. The issue's three-tier policy (searchable / describable / callable) is left for a follow-up if there's demand. The tools selector is the single knob for now.
  • Search defaults (search=None) to core's keyword matching, the same function ToolSearch uses, and takes any ToolSearchFunc.
  • Combine policy: the default id='tool_bridge' means two bridges merge into one, since the dispatcher names are fixed.
  • Category: Context management, next to Tool Search.
  • Telemetry: the bridge adds no spans of its own. The bridged call gets its own tool span through the normal hooks, and a test covers this.
  • Retries and scheduling: the dispatchers' max_retries defaults to the agent's tool retry budget (ctx.max_retries), as core's ToolSearchToolset does. A bridged tool's retries are counted under its own name in the run's tool manager, as for a direct call, so its ctx.retry and max_retries see only its own failures. Each failure also spends a tool_call retry. tool_call is sequential whenever a bridged tool is. sequential only affects local scheduling, so the provider's tool list doesn't change.
  • THREAT_MODEL.md: lists ToolBridge beside Code Mode as an execution path that has to keep approval controls. It also notes that approvals raised from inside a bridged tool name tool_call, and that bridged tool definitions arrive as tool results.
  • Shared internals: ToolBridge uses the tool_calls_limit reservation from _code_tools.reserve_nested_call, which CodeMode and SubAgents already use, and is_code_execution_tool.
  • Public core APIs only: unless_tool_kind handling doesn't use core's private filter. Tools with a kind or unless_tool_kind stay native, so the run's own filter picks the same tools it would without the bridge. The default search is core's keywords_search, which this PR exports from pydantic_ai.capabilities (a one-line re-export next to ToolSearchFunc), and a custom ToolSearchFunc can replace it. The nested ToolManager receives the run manager's failed_tools and succeeded_tools sets through its public constructor, so core's retry handling counts bridged tools' retries under their own names.
  • TOOL_CALL_KIND: tool_call has the tool kind tool_bridge.tool_call, exported with ToolCallArgs from pydantic_ai_harness.tool_bridge, so approval UIs and other capabilities can recognize it whatever it is named.
  • Ordering: ToolBridge declares CapabilityOrdering(position='outermost', wrapped_by=[ToolSearch]), so it sits just inside tool search and outside every other wrapper capability. SetToolMetadata and PrefixTools therefore apply to the bridged tools whatever order the capabilities are listed in. CodeMode and IPythonCodeMode now also declare wraps=[..., ToolBridge].
  • Code-execution tools stay native (code_arg_name metadata, as CodeMode uses), so a program isn't sent JSON-escaped inside args_json.

This PR changes core code outside pydantic_ai_harness, so it needs Aditya's (@adtyavrdhn) review. The core change exports the keyword search that ToolSearch(strategy='keywords') uses as pydantic_ai.capabilities.keywords_search. It's renamed from the private toolsets._tool_search.keywords_search_fn, since no other pydantic_ai.capabilities export ends in _fn. Outside harness, docs (the index, the capabilities overview, navigation) and THREAT_MODEL.md also changed.

Checklist

  • No breaking changes in accordance with the version policy.
  • Any permitted compatibility impact has a label, warning, migration and release note, and an exact API-check waiver if the gate reports it.
  • PR title is fit for the release changelog.