Skip to content

Commit 3c89d8c

Browse files
committed
fix: use named parameters for ErrorData and proper MCP_AVAILABLE flag
- Change ErrorData(code, message) to use named parameters: code=, message= - Replace None checks with MCP_AVAILABLE boolean flag - Provide proper fallback values: McpError=Exception, INTERNAL_ERROR=-32603 - This fixes all 10 mypy errors in the demo script
1 parent 06b9aa6 commit 3c89d8c

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

examples/mcp_http_error_handling_demo.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,14 @@
2121
try:
2222
from mcp.shared.exceptions import McpError # type: ignore[import-not-found]
2323
from mcp.types import INTERNAL_ERROR, ErrorData # type: ignore[import-not-found]
24+
25+
MCP_AVAILABLE = True
2426
except ImportError:
2527
# Fallback for Python < 3.10 or when MCP is not installed
26-
McpError = None
28+
MCP_AVAILABLE = False
29+
McpError = Exception
2730
ErrorData = None
28-
INTERNAL_ERROR = None
31+
INTERNAL_ERROR = -32603
2932

3033

3134
# Mock MCP server that simulates HTTP errors
@@ -44,28 +47,31 @@ async def call_tool(self, tool_name: str, arguments: dict[str, Any]):
4447

4548
if "invalid" in query.lower():
4649
# Simulate 422 Validation Error
47-
if McpError is not None and ErrorData is not None and INTERNAL_ERROR is not None:
50+
if MCP_AVAILABLE:
4851
raise McpError(
4952
ErrorData(
50-
INTERNAL_ERROR,
51-
"GET https://api.example.com/search: 422 Validation Error",
53+
code=INTERNAL_ERROR,
54+
message="GET https://api.example.com/search: 422 Validation Error",
5255
)
5356
)
5457

5558
if "notfound" in query.lower():
5659
# Simulate 404 Not Found
57-
if McpError is not None and ErrorData is not None and INTERNAL_ERROR is not None:
60+
if MCP_AVAILABLE:
5861
raise McpError(
59-
ErrorData(INTERNAL_ERROR, "GET https://api.example.com/search: 404 Not Found")
62+
ErrorData(
63+
code=INTERNAL_ERROR,
64+
message="GET https://api.example.com/search: 404 Not Found",
65+
)
6066
)
6167

6268
if "servererror" in query.lower():
6369
# Simulate 500 Internal Server Error
64-
if McpError is not None and ErrorData is not None and INTERNAL_ERROR is not None:
70+
if MCP_AVAILABLE:
6571
raise McpError(
6672
ErrorData(
67-
INTERNAL_ERROR,
68-
"GET https://api.example.com/search: 500 Internal Server Error",
73+
code=INTERNAL_ERROR,
74+
message="GET https://api.example.com/search: 500 Internal Server Error",
6975
)
7076
)
7177

@@ -111,7 +117,7 @@ async def search(query: str) -> str:
111117
return result_json
112118
except Exception as e:
113119
# Check if it's an MCP error (only when MCP is available)
114-
if McpError is not None and isinstance(e, McpError):
120+
if MCP_AVAILABLE and isinstance(e, McpError):
115121
# After PR #1948: Return structured error instead of crashing
116122
return json.dumps(
117123
{"error": {"message": str(e), "tool": "search", "type": "upstream_error"}}

0 commit comments

Comments
 (0)