@@ -38,14 +38,13 @@ async def setup(self):
3838 async def _get_tools (self ) -> Optional [List [dict [str , Any ]]]:
3939 if self .evaluation_row .tools is None :
4040 if self .mcp_client :
41- raw_tools = await self .mcp_client .get_available_tools ()
41+ raw_tools = await self .mcp_client .connect_to_servers () or None # ensure servers are connected
42+ raw_tools = await self .mcp_client .get_available_tools () if self .mcp_client else None
4243 tools_dicts : List [dict [str , Any ]] = []
4344 for t in raw_tools or []:
4445 if isinstance (t , dict ):
45- # Already a dict-like structure
4646 tools_dicts .append (t )
4747 continue
48- # Fallback: extract attributes from OpenAI types
4948 tool_type = getattr (t , "type" , "function" )
5049 func = getattr (t , "function" , None )
5150 name = getattr (func , "name" , None )
@@ -104,12 +103,10 @@ async def call_agent(self) -> Optional[Union[str, List[ChatCompletionContentPart
104103 return message .content
105104
106105 async def _call_model (self , messages : list [Message ], tools : Optional [List [dict [str , Any ]]]) -> Message :
107- # Convert Message models to plain dicts for LLM call
108106 messages_payload : List [Dict [str , Any ]] = [
109- message .model_dump () if hasattr (message , "model_dump" ) else message # type: ignore[misc]
107+ ( message .model_dump () if hasattr (message , "model_dump" ) else message ) # type: ignore[misc]
110108 for message in messages
111109 ]
112- # Normalize tool definitions into OpenAI-compatible dicts
113110 payload_tools : List [Dict [str , Any ]] = []
114111 for tool in tools or []:
115112 if isinstance (tool , dict ):
@@ -119,7 +116,6 @@ async def _call_model(self, messages: list[Message], tools: Optional[List[dict[s
119116 elif isinstance (fn , dict ):
120117 fn_payload = fn
121118 else :
122- # Best effort fallback
123119 name = getattr (fn , "name" , None )
124120 params = getattr (fn , "parameters" , None )
125121 if hasattr (params , "model_dump" ):
@@ -131,7 +127,6 @@ async def _call_model(self, messages: list[Message], tools: Optional[List[dict[s
131127 fn_payload = {"name" : name , "parameters" : params_payload }
132128 payload_tools .append ({"type" : tool .get ("type" , "function" ), "function" : fn_payload })
133129 else :
134- # Attribute-based fallback
135130 tool_type = getattr (tool , "type" , "function" )
136131 func = getattr (tool , "function" , None )
137132 name = getattr (func , "name" , None )
@@ -145,14 +140,17 @@ async def _call_model(self, messages: list[Message], tools: Optional[List[dict[s
145140 payload_tools .append ({"type" : tool_type , "function" : {"name" : name , "parameters" : params_payload }})
146141
147142 response = await self ._policy ._make_llm_call (messages = messages_payload , tools = payload_tools )
148- # Coerce content to a string to align with our Message model type expectations
149143 raw_content = response ["choices" ][0 ]["message" ].get ("content" )
150144 if isinstance (raw_content , list ):
151- content_for_model = "" .join ([getattr (p , "text" , str (p )) for p in raw_content ])
145+
146+ def _part_to_text (p : Any ) -> str :
147+ return getattr (p , "text" , str (p ))
148+
149+ content_for_model : Union [str , List [Any ]] = "" .join (_part_to_text (p ) for p in raw_content )
152150 else :
153151 content_for_model = raw_content
154152 return Message (
155- role = response ["choices" ][0 ]["message" ][ "role" ] ,
153+ role = response ["choices" ][0 ]["message" ]. get ( "role" , "assistant" ) ,
156154 content = content_for_model ,
157155 tool_calls = response ["choices" ][0 ]["message" ].get ("tool_calls" ),
158156 )
@@ -184,14 +182,9 @@ def _get_content_from_tool_result(self, tool_result: CallToolResult) -> List[Tex
184182 def _format_tool_message_content (
185183 self , content : List [TextContent ]
186184 ) -> Union [str , List [ChatCompletionContentPartTextParam ]]:
187- """Format tool result content for inclusion in a tool message.
188-
189- - If a single text item, return plain string per OpenAI semantics.
190- - If multiple items, return a list of text parts.
191- """
192185 if len (content ) == 1 and isinstance (content [0 ], TextContent ):
193186 return content [0 ].text
194- return [ChatCompletionContentPartTextParam (text = c .text , type = "text" ) for c in content ]
187+ return [ChatCompletionContentPartTextParam (text = c .text , type = "text" ) for c in content if hasattr ( c , "text" ) ]
195188
196189
197190class AgentRolloutProcessor (RolloutProcessor ):
0 commit comments