@@ -210,10 +210,16 @@ async def handle_call_tool(name: str, args: dict[str, Any]) -> list[TextContent]
210210 )
211211
212212 # Return the sampling result in the tool response
213- content = (
214- sampling_result .content [0 ] if isinstance (sampling_result .content , list ) else sampling_result .content
215- )
216- response = content .text if content .type == "text" else None
213+ if isinstance (sampling_result .content , list ):
214+ response = (
215+ sampling_result .content [0 ].text
216+ if sampling_result .content and sampling_result .content [0 ].type == "text"
217+ else None
218+ )
219+ elif sampling_result .content .type == "text" :
220+ response = sampling_result .content .text
221+ else :
222+ response = None
217223 return [
218224 TextContent (
219225 type = "text" ,
@@ -1242,12 +1248,13 @@ async def sampling_callback(
12421248 nonlocal sampling_callback_invoked , captured_message_params
12431249 sampling_callback_invoked = True
12441250 captured_message_params = params
1245- msg_content = (
1246- params .messages [0 ].content [0 ]
1247- if isinstance (params .messages [0 ].content , list )
1248- else params .messages [0 ].content
1249- )
1250- message_received = msg_content .text if msg_content .type == "text" else None
1251+ msg_content = params .messages [0 ].content
1252+ if isinstance (msg_content , list ):
1253+ message_received = msg_content [0 ].text if msg_content and msg_content [0 ].type == "text" else None
1254+ elif msg_content .type == "text" :
1255+ message_received = msg_content .text
1256+ else :
1257+ message_received = None
12511258
12521259 return types .CreateMessageResult (
12531260 role = "assistant" ,
0 commit comments