Print MCP exception stacktrace

This commit is contained in:
文龙 2025-05-29 11:05:20 +08:00
parent 27af7a6f22
commit 47888425e2

View File

@ -1,4 +1,5 @@
import json
import traceback
from typing import Any, Dict, ForwardRef, List, Optional, Type, Union
from fastapi import HTTPException
@ -236,7 +237,9 @@ def get_tool_handler(
return final_response
except McpError as e:
print(f"MCP Error calling {endpoint_name}: {e.error}")
print(
f"MCP Error calling {endpoint_name}: {traceback.format_exc()}"
)
status_code = MCP_ERROR_TO_HTTP_STATUS.get(e.error.code, 500)
raise HTTPException(
status_code=status_code,
@ -247,7 +250,9 @@ def get_tool_handler(
),
)
except Exception as e:
print(f"Unexpected error calling {endpoint_name}: {e}")
print(
f"Unexpected error calling {endpoint_name}: {traceback.format_exc()}"
)
raise HTTPException(
status_code=500,
detail={"message": "Unexpected error", "error": str(e)},
@ -286,7 +291,9 @@ def get_tool_handler(
return final_response
except McpError as e:
print(f"MCP Error calling {endpoint_name}: {e.error}")
print(
f"MCP Error calling {endpoint_name}: {traceback.format_exc()}"
)
status_code = MCP_ERROR_TO_HTTP_STATUS.get(e.error.code, 500)
# Propagate the error received from MCP as an HTTP exception
raise HTTPException(
@ -298,7 +305,9 @@ def get_tool_handler(
),
)
except Exception as e:
print(f"Unexpected error calling {endpoint_name}: {e}")
print(
f"Unexpected error calling {endpoint_name}: {traceback.format_exc()}"
)
raise HTTPException(
status_code=500,
detail={"message": "Unexpected error", "error": str(e)},