@@ -97,34 +97,13 @@ async def create_openai_response(
9797
9898 # Apply tool filtering if enabled and tools are provided
9999 filtered_tools = tools
100- if (
101- tools
102- and self .config .tools_filter .enabled
103- and len (tools ) > self .config .tools_filter .min_tools
104- ):
105- logger .info (
106- "Tool filtering enabled - filtering %d tools (threshold: %d)" ,
107- len (tools ),
108- self .config .tools_filter .min_tools ,
109- )
100+ if tools and self .config .tools_filter .enabled :
110101 filtered_tools = await self ._filter_tools_for_response (
111102 input = input ,
112103 tools = tools ,
113104 model = model ,
114105 conversation = conversation ,
115106 )
116- logger .info (
117- "Tool filtering complete - reduced from %d to %d tools" ,
118- len (tools ),
119- len (filtered_tools ) if filtered_tools else 0 ,
120- )
121- else :
122- logger .info (
123- "Skipping tool filtering - %d tools (threshold: %d, enabled: %s)" ,
124- len (tools ) if tools else 0 ,
125- self .config .tools_filter .min_tools ,
126- self .config .tools_filter .enabled ,
127- )
128107
129108 # Call parent with filtered tools and temperature
130109 return await super ().create_openai_response (
@@ -189,6 +168,20 @@ async def _filter_tools_for_response(
189168 logger .warning ("No tool definitions found for filtering" )
190169 return tools
191170
171+ if len (tools_for_filtering ) <= self .config .tools_filter .min_tools :
172+ logger .info (
173+ "Skipping tool filtering - %d tools (threshold: %d)" ,
174+ len (tools_for_filtering ),
175+ self .config .tools_filter .min_tools ,
176+ )
177+ return tools
178+
179+ logger .info (
180+ "Tool filtering enabled - filtering %d tools (threshold: %d)" ,
181+ len (tools_for_filtering ),
182+ self .config .tools_filter .min_tools ,
183+ )
184+
192185 # Extract user prompt text from input
193186 if isinstance (input , str ):
194187 user_prompt = input
@@ -246,28 +239,26 @@ async def _filter_tools_for_response(
246239 logger .error ("Failed to parse LLM response as JSON: %s" , exp )
247240 filtered_tool_names = []
248241
249- # Filter the original tools list
242+ # Filter using expanded tool definitions
250243 if filtered_tool_names or always_included_tools :
251- # Create a mapping from tool names to tool configs
252- tool_name_to_config = {}
253- for i , tool in enumerate (tools ):
254- tool_dict = tool if isinstance (tool , dict ) else tool .model_dump ()
255- tool_name = self ._get_tool_name_from_config (tool_dict , i )
256- tool_name_to_config [tool_name ] = tool
257-
258- # Filter based on LLM response and always included tools
259- filtered_tools = [
260- tool_name_to_config [name ]
261- for name in tool_name_to_config
262- if name in filtered_tool_names or name in always_included_tools
244+ selected_names = set (filtered_tool_names ) | always_included_tools
245+ result = [
246+ {
247+ "type" : "function" ,
248+ "name" : td ["tool_name" ],
249+ "description" : td ["description" ],
250+ "parameters" : td .get ("parameters" , {}),
251+ }
252+ for td in tools_for_filtering
253+ if td ["tool_name" ] in selected_names
263254 ]
264255
265256 logger .info (
266- "Filtered tools count : %d removed, %d remaining" ,
267- len (tools ) - len (filtered_tools ),
268- len (filtered_tools ),
257+ "Filtered tools: %d removed, %d remaining" ,
258+ len (tools_for_filtering ) - len (result ),
259+ len (result ),
269260 )
270- return filtered_tools
261+ return result
271262 else :
272263 logger .warning ("No tools matched filtering criteria, returning empty list" )
273264 return []
@@ -337,9 +328,11 @@ async def _extract_tool_definitions(
337328 }
338329 )
339330 elif tool_type == "function" :
340- name = tool_dict .get ("name" , f"function_{ i } " )
341- description = tool_dict .get ("description" , "" )
342- tool_defs .append ({"tool_name" : name , "description" : description })
331+ func = tool_dict .get ("function" , {})
332+ name = func .get ("name" , tool_dict .get ("name" , f"function_{ i } " ))
333+ description = func .get ("description" , tool_dict .get ("description" , "" ))
334+ parameters = func .get ("parameters" , tool_dict .get ("parameters" , {}))
335+ tool_defs .append ({"tool_name" : name , "description" : description , "parameters" : parameters })
343336 else :
344337 logger .warning ("Unknown tool type: %s" , tool_type )
345338 tool_defs .append (
@@ -385,6 +378,7 @@ async def _get_mcp_tool_definitions(
385378 {
386379 "tool_name" : tool_def .name ,
387380 "description" : tool_def .description or "" ,
381+ "parameters" : tool_def .parameters if hasattr (tool_def , "parameters" ) else {},
388382 }
389383 )
390384
0 commit comments