Skip to content

Commit a5db2f5

Browse files
committed
fix(tools-mcp): surface exception group exceptions (#2029)
fix: surface exception group exceptions
1 parent 4b94e97 commit a5db2f5

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
mcp[ws]>=1.10.0
1+
mcp[ws]>=1.10.0
2+
exceptiongroup>=1.2.2

griptape/tools/mcp/tool.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
111125
class 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

Comments
 (0)