Skip to content

Commit 717566e

Browse files
author
Containerized Agent
committed
fix: support MCP tools in programmatic_tool_caller
Use agent.tool.<name>() instead of directly calling tool_impl() from registry. This properly handles all tool types including MCP tools which are not directly callable but work through the ToolExecutor._stream() mechanism. - Changed _execute_tool to use getattr(agent.tool, tool_name)() - Added record_direct_tool_call=False to prevent polluting message history - Handle AttributeError for tool not found case
1 parent 4c0857b commit 717566e

1 file changed

Lines changed: 10 additions & 9 deletions

File tree

src/strands_tools/programmatic_tool_caller.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -141,19 +141,18 @@ def execute(self, code: str, namespace: Dict[str, Any]) -> str:
141141

142142

143143
def _execute_tool(agent: Any, tool_name: str, tool_input: Dict[str, Any]) -> Any:
144-
"""Execute a tool through the agent's tool registry."""
144+
"""Execute a tool through the agent's tool caller.
145+
146+
Uses agent.tool.<name>() which properly handles all tool types including MCP tools.
147+
"""
145148
if agent is None:
146149
raise RuntimeError("No agent available for tool execution")
147150

148-
tool_impl = agent.tool_registry.registry.get(tool_name)
149-
if tool_impl is None:
150-
raise RuntimeError(f"Tool '{tool_name}' not found in registry")
151-
152151
try:
153-
if callable(tool_impl):
154-
result = tool_impl(**tool_input)
155-
else:
156-
raise RuntimeError(f"Tool '{tool_name}' is not callable")
152+
# Use agent.tool.<name>() which works for ALL tool types (including MCP tools)
153+
# record_direct_tool_call=False prevents polluting message history during programmatic calls
154+
tool_func = getattr(agent.tool, tool_name)
155+
result = tool_func(record_direct_tool_call=False, **tool_input)
157156

158157
if isinstance(result, dict):
159158
if result.get("status") == "error":
@@ -170,6 +169,8 @@ def _execute_tool(agent: Any, tool_name: str, tool_input: Dict[str, Any]) -> Any
170169

171170
return result
172171

172+
except AttributeError as e:
173+
raise RuntimeError(f"Tool '{tool_name}' not found in registry") from e
173174
except RuntimeError:
174175
raise
175176
except Exception as e:

0 commit comments

Comments
 (0)