Store a toolset copy of a Tool instead of rewriting the one other toolsets hold
Closes #12810
What problem this solves
FunctionToolset.add_tool() applied the toolset's defaults by assigning to the Tool it was given:
if tool.max_retries is None and self.max_retries is not None:
tool.max_retries = self.max_retries
if self.requires_approval:
tool.requires_approval = True
if self.metadata is not None:
tool.metadata = self.metadata | (tool.metadata or {})
A Tool is an ordinary object that can be held by more than one toolset, so the last toolset it was added to rewrote what every other holder saw. Before this PR, on main:
tool = Tool(my_tool)
plain = FunctionToolset([tool])
guarded = FunctionToolset([tool], requires_approval=True, max_retries=3, metadata={'k': 'v'})
plain.tools['my_tool'].requires_approval, plain.tools['my_tool'].max_retries, plain.tools['my_tool'].metadata
# (True, 3, {'k': 'v'})
plain never asked for approval, retries or metadata; it silently got all three. The same happens to a Tool passed to Agent(tools=[tool]) while it is also listed in a configured FunctionToolset, and the order in which the toolsets are built decides which value a nullable field (max_retries) ends up with.
How it is fixed
add_tool() now stores dataclasses.replace(tool, ...) with the defaults resolved into the copy, and leaves the argument untouched, so each toolset resolves its own defaults for its own copy and the shared Tool keeps what the caller set. add_function() returns the stored copy (self.tools[tool.name]) instead of the object it constructed internally, so the tool a decorator hands back still carries the toolset's defaults — and an explicit requires_approval=False is still applied to the stored copy, which kept its override behaviour (test_function_requires_approval_false_overrides_the_toolset_default).
Nothing reads toolset.tools[name] is tool: the ACP adapter's _scan_approval_names and get_tools() read the resolved values from toolset.tools, which still hold them.
Verification
Two tests added to tests/test_tools.py:
-
test_add_tool_leaves_a_shared_tool_untouched— red before the fix (assert (True, 3, {'k': 'v'}) == (False, None, None)), green after: the plain toolset reports(False, None, None), the configured one reports(True, 3, {'k': 'v'}), and theToolthe caller holds is unchanged. -
test_add_tool_returns_the_toolset_copy_from_add_function— pins thatadd_function()returns the stored copy carrying the toolset'smax_retries. -
pytest tests/test_tools.py -p no:randomly -q→ 185 passed -
pytest tests/test_toolsets.py tests/test_agent.py tests/test_tool_population_diff.py -p no:randomly -q→ 633 passed, 2 skipped, 5 failed: all five are the MCP-stdio tests that need atests.mcp_serverchild process and fail on this Windows host onmaintoo (McpError/RuntimeErrorfromfastmcp), not related to this change. -
ruff check+ruff format --checkclean on both files. -
scripts/check_complexity.py --base origin/mainclean.
Maintainer: this touches pkg:core, so it needs the pkg:core label, which I can't apply as an external contributor.
Summary by cubic
Fixes FunctionToolset.add_tool() rewriting a shared Tool, so adding the same tool to multiple toolsets no longer lets the last toolset's defaults leak into all the others. Each toolset now stores its own copy resolved with max_retries, strict, include_return_schema, requires_approval, and metadata, and the original Tool keeps the caller's values; add_function() returns that stored copy so the decorator still hands back a tool carrying the toolset's defaults.
Closes #12810.
add_tool()applies the per-toolset defaults to adataclasses.replace(tool, ...)copy instead of mutating the passed-in object.sequentialanddefer_loadingstill mutate the sharedTooland its markers, since durable execution reads them to list a tool the way a workflow started before they were set saw it.add_function()restores its explicitsequential,requires_approval, anddefer_loadingoverrides on the stored copy and returns it.- Adds three tests pinning the shared-tool behavior and the
add_function()return value.