Skip to content

Commit d1944b5

Browse files
committed
feat(tools): remove tool_name
1 parent 4beba2b commit d1944b5

File tree

2 files changed

+10
-29
lines changed

2 files changed

+10
-29
lines changed

src/strands/tools/registry.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -279,25 +279,23 @@ def register_tool(self, tool: AgentTool) -> None:
279279
list(self.dynamic_tools.keys()),
280280
)
281281

282-
def replace(self, tool_name: str, new_tool: AgentTool) -> None:
282+
def replace(self, new_tool: AgentTool) -> None:
283283
"""Replace an existing tool with a new implementation.
284284
285285
This performs an atomic swap of the tool implementation in the registry.
286286
The replacement takes effect on the next agent invocation.
287287
288288
Args:
289-
tool_name: Name of the tool to replace.
290-
new_tool: New tool implementation.
289+
new_tool: New tool implementation. Its name must match the tool being replaced.
291290
292291
Raises:
293-
ValueError: If the tool doesn't exist or if names don't match.
292+
ValueError: If the tool doesn't exist.
294293
"""
294+
tool_name = new_tool.tool_name
295+
295296
if tool_name not in self.registry:
296297
raise ValueError(f"Cannot replace tool '{tool_name}' - tool does not exist")
297298

298-
if new_tool.tool_name != tool_name:
299-
raise ValueError(f"Tool names must match - expected '{tool_name}', got '{new_tool.tool_name}'")
300-
301299
# Atomic replacement in main registry
302300
self.registry[tool_name] = new_tool
303301

tests/strands/tools/test_registry.py

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ def test_tool_registry_replace_existing_tool():
526526

527527
registry = ToolRegistry()
528528
registry.register_tool(old_tool)
529-
registry.replace("my_tool", new_tool)
529+
registry.replace(new_tool)
530530

531531
assert registry.registry["my_tool"] == new_tool
532532

@@ -539,24 +539,7 @@ def test_tool_registry_replace_nonexistent_tool():
539539
registry = ToolRegistry()
540540

541541
with pytest.raises(ValueError, match="Cannot replace tool 'my_tool' - tool does not exist"):
542-
registry.replace("my_tool", new_tool)
543-
544-
545-
def test_tool_registry_replace_with_different_name():
546-
"""Test replacing with different name raises ValueError."""
547-
old_tool = MagicMock()
548-
old_tool.tool_name = "old_tool"
549-
old_tool.is_dynamic = False
550-
old_tool.supports_hot_reload = False
551-
552-
new_tool = MagicMock()
553-
new_tool.tool_name = "new_tool"
554-
555-
registry = ToolRegistry()
556-
registry.register_tool(old_tool)
557-
558-
with pytest.raises(ValueError, match="Tool names must match"):
559-
registry.replace("old_tool", new_tool)
542+
registry.replace(new_tool)
560543

561544

562545
def test_tool_registry_replace_dynamic_tool():
@@ -572,7 +555,7 @@ def test_tool_registry_replace_dynamic_tool():
572555

573556
registry = ToolRegistry()
574557
registry.register_tool(old_tool)
575-
registry.replace("dynamic_tool", new_tool)
558+
registry.replace(new_tool)
576559

577560
assert registry.registry["dynamic_tool"] == new_tool
578561
assert registry.dynamic_tools["dynamic_tool"] == new_tool
@@ -594,7 +577,7 @@ def test_tool_registry_replace_dynamic_with_non_dynamic():
594577

595578
assert "my_tool" in registry.dynamic_tools
596579

597-
registry.replace("my_tool", new_tool)
580+
registry.replace(new_tool)
598581

599582
assert registry.registry["my_tool"] == new_tool
600583
assert "my_tool" not in registry.dynamic_tools
@@ -616,7 +599,7 @@ def test_tool_registry_replace_non_dynamic_with_dynamic():
616599

617600
assert "my_tool" not in registry.dynamic_tools
618601

619-
registry.replace("my_tool", new_tool)
602+
registry.replace(new_tool)
620603

621604
assert registry.registry["my_tool"] == new_tool
622605
assert registry.dynamic_tools["my_tool"] == new_tool

0 commit comments

Comments
 (0)