@@ -67,6 +67,38 @@ async def client_wrapper(endpoint: str, headers: dict[str, str]) -> AsyncGenerat
67
67
raise AuthenticationRequiredError (exc ) from exc
68
68
if i == len (connection_strategies ) - 1 :
69
69
raise
70
+ except* httpx .ConnectError as eg :
71
+ # Connection refused, server down, network unreachable
72
+ if i == len (connection_strategies ) - 1 :
73
+ error_msg = f"Failed to connect to MCP server at { endpoint } : Connection refused"
74
+ logger .error (f"MCP connection error: { error_msg } " )
75
+ raise ConnectionError (error_msg ) from eg
76
+ else :
77
+ logger .warning (
78
+ f"failed to connect to MCP server at { endpoint } via { strategy .name } , falling back to { connection_strategies [i + 1 ].name } "
79
+ )
80
+ except* httpx .TimeoutException as eg :
81
+ # Request timeout, server too slow
82
+ if i == len (connection_strategies ) - 1 :
83
+ error_msg = f"MCP server at { endpoint } timed out"
84
+ logger .error (f"MCP timeout error: { error_msg } " )
85
+ raise TimeoutError (error_msg ) from eg
86
+ else :
87
+ logger .warning (
88
+ f"MCP server at { endpoint } timed out via { strategy .name } , falling back to { connection_strategies [i + 1 ].name } "
89
+ )
90
+ except* httpx .RequestError as eg :
91
+ # DNS resolution failures, network errors, invalid URLs
92
+ if i == len (connection_strategies ) - 1 :
93
+ # Get the first exception's message for the error string
94
+ exc_msg = str (eg .exceptions [0 ]) if eg .exceptions else "Unknown error"
95
+ error_msg = f"Network error connecting to MCP server at { endpoint } : { exc_msg } "
96
+ logger .error (f"MCP network error: { error_msg } " )
97
+ raise ConnectionError (error_msg ) from eg
98
+ else :
99
+ logger .warning (
100
+ f"network error connecting to MCP server at { endpoint } via { strategy .name } , falling back to { connection_strategies [i + 1 ].name } "
101
+ )
70
102
except* McpError :
71
103
if i < len (connection_strategies ) - 1 :
72
104
logger .warning (
0 commit comments