Spaces:
Sleeping
Sleeping
Update server_manager.py
Browse files- server_manager.py +53 -12
server_manager.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
"""
|
2 |
Server management utilities for Universal MCP Client
|
|
|
3 |
"""
|
4 |
import asyncio
|
5 |
import re
|
@@ -13,7 +14,7 @@ from mcp_client import UniversalMCPClient
|
|
13 |
logger = logging.getLogger(__name__)
|
14 |
|
15 |
class ServerManager:
|
16 |
-
"""Manages MCP server connections and status"""
|
17 |
|
18 |
def __init__(self, mcp_client: UniversalMCPClient):
|
19 |
self.mcp_client = mcp_client
|
@@ -99,6 +100,7 @@ class ServerManager:
|
|
99 |
<p><strong>Space:</strong> {space_name}</p>
|
100 |
<p><strong>Base URL:</strong> {mcp_url}</p>
|
101 |
<p><strong>Status:</strong> Connected successfully!</p>
|
|
|
102 |
<div style="margin-top: 10px;">
|
103 |
{tools_info.replace('**', '<strong>').replace('**', '</strong>').replace(chr(10), '<br>')}
|
104 |
</div>
|
@@ -126,20 +128,24 @@ class ServerManager:
|
|
126 |
def get_server_status(self) -> Tuple[str, str]:
|
127 |
"""Get status of all servers in accordion format"""
|
128 |
try:
|
129 |
-
status
|
130 |
-
server_count = f"**Total MCP Servers**: {len(status)}"
|
131 |
|
132 |
-
|
133 |
-
|
|
|
|
|
|
|
|
|
|
|
134 |
|
135 |
accordion_html = ""
|
136 |
|
137 |
-
for name,
|
138 |
-
|
139 |
-
base_url =
|
140 |
|
141 |
-
# Determine health status
|
142 |
-
health = "π’ Healthy"
|
143 |
|
144 |
accordion_html += f"""
|
145 |
<details style="margin-bottom: 10px;">
|
@@ -149,11 +155,46 @@ class ServerManager:
|
|
149 |
<p><strong>Status:</strong> Connected (MCP Protocol)</p>
|
150 |
<p><strong>Health:</strong> {health}</p>
|
151 |
<p><strong>Base URL:</strong> {base_url}</p>
|
|
|
|
|
152 |
</div>
|
153 |
</details>
|
154 |
"""
|
155 |
|
156 |
-
|
|
|
157 |
|
158 |
except Exception as e:
|
159 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
"""
|
2 |
Server management utilities for Universal MCP Client
|
3 |
+
Version 2.0 - Enhanced with server enabling/disabling support
|
4 |
"""
|
5 |
import asyncio
|
6 |
import re
|
|
|
14 |
logger = logging.getLogger(__name__)
|
15 |
|
16 |
class ServerManager:
|
17 |
+
"""Manages MCP server connections and status with enhanced server management"""
|
18 |
|
19 |
def __init__(self, mcp_client: UniversalMCPClient):
|
20 |
self.mcp_client = mcp_client
|
|
|
100 |
<p><strong>Space:</strong> {space_name}</p>
|
101 |
<p><strong>Base URL:</strong> {mcp_url}</p>
|
102 |
<p><strong>Status:</strong> Connected successfully!</p>
|
103 |
+
<p><strong>Enabled:</strong> Yes (new servers are enabled by default)</p>
|
104 |
<div style="margin-top: 10px;">
|
105 |
{tools_info.replace('**', '<strong>').replace('**', '</strong>').replace(chr(10), '<br>')}
|
106 |
</div>
|
|
|
128 |
def get_server_status(self) -> Tuple[str, str]:
|
129 |
"""Get status of all servers in accordion format"""
|
130 |
try:
|
131 |
+
logger.info(f"π Getting server status, found {len(self.mcp_client.servers)} servers")
|
|
|
132 |
|
133 |
+
# Get server status from mcp_client
|
134 |
+
server_count = len(self.mcp_client.servers)
|
135 |
+
server_status_text = f"**Total MCP Servers**: {server_count}"
|
136 |
+
|
137 |
+
if not self.mcp_client.servers:
|
138 |
+
logger.info("π No servers found")
|
139 |
+
return server_status_text, "<p><em>No MCP servers configured yet.</em></p>"
|
140 |
|
141 |
accordion_html = ""
|
142 |
|
143 |
+
for name, config in self.mcp_client.servers.items():
|
144 |
+
logger.info(f"π Processing server: {name}")
|
145 |
+
base_url = config.url.replace("/gradio_api/mcp/sse", "")
|
146 |
|
147 |
+
# Determine health status (assume healthy if it's in the servers dict)
|
148 |
+
health = "π’ Healthy"
|
149 |
|
150 |
accordion_html += f"""
|
151 |
<details style="margin-bottom: 10px;">
|
|
|
155 |
<p><strong>Status:</strong> Connected (MCP Protocol)</p>
|
156 |
<p><strong>Health:</strong> {health}</p>
|
157 |
<p><strong>Base URL:</strong> {base_url}</p>
|
158 |
+
<p><strong>Description:</strong> {config.description}</p>
|
159 |
+
{f'<p><strong>Space ID:</strong> {config.space_id}</p>' if config.space_id else ''}
|
160 |
</div>
|
161 |
</details>
|
162 |
"""
|
163 |
|
164 |
+
logger.info(f"π Generated accordion HTML for {server_count} servers")
|
165 |
+
return server_status_text, accordion_html
|
166 |
|
167 |
except Exception as e:
|
168 |
+
error_msg = f"Error getting server status: {str(e)}"
|
169 |
+
logger.error(f"β {error_msg}")
|
170 |
+
logger.error(traceback.format_exc())
|
171 |
+
return "**Total MCP Servers**: Error", f"<p style='color: red;'>β {error_msg}</p>"
|
172 |
+
|
173 |
+
def remove_server(self, server_name: str) -> Tuple[str, str]:
|
174 |
+
"""Remove a specific server"""
|
175 |
+
success = self.mcp_client.remove_server(server_name)
|
176 |
+
if success:
|
177 |
+
return f"β
Removed server: {server_name}", ""
|
178 |
+
else:
|
179 |
+
return f"β Server not found: {server_name}", ""
|
180 |
+
|
181 |
+
def remove_all_servers(self) -> Tuple[str, str]:
|
182 |
+
"""Remove all servers"""
|
183 |
+
count = self.mcp_client.remove_all_servers()
|
184 |
+
if count > 0:
|
185 |
+
return f"β
Removed all {count} MCP servers", ""
|
186 |
+
else:
|
187 |
+
return "βΉοΈ No servers to remove", ""
|
188 |
+
|
189 |
+
def enable_server(self, server_name: str, enabled: bool = True) -> Tuple[str, str]:
|
190 |
+
"""Enable or disable a server"""
|
191 |
+
if server_name in self.mcp_client.servers:
|
192 |
+
self.mcp_client.enable_server(server_name, enabled)
|
193 |
+
status = "enabled" if enabled else "disabled"
|
194 |
+
return f"β
Server {server_name} {status}", ""
|
195 |
+
else:
|
196 |
+
return f"β Server not found: {server_name}", ""
|
197 |
+
|
198 |
+
def get_server_list_with_status(self) -> list:
|
199 |
+
"""Get server list with enable/disable status for UI components"""
|
200 |
+
return self.mcp_client.get_server_list_with_status()
|