-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathbeeai-framework-error-context.patch
More file actions
50 lines (45 loc) · 2.44 KB
/
Copy pathbeeai-framework-error-context.patch
File metadata and controls
50 lines (45 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
diff --git a/python/beeai_framework/adapters/mcp/serve/server.py b/python/beeai_framework/adapters/mcp/serve/server.py
index b4a458be..93dc2025 100644
--- a/python/beeai_framework/adapters/mcp/serve/server.py
+++ b/python/beeai_framework/adapters/mcp/serve/server.py
@@ -169,7 +169,16 @@ def _tool_factory(
) -> MCPNativeTool:
async def run(**kwargs: Any) -> MCPCallToolResult:
cloned_tool = await tool.clone()
- output: ToolOutput = await cloned_tool.run(kwargs)
+ try:
+ output: ToolOutput = await cloned_tool.run(kwargs)
+ except Exception as e:
+ error_context = getattr(e, "context", None) or {}
+ meta = {"error_context": error_context} if error_context else None
+ return MCPCallToolResult(
+ content=[MCPTextContent(type="text", text=f"Error executing tool {tool.name}: {e}")],
+ isError=True,
+ _meta=meta,
+ )
return MCPCallToolResult(
content=[MCPTextContent(type="text", text=output.get_text_content())],
@@ -195,7 +204,9 @@ def _tool_factory(
# The FastMCP server allows either returning a structured output or a message. We want to support both.
# Based on https://github.com/modelcontextprotocol/python-sdk
# ... return a tuple of (content, structured_data)
- lambda result: (result.content, result.structuredContent),
+ # Error results are passed through as CallToolResult so the lowlevel server
+ # preserves isError and _meta (the tuple path reconstructs with isError=False).
+ lambda result: result if result.isError else (result.content, result.structuredContent),
)
return mcp_tool
diff --git a/python/beeai_framework/tools/mcp/mcp.py b/python/beeai_framework/tools/mcp/mcp.py
index 3fbd5c25..47e39d05 100644
--- a/python/beeai_framework/tools/mcp/mcp.py
+++ b/python/beeai_framework/tools/mcp/mcp.py
@@ -98,7 +98,11 @@ class MCPTool(Tool[BaseModel, ToolRunOptions, JSONToolOutput]):
data_result = result.content
if result.isError:
- raise ToolError(to_json(data_result, indent=4, sort_keys=False))
+ error_context = (result.meta or {}).get("error_context")
+ raise ToolError(
+ to_json(data_result, indent=4, sort_keys=False),
+ context=error_context if isinstance(error_context, dict) else None,
+ )
return JSONToolOutput(data_result)