@@ -117,6 +117,10 @@ async def on_tools_listing_error(
117117 original exception; this hook is only invoked by the framework's
118118 isolate-and-continue paths.
119119
120+ Framework callers apply ``tool_name_prefix`` to the returned tools the same
121+ way ``get_tools_with_prefix`` would, so overrides should return unprefixed
122+ tool names.
123+
120124 Args:
121125 error: The exception raised by ``get_tools`` / ``get_tools_with_prefix``.
122126 readonly_context: The same context passed to the failed listing call.
@@ -128,52 +132,27 @@ async def on_tools_listing_error(
128132 del error , readonly_context
129133 return []
130134
131- @final
132- async def get_tools_with_prefix (
133- self ,
134- readonly_context : Optional [ReadonlyContext ] = None ,
135- ) -> list [BaseTool ]:
136- """Return all tools with optional prefix applied to tool names.
137-
138- This method calls get_tools() and applies prefixing if tool_name_prefix is provided.
135+ def _apply_tool_name_prefix (self , tools : list [BaseTool ]) -> list [BaseTool ]:
136+ """Applies ``tool_name_prefix`` to tools, matching ``get_tools_with_prefix``.
139137
140138 Args:
141- readonly_context (ReadonlyContext, optional): Context used to filter tools
142- available to the agent. If None, all tools in the toolset are returned .
139+ tools: Tools with unprefixed names (as returned by ``get_tools`` or
140+ ``on_tools_listing_error``) .
143141
144142 Returns:
145- list[BaseTool]: A list of tools with prefixed names if tool_name_prefix is provided.
143+ The same tools when no prefix is configured; otherwise shallow copies
144+ with prefixed ``name`` / declaration names.
146145 """
147- invocation_id = readonly_context .invocation_id if readonly_context else None
148-
149- if (
150- self ._use_invocation_cache
151- and self ._cached_prefixed_tools is not None
152- and self ._cached_invocation_id == invocation_id
153- ):
154- return self ._cached_prefixed_tools
155-
156- tools = await self .get_tools (readonly_context )
157-
158146 if not self .tool_name_prefix :
159- self ._cached_invocation_id = invocation_id
160- self ._cached_prefixed_tools = tools
161147 return tools
162148
163149 prefix = self .tool_name_prefix
164-
165- # Create copies of tools to avoid modifying original instances
166150 prefixed_tools = []
167151 for tool in tools :
168- # Create a shallow copy of the tool
169152 tool_copy = copy .copy (tool )
170-
171- # Apply prefix to the copied tool
172- prefixed_name = f"{ prefix } _{ tool .name } "
153+ prefixed_name = f'{ prefix } _{ tool .name } '
173154 tool_copy .name = prefixed_name
174155
175- # Also update the function declaration name if the tool has one
176- # Use default parameters to capture the current values in the closure
177156 def _create_prefixed_declaration (
178157 original_get_declaration = tool ._get_declaration ,
179158 prefixed_name = prefixed_name ,
@@ -189,6 +168,35 @@ def _get_prefixed_declaration():
189168
190169 tool_copy ._get_declaration = _create_prefixed_declaration ()
191170 prefixed_tools .append (tool_copy )
171+ return prefixed_tools
172+
173+ @final
174+ async def get_tools_with_prefix (
175+ self ,
176+ readonly_context : Optional [ReadonlyContext ] = None ,
177+ ) -> list [BaseTool ]:
178+ """Return all tools with optional prefix applied to tool names.
179+
180+ This method calls get_tools() and applies prefixing if tool_name_prefix is provided.
181+
182+ Args:
183+ readonly_context (ReadonlyContext, optional): Context used to filter tools
184+ available to the agent. If None, all tools in the toolset are returned.
185+
186+ Returns:
187+ list[BaseTool]: A list of tools with prefixed names if tool_name_prefix is provided.
188+ """
189+ invocation_id = readonly_context .invocation_id if readonly_context else None
190+
191+ if (
192+ self ._use_invocation_cache
193+ and self ._cached_prefixed_tools is not None
194+ and self ._cached_invocation_id == invocation_id
195+ ):
196+ return self ._cached_prefixed_tools
197+
198+ tools = await self .get_tools (readonly_context )
199+ prefixed_tools = self ._apply_tool_name_prefix (tools )
192200
193201 self ._cached_invocation_id = invocation_id
194202 self ._cached_prefixed_tools = prefixed_tools
0 commit comments