@@ -107,6 +107,20 @@ def process_anyof_types(any_of_items: list) -> Or:
107107 return Or (* any_of_types ) if any_of_types else ANY_TYPE
108108
109109
110+ def _exc_iter (exc ) -> Any : # noqa: ANN001
111+ """Iterate over all non-exceptiongroup parts of an exception(group) because spread syntax not available in python 3.9.
112+
113+ https://stackoverflow.com/a/78453879
114+ """
115+ from exceptiongroup import BaseExceptionGroup
116+
117+ if isinstance (exc , BaseExceptionGroup ):
118+ for e in exc .exceptions :
119+ yield from _exc_iter (e )
120+ else :
121+ yield exc
122+
123+
110124@define
111125class MCPTool (BaseTool ):
112126 """MCP activities through a tool.
@@ -181,11 +195,16 @@ def activity_handler(self: MCPTool, values: dict) -> Any:
181195
182196 async def _run_activity (self , activity_name : str , params : dict ) -> BaseArtifact :
183197 """Runs an activity on the MCP Server with the provided parameters."""
198+ from exceptiongroup import BaseExceptionGroup
199+
184200 try :
185201 async with self ._get_session () as session :
186202 await session .initialize ()
187203 tool_result = await session .call_tool (activity_name , params )
188204 return self ._convert_call_tool_result_to_artifact (tool_result )
205+ except BaseExceptionGroup as e :
206+ exception_message = "" .join (f"\n { str (exc )} " for exc in _exc_iter (e ))
207+ return ErrorArtifact (value = exception_message )
189208 except Exception as e :
190209 return ErrorArtifact (value = str (e ), exception = e )
191210
0 commit comments