Skip to content

Commit 0f1e65c

Browse files
gn00295120claude
andcommitted
fix: correct McpError usage in demo script - use ErrorData type
Fixes mypy type errors in examples/mcp_http_error_handling_demo.py: - Use ErrorData(error_code, message) instead of McpError(string) - Add proper type annotation for model_dump_json() return value - Import ErrorData and INTERNAL_ERROR from mcp.types - Add mypy type: ignore comments for conditional MCP imports Resolves 4 mypy errors: 1. Line 39: McpError expects ErrorData, not str 2. Line 45: McpError expects ErrorData, not str 3. Line 51: McpError expects ErrorData, not str 4. Line 93: Returning Any from function declared to return str 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 78b3305 commit 0f1e65c

File tree

1 file changed

+37
-18
lines changed

1 file changed

+37
-18
lines changed

examples/mcp_http_error_handling_demo.py

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@
1717

1818
from agents import Agent, Runner, function_tool
1919

20+
# Import MCP types for proper error handling
21+
try:
22+
from mcp.shared.exceptions import McpError # type: ignore[import-not-found]
23+
from mcp.types import INTERNAL_ERROR, ErrorData # type: ignore[import-not-found]
24+
except ImportError:
25+
# Fallback for Python < 3.10 or when MCP is not installed
26+
McpError = None
27+
ErrorData = None
28+
INTERNAL_ERROR = None
29+
2030

2131
# Mock MCP server that simulates HTTP errors
2232
class MockMCPServerWithErrors:
@@ -34,21 +44,30 @@ async def call_tool(self, tool_name: str, arguments: dict[str, Any]):
3444

3545
if "invalid" in query.lower():
3646
# Simulate 422 Validation Error
37-
from mcp.shared.exceptions import McpError
38-
39-
raise McpError("GET https://api.example.com/search: 422 Validation Error")
47+
if McpError is not None and ErrorData is not None and INTERNAL_ERROR is not None:
48+
raise McpError(
49+
ErrorData(
50+
INTERNAL_ERROR,
51+
"GET https://api.example.com/search: 422 Validation Error",
52+
)
53+
)
4054

4155
if "notfound" in query.lower():
4256
# Simulate 404 Not Found
43-
from mcp.shared.exceptions import McpError
44-
45-
raise McpError("GET https://api.example.com/search: 404 Not Found")
57+
if McpError is not None and ErrorData is not None and INTERNAL_ERROR is not None:
58+
raise McpError(
59+
ErrorData(INTERNAL_ERROR, "GET https://api.example.com/search: 404 Not Found")
60+
)
4661

4762
if "servererror" in query.lower():
4863
# Simulate 500 Internal Server Error
49-
from mcp.shared.exceptions import McpError
50-
51-
raise McpError("GET https://api.example.com/search: 500 Internal Server Error")
64+
if McpError is not None and ErrorData is not None and INTERNAL_ERROR is not None:
65+
raise McpError(
66+
ErrorData(
67+
INTERNAL_ERROR,
68+
"GET https://api.example.com/search: 500 Internal Server Error",
69+
)
70+
)
5271

5372
# Successful case
5473
return type(
@@ -86,17 +105,17 @@ async def search(query: str) -> str:
86105
Search results or error message
87106
"""
88107
# This simulates how MCPUtil.invoke_mcp_tool works
89-
from mcp.shared.exceptions import McpError
90-
91108
try:
92109
result = await mock_server.call_tool("search", {"query": query})
93-
return result.content[0].model_dump_json()
94-
except McpError as e:
95-
# After PR #1948: Return structured error instead of crashing
96-
return json.dumps(
97-
{"error": {"message": str(e), "tool": "search", "type": "upstream_error"}}
98-
)
99-
except Exception:
110+
result_json: str = result.content[0].model_dump_json()
111+
return result_json
112+
except Exception as e:
113+
# Check if it's an MCP error (only when MCP is available)
114+
if McpError is not None and isinstance(e, McpError):
115+
# After PR #1948: Return structured error instead of crashing
116+
return json.dumps(
117+
{"error": {"message": str(e), "tool": "search", "type": "upstream_error"}}
118+
)
100119
# Programming errors still raise
101120
raise
102121

0 commit comments

Comments
 (0)