forked from fractalic-ai/fractalic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfractalic_mcp_manager.py
More file actions
276 lines (226 loc) · 9.13 KB
/
fractalic_mcp_manager.py
File metadata and controls
276 lines (226 loc) · 9.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#!/usr/bin/env python3
"""
FastMCP-based MCP Manager - Main Entry Point
Maintains CLI compatibility with original fractalic_mcp_manager.py
"""
import asyncio
import logging
import argparse
import signal
import sys
from pathlib import Path
from aiohttp import web
import aiohttp_cors
from mcp_manager.api_handlers import setup_routes, init_manager
from mcp_manager.fastmcp_manager import FastMCPManager
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
class FastMCPServer:
"""FastMCP-based MCP Manager Server"""
def __init__(self, port: int = 5859, host: str = "localhost"):
self.port = port
self.host = host
self.app = None
self.runner = None
self.site = None
self._shutdown_event = asyncio.Event()
async def create_app(self):
"""Create aiohttp web application"""
self.app = web.Application()
# Initialize global manager
init_manager()
# Setup routes with CORS
setup_routes(self.app)
# Add middleware for logging
@web.middleware
async def logging_middleware(request, handler):
start_time = asyncio.get_event_loop().time()
try:
response = await handler(request)
process_time = asyncio.get_event_loop().time() - start_time
logger.info(f"{request.method} {request.path} - {response.status} - {process_time:.3f}s")
return response
except Exception as e:
process_time = asyncio.get_event_loop().time() - start_time
logger.error(f"{request.method} {request.path} - ERROR: {e} - {process_time:.3f}s")
raise
self.app.middlewares.append(logging_middleware)
return self.app
async def start_server(self):
"""Start the HTTP server"""
try:
await self.create_app()
self.runner = web.AppRunner(self.app)
await self.runner.setup()
self.site = web.TCPSite(self.runner, self.host, self.port)
await self.site.start()
logger.info(f"FastMCP Manager started on http://{self.host}:{self.port}")
logger.info("Available endpoints:")
logger.info(" GET /health - Health check")
logger.info(" GET /status - Service status")
logger.info(" GET /status/complete - Complete status with OAuth")
logger.info(" GET /list_tools - All tools (Fractalic compatibility)")
logger.info(" GET /tools - All tools")
logger.info(" GET /tools/{name} - Tools for service")
logger.info(" POST /call/{service}/{tool} - Call tool")
logger.info(" POST /toggle/{name} - Toggle service")
logger.info(" GET /oauth/status - OAuth status for all services")
logger.info(" POST /oauth/start/{service} - Start OAuth flow")
logger.info(" POST /oauth/reset/{service} - Reset OAuth tokens")
logger.info(" POST /add_server - Add MCP server")
logger.info(" POST /delete_server - Delete MCP server")
logger.info(" POST /kill - Shutdown server")
except Exception as e:
logger.error(f"Failed to start server: {e}")
raise
async def stop_server(self):
"""Stop the HTTP server"""
try:
if self.site:
await self.site.stop()
logger.info("Server site stopped")
if self.runner:
await self.runner.cleanup()
logger.info("Server runner cleaned up")
except Exception as e:
logger.error(f"Error stopping server: {e}")
async def run_forever(self):
"""Run server until shutdown signal"""
await self.start_server()
# Setup signal handlers
def signal_handler(signum, frame):
logger.info(f"Received signal {signum}, shutting down...")
self._shutdown_event.set()
signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)
try:
# Wait for shutdown signal
await self._shutdown_event.wait()
finally:
logger.info("Shutting down FastMCP Manager...")
await self.stop_server()
async def test_services():
"""Test connectivity to configured MCP services"""
logger.info("Testing MCP service connectivity...")
manager = FastMCPManager()
status = await manager.get_service_status()
print("\n=== MCP Services Status ===")
print(f"Total Services: {len(status['services'])}")
print(f"Enabled: {status['total_enabled']}")
print(f"Disabled: {status['total_disabled']}")
print()
for service_name, service_info in status['services'].items():
status_icon = "✅" if service_info['connected'] else "❌"
# OAuth status now comes separately from get_oauth_status()
tools_count = f" ({service_info['tools_count']} tools)" if service_info['connected'] else ""
error_msg = f" - Error: {service_info['error']}" if service_info.get('error') else ""
print(f"{status_icon} {service_name}{tools_count}{error_msg}")
print("\n=== OAuth Status ===")
oauth_status = await manager.get_oauth_status()
for service_name, oauth_info in oauth_status.items():
if oauth_info.get('authenticated'):
print(f"🔐 {service_name}: Authenticated")
else:
print(f"🔓 {service_name}: Not authenticated")
async def list_tools():
"""List all available tools from all services"""
logger.info("Listing all MCP tools...")
manager = FastMCPManager()
all_tools = await manager.get_all_tools()
print("\n=== Available MCP Tools ===")
total_tools = 0
for service_name, service_data in all_tools.items():
if 'error' in service_data:
print(f"\n❌ {service_name}: {service_data['error']}")
continue
tools = service_data.get('tools', [])
count = len(tools)
total_tools += count
print(f"\n📦 {service_name} ({count} tools):")
for tool in tools:
print(f" • {tool['name']}: {tool.get('description', 'No description')}")
print(f"\nTotal tools available: {total_tools}")
def main():
"""Main CLI entry point - maintains compatibility with original"""
parser = argparse.ArgumentParser(
description="FastMCP-based MCP Manager",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
python fastmcp_main.py # Start server on default port 5859
python fastmcp_main.py --port 8080 # Start server on custom port
python fastmcp_main.py --test # Test service connectivity
python fastmcp_main.py --list-tools # List all available tools
python fastmcp_main.py --verbose # Enable debug logging
"""
)
# Add optional 'serve' subcommand for UI server compatibility
parser.add_argument(
'command',
nargs='?',
default='serve',
choices=['serve'],
help='Command to run (default: serve) - for UI server compatibility'
)
parser.add_argument(
'--port', '-p',
type=int,
default=5859,
help='Port to run the server on (default: 5859)'
)
parser.add_argument(
'--host',
type=str,
default='localhost',
help='Host to bind the server to (default: localhost)'
)
parser.add_argument(
'--test', '-t',
action='store_true',
help='Test connectivity to all configured MCP services'
)
parser.add_argument(
'--list-tools', '-l',
action='store_true',
help='List all available tools from all services'
)
parser.add_argument(
'--verbose', '-v',
action='store_true',
help='Enable debug logging'
)
parser.add_argument(
'--config',
type=str,
default='mcp_servers.json',
help='Path to MCP servers configuration file'
)
args = parser.parse_args()
# Set logging level
if args.verbose:
logging.getLogger().setLevel(logging.DEBUG)
logger.debug("Debug logging enabled")
# Handle different modes
if args.test:
asyncio.run(test_services())
return
if args.list_tools:
asyncio.run(list_tools())
return
# Default: Start HTTP server
logger.info(f"Starting FastMCP Manager (reduced from 4656 to ~900 lines)")
logger.info(f"Configuration file: {args.config}")
server = FastMCPServer(port=args.port, host=args.host)
try:
asyncio.run(server.run_forever())
except KeyboardInterrupt:
logger.info("Received keyboard interrupt, shutting down...")
except Exception as e:
logger.error(f"Server error: {e}")
sys.exit(1)
if __name__ == '__main__':
main()