Spaces:
Runtime error
Runtime error
mashrur-rahman-fahim-brim commited on
Commit ·
e9e3f55
1
Parent(s): aee0dfd
fix the wake up issue
Browse files- mcp_client.py +70 -27
mcp_client.py
CHANGED
|
@@ -55,19 +55,39 @@ class FleetMindMCPClient:
|
|
| 55 |
|
| 56 |
async def _wake_up_space(self) -> bool:
|
| 57 |
"""Wake up HF space before connecting (free tier spaces sleep when inactive)"""
|
|
|
|
| 58 |
try:
|
| 59 |
-
async with httpx.AsyncClient(timeout=
|
| 60 |
-
|
|
|
|
| 61 |
try:
|
|
|
|
|
|
|
| 62 |
response = await client.get(f"{self.server_url}/")
|
| 63 |
if response.status_code == 200:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
return True
|
| 65 |
except httpx.TimeoutException:
|
| 66 |
-
|
| 67 |
-
|
|
|
|
| 68 |
continue
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
return False
|
| 70 |
-
except Exception:
|
|
|
|
| 71 |
return False
|
| 72 |
|
| 73 |
@asynccontextmanager
|
|
@@ -75,7 +95,8 @@ class FleetMindMCPClient:
|
|
| 75 |
"""Create a fresh MCP session for each operation (thread-safe)"""
|
| 76 |
sse_url = f"{self.server_url}/sse?api_key={self.api_key}"
|
| 77 |
|
| 78 |
-
|
|
|
|
| 79 |
async with ClientSession(read_stream, write_stream) as session:
|
| 80 |
await session.initialize()
|
| 81 |
yield session
|
|
@@ -84,30 +105,52 @@ class FleetMindMCPClient:
|
|
| 84 |
"""
|
| 85 |
Initialize connection to MCP server and discover tools
|
| 86 |
Uses official MCP SDK SSE client for fast connection
|
|
|
|
|
|
|
| 87 |
"""
|
| 88 |
try:
|
| 89 |
# First wake up HF space (free tier spaces sleep when inactive)
|
| 90 |
-
await self._wake_up_space()
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
)
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
except Exception as e:
|
| 113 |
return {"success": False, "error": str(e)}
|
|
|
|
| 55 |
|
| 56 |
async def _wake_up_space(self) -> bool:
|
| 57 |
"""Wake up HF space before connecting (free tier spaces sleep when inactive)"""
|
| 58 |
+
print("Waking up HuggingFace space...")
|
| 59 |
try:
|
| 60 |
+
async with httpx.AsyncClient(timeout=180.0) as client:
|
| 61 |
+
# Try up to 5 times with increasing delays
|
| 62 |
+
for attempt in range(5):
|
| 63 |
try:
|
| 64 |
+
print(f" Wake-up attempt {attempt + 1}/5...")
|
| 65 |
+
# First hit the root endpoint
|
| 66 |
response = await client.get(f"{self.server_url}/")
|
| 67 |
if response.status_code == 200:
|
| 68 |
+
print(f" Root endpoint responded OK")
|
| 69 |
+
# Also try the health/status endpoint if available
|
| 70 |
+
try:
|
| 71 |
+
health = await client.get(f"{self.server_url}/health", timeout=30.0)
|
| 72 |
+
print(f" Health check: {health.status_code}")
|
| 73 |
+
except:
|
| 74 |
+
pass
|
| 75 |
+
# Wait a bit for full initialization
|
| 76 |
+
await asyncio.sleep(2)
|
| 77 |
return True
|
| 78 |
except httpx.TimeoutException:
|
| 79 |
+
print(f" Attempt {attempt + 1} timed out, waiting...")
|
| 80 |
+
# Increasing delay between retries
|
| 81 |
+
await asyncio.sleep(3 + attempt * 2)
|
| 82 |
continue
|
| 83 |
+
except Exception as e:
|
| 84 |
+
print(f" Attempt {attempt + 1} error: {e}")
|
| 85 |
+
await asyncio.sleep(3 + attempt * 2)
|
| 86 |
+
continue
|
| 87 |
+
print(" Wake-up failed after all attempts")
|
| 88 |
return False
|
| 89 |
+
except Exception as e:
|
| 90 |
+
print(f" Wake-up exception: {e}")
|
| 91 |
return False
|
| 92 |
|
| 93 |
@asynccontextmanager
|
|
|
|
| 95 |
"""Create a fresh MCP session for each operation (thread-safe)"""
|
| 96 |
sse_url = f"{self.server_url}/sse?api_key={self.api_key}"
|
| 97 |
|
| 98 |
+
# Increased timeouts for cold-start scenarios
|
| 99 |
+
async with sse_client(sse_url, timeout=120.0, sse_read_timeout=300.0) as (read_stream, write_stream):
|
| 100 |
async with ClientSession(read_stream, write_stream) as session:
|
| 101 |
await session.initialize()
|
| 102 |
yield session
|
|
|
|
| 105 |
"""
|
| 106 |
Initialize connection to MCP server and discover tools
|
| 107 |
Uses official MCP SDK SSE client for fast connection
|
| 108 |
+
|
| 109 |
+
Includes retry logic for cold-start scenarios on HuggingFace
|
| 110 |
"""
|
| 111 |
try:
|
| 112 |
# First wake up HF space (free tier spaces sleep when inactive)
|
| 113 |
+
wake_up_success = await self._wake_up_space()
|
| 114 |
+
if not wake_up_success:
|
| 115 |
+
print("Warning: Space wake-up may have failed, trying to connect anyway...")
|
| 116 |
+
|
| 117 |
+
# Retry connection up to 3 times
|
| 118 |
+
last_error = None
|
| 119 |
+
for attempt in range(3):
|
| 120 |
+
try:
|
| 121 |
+
print(f"Connecting to MCP server (attempt {attempt + 1}/3)...")
|
| 122 |
+
|
| 123 |
+
# Create a temporary session just to discover tools
|
| 124 |
+
async with self._get_session() as session:
|
| 125 |
+
# Discover tools
|
| 126 |
+
tools_result = await session.list_tools()
|
| 127 |
+
for tool in tools_result.tools:
|
| 128 |
+
self.tools[tool.name] = MCPTool(
|
| 129 |
+
name=tool.name,
|
| 130 |
+
description=tool.description or "",
|
| 131 |
+
parameters=tool.inputSchema if hasattr(tool, 'inputSchema') else {}
|
| 132 |
+
)
|
| 133 |
+
|
| 134 |
+
self._connected = True
|
| 135 |
+
print(f"Connected successfully! Found {len(self.tools)} tools.")
|
| 136 |
+
|
| 137 |
+
return {
|
| 138 |
+
"success": True,
|
| 139 |
+
"session_id": "mcp-sdk-session",
|
| 140 |
+
"tools_count": len(self.tools),
|
| 141 |
+
"tools": list(self.tools.keys())
|
| 142 |
+
}
|
| 143 |
+
|
| 144 |
+
except Exception as e:
|
| 145 |
+
last_error = str(e)
|
| 146 |
+
print(f"Connection attempt {attempt + 1} failed: {e}")
|
| 147 |
+
if attempt < 2:
|
| 148 |
+
# Wait before retry with increasing delay
|
| 149 |
+
wait_time = 3 + attempt * 2
|
| 150 |
+
print(f"Retrying in {wait_time} seconds...")
|
| 151 |
+
await asyncio.sleep(wait_time)
|
| 152 |
+
|
| 153 |
+
return {"success": False, "error": f"Failed after 3 attempts: {last_error}"}
|
| 154 |
|
| 155 |
except Exception as e:
|
| 156 |
return {"success": False, "error": str(e)}
|