-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdeploy.py
More file actions
1033 lines (872 loc) · 44.8 KB
/
Copy pathdeploy.py
File metadata and controls
1033 lines (872 loc) · 44.8 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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""
MCP Server Deployment Script
"""
import argparse
import sys
import time
import json
import subprocess
import signal
import os
import socket
import hashlib
from pathlib import Path
import select
from dataclasses import dataclass
from typing import Dict, List, Optional
import threading
import logging
import fcntl # For file locking on Unix/Mac
# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
def is_port_in_use(port: int, host: str = "0.0.0.0") -> bool:
"""
Check if a port is currently in use.
Args:
port: The port number to check
host: The host address (default: 0.0.0.0)
Returns:
True if port is in use, False if available
"""
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
try:
s.bind((host, port))
return False
except OSError:
return True
def find_available_port(start_port: int, end_port: int, used_ports: set = None) -> Optional[int]:
"""
Find the next available port in a range.
Args:
start_port: Starting port number
end_port: Ending port number (inclusive)
used_ports: Set of ports already assigned in config (optional)
Returns:
Available port number or None if no ports available
"""
if used_ports is None:
used_ports = set()
for port in range(start_port, end_port + 1):
if port not in used_ports and not is_port_in_use(port):
return port
return None
DEFAULT_FOLDER = "mcp_host"
WORKSPACE_FOLDER = "workspace"
# Define default port ranges, overwritten by arguments
HOST_PORT_MIN = 5000
HOST_PORT_MAX = 5099
# Crash-restart policy for supervised servers
RESTART_MAX_ATTEMPTS = 5 # Restarts allowed per server within the window
RESTART_WINDOW_S = 300 # Sliding window for the restart cap (seconds)
RESTART_BASE_DELAY_S = 2.0 # Backoff delay before the first restart (seconds)
RESTART_MAX_DELAY_S = 60.0 # Backoff delay ceiling (seconds)
def generate_instance_id(workspace_path: str) -> str:
"""
Generate a unique instance ID from workspace path.
Uses first 8 characters of MD5 hash of workspace path.
Args:
workspace_path: Path to the workspace directory
Returns:
Unique instance ID (8 characters)
Example:
"workspace_martin" -> "a3f2b1c9"
"workspace_john" -> "f7e2d4a1"
"""
workspace_abs = Path(workspace_path).resolve()
hash_obj = hashlib.md5(str(workspace_abs).encode())
instance_id = hash_obj.hexdigest()[:8]
logger.info(f"Generated instance_id '{instance_id}' for workspace '{workspace_abs}'")
return instance_id
@dataclass
class ProcessInfo:
"""Immutable process information"""
proc: subprocess.Popen
file_path: str
port: Optional[int] = None
process_type: str = 'python'
status: str = 'running'
is_critical: bool = False # If True, failure will cause deployment to exit
def __post_init__(self):
if self.process_type not in ['python', 'docker']:
raise ValueError(f"Invalid process type: {self.process_type}")
@dataclass
class PendingRestart:
"""A crashed server queued for relaunch once its backoff delay expires"""
file_path: str
port: Optional[int]
process_type: str
restart_at: float
class ProcessManager:
"""Manages MCP server processes with proper lifecycle"""
def __init__(self, workspace_dir: Path, instance_id: str = "default"):
self.processes: List[ProcessInfo] = []
self.shutdown_event = threading.Event()
self.failure_event = threading.Event() # Set when a critical process fails
self.workspace_dir = workspace_dir
self.failed_processes: List[ProcessInfo] = [] # Track failed processes
self.instance_id = instance_id # Unique instance identifier for Docker services
self.pending_restarts: List[PendingRestart] = [] # Crashed servers awaiting relaunch
self.restart_history: Dict[str, List[float]] = {} # Restart timestamps per server path
self.abandoned_servers: List[str] = [] # Servers given up on after repeated crashes
def start_python_server(self, server_path: Path, port: int) -> ProcessInfo:
"""Start a Python MCP server in the workspace directory"""
if not server_path.exists():
raise FileNotFoundError(f"Server file not found: {server_path}")
# Set up environment with server directory in PYTHONPATH
server_dir = server_path.parent
env = os.environ.copy()
current_pythonpath = env.get('PYTHONPATH', '')
if current_pythonpath:
env['PYTHONPATH'] = f"{server_dir}:{current_pythonpath}"
else:
env['PYTHONPATH'] = str(server_dir)
# Use absolute path for the server file since we're changing working directory
absolute_server_path = server_path.resolve()
# Use the same Python interpreter that's running this script
py_cmd = sys.executable
cmd = [py_cmd, str(absolute_server_path), str(port)]
proc = subprocess.Popen(
cmd,
cwd=self.workspace_dir, # Execute in workspace directory
env=env, # Preserve import paths
stdout=subprocess.PIPE,
stderr=subprocess.PIPE, # Capture stderr separately
text=True,
bufsize=1
)
process_info = ProcessInfo(
proc=proc,
file_path=str(server_path),
port=port,
process_type='python'
)
self.processes.append(process_info)
logger.info(f"Started Python server: {absolute_server_path} on port {port} (workspace: {self.workspace_dir})")
return process_info
def start_docker_compose(self, compose_file: Path, port: Optional[int] = None) -> ProcessInfo:
"""Start docker-compose service with optional port configuration"""
platform = sys.platform
# Project name must be unique per compose file: workspace-hash isolates
# *this workspace* from other workspaces, and the parent-directory slug
# isolates *this MCP* from sibling MCPs whose compose files declare the
# same service name (e.g. `app`). Sharing a project across MCPs collapses
# them onto the same image tag and container name.
service_slug = compose_file.parent.name.lower().replace('.', '_')
project_name = f"toolomics_{self.instance_id}_{service_slug}"
if platform == "linux":
cmd = ['docker', 'compose', '-p', project_name, '-f', str(compose_file), 'up', '-d']
else:
cmd = ['docker-compose', '-p', project_name, '-f', str(compose_file), 'up', '-d']
logger.info(f"Using Docker project name: {project_name}")
# Set up environment with port if provided
env = os.environ.copy()
if port is not None:
env['MCP_PORT'] = str(port)
env['FASTMCP_PORT'] = str(port)
logger.info(f"Setting MCP_PORT={port} for docker-compose: {compose_file}")
# Set instance-specific environment variables for Docker services
env['INSTANCE_ID'] = self.instance_id
logger.info(f"Setting INSTANCE_ID={self.instance_id} for docker-compose: {compose_file}")
# Set workspace path for docker-compose volume mounts (relative to project root)
workspace_path = str(self.workspace_dir)
env['WORKSPACE_PATH'] = workspace_path
logger.info(f"Setting WORKSPACE_PATH={workspace_path} for docker-compose: {compose_file}")
# Set auxiliary ports for services that need them
# RStudio Server port (default 8787, offset by 1000+ instance hash to avoid conflicts)
rstudio_port = 9000 + (int(self.instance_id, 16) % 1000)
env['RSTUDIO_PORT'] = str(rstudio_port)
# SearxNG port - fixed at 8080 for browser MCP server compatibility
# Browser MCP server expects SearXNG at localhost:8080
searxng_port = 8080
env['SEARXNG_PORT'] = str(searxng_port)
logger.info(f"Setting auxiliary ports - RSTUDIO_PORT={rstudio_port}, SEARXNG_PORT={searxng_port}")
proc = subprocess.Popen(
cmd,
env=env,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
bufsize=1
)
process_info = ProcessInfo(
proc=proc,
file_path=str(compose_file),
port=port,
process_type='docker',
is_critical=False
)
self.processes.append(process_info)
logger.info(f"Started Docker compose: {compose_file}" + (f" on port {port}" if port else ""))
return process_info
def monitor_processes(self, check_interval: float = 0.1):
"""
Monitor all processes with non-blocking I/O, restarting crashed servers.
Python MCP servers are long-running: any exit is treated as a crash and
the server is relaunched after an exponential backoff (RESTART_BASE_DELAY_S
doubling up to RESTART_MAX_DELAY_S). Docker 'up -d' bootstraps normally
exit 0 and are only re-run when they fail; the containers themselves are
supervised by Docker via their 'restart: unless-stopped' policy.
A server crashing more than RESTART_MAX_ATTEMPTS times within
RESTART_WINDOW_S is abandoned and reported at exit.
"""
while ((self.processes or self.pending_restarts)
and not self.shutdown_event.is_set()
and not self.failure_event.is_set()):
# Check process outputs
for process_info in self.processes[:]:
self._check_process_output(process_info)
# Remove completed/failed processes
if process_info.proc.poll() is not None:
failed = self._handle_process_completion(process_info)
self.processes.remove(process_info)
# If a critical process failed, trigger shutdown
if failed and process_info.is_critical:
logger.error(f"Critical process {process_info.file_path} failed, initiating shutdown...")
self.failure_event.set()
break
if self._needs_restart(process_info, failed):
queued = self._schedule_restart(process_info.file_path, process_info.port,
process_info.process_type)
# A crash being recovered by restart is not an unrecovered failure
if queued and process_info in self.failed_processes:
self.failed_processes.remove(process_info)
self._launch_due_restarts()
time.sleep(check_interval)
# If we exited due to failure, shutdown remaining processes
if self.failure_event.is_set():
self.shutdown()
self._display_failure_summary()
sys.exit(1)
if self.abandoned_servers:
self.display_abandoned_summary()
sys.exit(1)
def _needs_restart(self, process_info: ProcessInfo, failed: bool) -> bool:
"""Python servers restart on any exit; docker bootstraps only when they failed"""
if self.shutdown_event.is_set():
return False
if process_info.process_type == 'python':
return True
return failed
def _schedule_restart(self, file_path: str, port: Optional[int], process_type: str) -> bool:
"""
Queue a delayed restart for a crashed server.
Applies exponential backoff (doubling from RESTART_BASE_DELAY_S, capped at
RESTART_MAX_DELAY_S) and abandons the server once it has been restarted
RESTART_MAX_ATTEMPTS times within the RESTART_WINDOW_S sliding window.
Returns True when a restart was queued, False when the server was abandoned.
"""
now = time.time()
recent = [t for t in self.restart_history.get(file_path, []) if now - t < RESTART_WINDOW_S]
if len(recent) >= RESTART_MAX_ATTEMPTS:
self.abandoned_servers.append(file_path)
self._report_abandonment(file_path)
return False
recent.append(now)
self.restart_history[file_path] = recent
delay = min(RESTART_BASE_DELAY_S * 2 ** (len(recent) - 1), RESTART_MAX_DELAY_S)
self.pending_restarts.append(PendingRestart(file_path, port, process_type, now + delay))
logger.warning(f"Server {file_path} exited unexpectedly. "
f"Restart {len(recent)}/{RESTART_MAX_ATTEMPTS} in {delay:.0f}s.")
return True
def _launch_due_restarts(self):
"""Relaunch every queued server whose backoff delay has elapsed"""
now = time.time()
for restart in [r for r in self.pending_restarts if r.restart_at <= now]:
self.pending_restarts.remove(restart)
self._execute_restart(restart)
def _execute_restart(self, restart: PendingRestart):
"""
Relaunch one server.
A python restart whose port is still busy (TIME_WAIT, lingering worker,
or stolen by another process) is deferred instead of launched into a
guaranteed bind failure. Deferrals and failed relaunches both consume
a restart attempt, so a permanently stolen port ends in abandonment.
"""
port_busy = (restart.process_type == 'python' and restart.port is not None
and is_port_in_use(restart.port))
if port_busy:
logger.warning(f"Port {restart.port} still busy, deferring restart of {restart.file_path}")
self._schedule_restart(restart.file_path, restart.port, restart.process_type)
return
try:
if restart.process_type == 'python':
self.start_python_server(Path(restart.file_path), restart.port)
else:
self.start_docker_compose(Path(restart.file_path), restart.port)
logger.info(f"Restarted {restart.file_path}"
+ (f" on port {restart.port}" if restart.port else ""))
except Exception as e:
logger.error(f"Restart of {restart.file_path} failed: {e}")
self._schedule_restart(restart.file_path, restart.port, restart.process_type)
def _report_abandonment(self, file_path: str):
"""Loudly report a server given up on, at the moment it happens"""
logger.error("=" * 80)
logger.error(f"SERVER ABANDONED: {file_path}")
logger.error(f"It crashed {RESTART_MAX_ATTEMPTS} times within {RESTART_WINDOW_S}s "
f"and will NOT be restarted again.")
logger.error("Fix the server code, then rerun start.sh to bring it back.")
logger.error("=" * 80)
def display_abandoned_summary(self):
"""Display servers abandoned after exceeding the restart cap"""
logger.error("=" * 80)
logger.error("SERVERS ABANDONED AFTER REPEATED CRASHES")
logger.error("=" * 80)
for file_path in self.abandoned_servers:
logger.error(f"ABANDONED: {file_path}")
def _check_process_output(self, process_info: ProcessInfo):
"""Check and log process output"""
proc = process_info.proc
# Check stdout
if proc.stdout and proc.stdout.readable():
try:
if hasattr(select, 'select'):
# Use select to check if data is available
ready, _, _ = select.select([proc.stdout], [], [], 0)
while ready:
line = proc.stdout.readline()
if not line:
break
logger.info(f"[{process_info.file_path}:{process_info.port}] STDOUT: {line.strip()}")
# Check if more data is available
ready, _, _ = select.select([proc.stdout], [], [], 0)
else:
# Fallback for Windows - try to read one line
try:
line = proc.stdout.readline()
if line:
logger.info(f"[{process_info.file_path}:{process_info.port}] STDOUT: {line.strip()}")
except:
pass
except Exception as e:
logger.error(f"Error reading stdout from {process_info.file_path}: {e}")
# Check stderr
if proc.stderr and proc.stderr.readable():
try:
if hasattr(select, 'select'):
# Use select to check if data is available
ready, _, _ = select.select([proc.stderr], [], [], 0)
while ready:
line = proc.stderr.readline()
if not line:
break
logger.error(f"[{process_info.file_path}:{process_info.port}] STDERR: {line.strip()}")
# Check if more data is available
ready, _, _ = select.select([proc.stderr], [], [], 0)
else:
# Fallback for Windows - try to read one line
try:
line = proc.stderr.readline()
if line:
logger.error(f"[{process_info.file_path}:{process_info.port}] STDERR: {line.strip()}")
except:
pass
except Exception as e:
logger.error(f"Error reading stderr from {process_info.file_path}: {e}")
def _handle_process_completion(self, process_info: ProcessInfo) -> bool:
"""Handle process completion and return True if process failed"""
return_code = process_info.proc.poll()
# Read remaining output from both stdout and stderr
try:
stdout_output, stderr_output = process_info.proc.communicate(timeout=1)
# Display remaining stdout
if stdout_output:
logger.info(f"[{process_info.file_path}] Final STDOUT:")
for line in stdout_output.split('\n'):
if line.strip():
logger.info(f"[{process_info.file_path}] STDOUT: {line.strip()}")
# Display remaining stderr
if stderr_output:
logger.error(f"[{process_info.file_path}] Final STDERR:")
for line in stderr_output.split('\n'):
if line.strip():
logger.error(f"[{process_info.file_path}] STDERR: {line.strip()}")
except subprocess.TimeoutExpired:
logger.warning(f"Timeout reading final output from {process_info.file_path}")
# Force kill and try to get output
try:
process_info.proc.kill()
stdout_output, stderr_output = process_info.proc.communicate(timeout=1)
if stderr_output:
logger.error(f"[{process_info.file_path}] STDERR (after kill): {stderr_output}")
except Exception as e:
print(str(e))
pass
# Track failed processes
failed = return_code != 0
if failed:
logger.error(f"Process {process_info.file_path} exited with code {return_code}")
self.failed_processes.append(process_info)
else:
logger.info(f"Process {process_info.file_path} completed successfully")
return failed
def _display_failure_summary(self):
"""Display a summary of failed processes"""
if not self.failed_processes:
return
logger.error("=" * 80)
logger.error("ONE OR MORE PROCESSES FAILED")
logger.error("=" * 80)
logger.error(f"Number of failed processes: {len(self.failed_processes)}")
for process_info in self.failed_processes:
logger.error(f"FAILED: {process_info.file_path}")
logger.error(f" Port: {process_info.port}")
logger.error(f" Type: {process_info.process_type}")
logger.error(f" Exit Code: {process_info.proc.returncode}")
logger.error(f" Critical: {process_info.is_critical}")
def shutdown(self):
"""Gracefully shutdown all processes"""
logger.info("Shutting down all processes...")
self.shutdown_event.set()
for process_info in self.processes:
try:
process_info.proc.terminate()
process_info.proc.wait(timeout=5)
except subprocess.TimeoutExpired:
logger.warning(f"Force killing process {process_info.file_path}")
process_info.proc.kill()
except Exception as e:
logger.error(f"Error shutting down {process_info.file_path}: {e}")
class ServerDiscovery:
"""Handles discovery of MCP servers and Docker services"""
@staticmethod
def has_gpu() -> bool:
"""Detect if GPU (NVIDIA) is available on the system AND usable by the Docker daemon.
nvidia-smi only checks the host. The docker CLI may point at a daemon that
cannot honor GPU device requests (e.g. the Docker Desktop VM engine via the
desktop-linux context), which then fails with
'could not select device driver "nvidia" with capabilities: [[gpu]]'.
Verify both sides before selecting a GPU-enabled compose file.
"""
try:
# Check if nvidia-smi command exists and can detect GPU
result = subprocess.run(
['nvidia-smi'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
timeout=5
)
if result.returncode != 0:
logger.info("No GPU detected: Running in CPU-only mode")
return False
except (FileNotFoundError, subprocess.TimeoutExpired, Exception) as e:
logger.info(f"No GPU detected: {e}")
return False
logger.info("GPU detected: NVIDIA GPU available")
# Host has a GPU, but the daemon selected by the active docker context
# must also support it: require an nvidia runtime or nvidia.com/gpu CDI devices.
try:
result = subprocess.run(
['docker', 'info'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
timeout=15,
text=True
)
if result.returncode != 0:
logger.warning(f"Could not query docker daemon ('docker info' failed), falling back to CPU compose files: {result.stderr.strip()}")
return False
info = result.stdout
has_nvidia_runtime = any(
line.strip().startswith('Runtimes:') and 'nvidia' in line.split()
for line in info.splitlines()
)
has_nvidia_cdi = 'nvidia.com/gpu' in info
if has_nvidia_runtime or has_nvidia_cdi:
logger.info("Docker daemon supports GPU (nvidia runtime or nvidia.com/gpu CDI devices found)")
return True
logger.warning(
"Docker daemon has no nvidia runtime or nvidia.com/gpu CDI devices. "
"The active docker context may point at an engine without GPU support "
"(e.g. Docker Desktop). Run 'docker context use default' to fix this. "
"Falling back to CPU compose files."
)
return False
except (FileNotFoundError, subprocess.TimeoutExpired, Exception) as e:
logger.warning(f"Could not verify docker daemon GPU support ({e}), falling back to CPU compose files")
return False
@staticmethod
def find_server_files(mcp_dir: Path) -> List[Path]:
"""Find all server.py files in subdirectories, excluding those with docker-compose.yml in the same folder"""
server_files = []
for server_file in mcp_dir.rglob('server.py'):
if server_file.parent != mcp_dir: # Must be in subdirectory
# Check if there's a docker-compose.yml in the same directory
docker_compose_in_same_dir = (server_file.parent / 'docker-compose.yml').exists()
if not docker_compose_in_same_dir:
server_files.append(server_file)
else:
logger.info(f"Skipping {server_file} - docker-compose.yml found in same directory")
return server_files
@staticmethod
def find_docker_compose_files(mcp_dir: Path) -> List[Path]:
"""
Find all docker-compose files in subdirectories.
Prefers .gpu.yml files when GPU is available, otherwise uses standard .yml files.
"""
has_gpu = ServerDiscovery.has_gpu()
compose_files = []
processed_dirs = set()
# First pass: collect all docker-compose files by directory
compose_by_dir = {}
for compose_file in mcp_dir.rglob('docker-compose*.yml'):
if compose_file.parent != mcp_dir: # Must be in subdirectory
parent = compose_file.parent
if parent not in compose_by_dir:
compose_by_dir[parent] = []
compose_by_dir[parent].append(compose_file)
# Second pass: select appropriate file based on GPU availability
for parent, files in compose_by_dir.items():
gpu_file = None
standard_file = None
for f in files:
if f.name == 'docker-compose.gpu.yml':
gpu_file = f
elif f.name == 'docker-compose.yml':
standard_file = f
# Select the appropriate file
if has_gpu and gpu_file:
logger.info(f"Using GPU-enabled compose file: {gpu_file}")
compose_files.append(gpu_file)
elif standard_file:
if has_gpu and not gpu_file:
logger.info(f"GPU detected but no .gpu.yml found, using standard: {standard_file}")
else:
logger.info(f"Using standard compose file: {standard_file}")
compose_files.append(standard_file)
else:
logger.warning(f"No suitable docker-compose file found in {parent}")
return compose_files
class ConfigManager:
"""Manages port configuration with persistence"""
def __init__(self, config_path: str):
self.config_path = Path(config_path)
def load_config(self) -> Dict[str, dict]:
"""
Load port configuration with enabled flag with file locking.
Returns dict mapping path to {"port": int, "enabled": bool}
"""
if not self.config_path.exists():
logger.warning(f"Config file {self.config_path} does not exist.")
return {}
# Check if file is empty
if self.config_path.stat().st_size == 0:
logger.warning(f"Config file {self.config_path} is empty.")
return {}
try:
with open(self.config_path, 'r', encoding='utf-8-sig') as f: # utf-8-sig handles BOM
# Acquire shared lock for reading
try:
fcntl.flock(f.fileno(), fcntl.LOCK_SH)
except Exception as lock_e:
logger.warning(f"Could not acquire file lock: {lock_e}")
try:
content = f.read().strip()
if not content:
logger.warning(f"Config file {self.config_path} contains only whitespace.")
return {}
# Debug logging
logger.debug(f"Config file size: {len(content)} chars, first 50: {content[:50]}")
config_list = json.loads(content)
config_dict = {}
for item in config_list:
if 'path' in item and 'port' in item:
# New format
path = item['path']
config_dict[path] = {
'port': item['port'],
'enabled': item.get('enabled', True) # Default to enabled
}
else:
raise ValueError("Can't parse config.json file")
logger.info(f"Successfully loaded {len(config_dict)} items from config (enabled: {sum(1 for v in config_dict.values() if v.get('enabled'))})")
return config_dict
finally:
# Release lock
try:
fcntl.flock(f.fileno(), fcntl.LOCK_UN)
except:
pass
except (json.JSONDecodeError, KeyError, IndexError, ValueError) as e:
logger.error(f"Error loading config from {self.config_path}: {e}")
# Try to read the file again to see what's actually there
try:
file_size = self.config_path.stat().st_size
with open(self.config_path, 'rb') as f:
raw_bytes = f.read(100)
logger.error(f"File size: {file_size}, first 100 bytes: {raw_bytes!r}")
except Exception as debug_e:
logger.error(f"Could not read file for debugging: {debug_e}")
# Backup the corrupted file instead of silently regenerating
backup_path = self.config_path.with_suffix('.json.backup')
try:
import shutil
shutil.copy2(self.config_path, backup_path)
logger.warning(f"Backed up corrupted config to: {backup_path}")
except Exception as backup_e:
logger.warning(f"Could not backup config: {backup_e}")
logger.warning(f"Config file appears corrupted. Will regenerate fresh config.")
logger.warning(f"If you had enabled services, please re-enable them after checking {backup_path}")
return {}
def save_config(self, config: Dict[str, dict]) -> None:
"""
Save port configuration with enabled flag.
Config dict maps path to {"port": int, "enabled": bool}
Uses atomic write to prevent race conditions.
"""
# Convert dict to list format with new structure
config_list = [
{
"path": path,
"port": info['port'],
"enabled": info['enabled']
}
for path, info in config.items()
]
# Write to temporary file first, then rename atomically
import tempfile
temp_fd, temp_path = tempfile.mkstemp(
dir=self.config_path.parent,
prefix='.tmp_config_',
suffix='.json',
text=True
)
try:
with os.fdopen(temp_fd, 'w') as f:
json.dump(config_list, f, indent=4)
# Atomic rename (on POSIX systems)
os.replace(temp_path, self.config_path)
logger.debug(f"Config saved atomically to {self.config_path}")
except Exception as e:
# Clean up temp file on error
try:
os.unlink(temp_path)
except:
pass
raise e
def assign_ports(self, server_files: List[Path], compose_files: List[Path] = None,
starting_port: int = HOST_PORT_MIN,
host_port_min: int = HOST_PORT_MIN,
host_port_max: int = HOST_PORT_MAX,
enable_new: bool = False) -> Dict[str, dict]:
"""
Assign ports to server files and docker-compose files with proper range management.
Preserves enabled status for existing servers, new servers are disabled by default
unless enable_new=True is passed (e.g. via --enable-all flag).
Returns dict mapping path to {"port": int, "enabled": bool}
"""
config = self.load_config()
# Separate servers by type
host_servers = []
for server_file in server_files:
host_servers.append(server_file)
# Get currently used ports
used_ports = set(info['port'] for info in config.values())
# Assign ports to host servers (5000-5099)
next_host_port = starting_port
for server_file in host_servers:
server_str = str(server_file)
if server_str not in config:
# Find next available port in host range
while (next_host_port in used_ports or
next_host_port < host_port_min or
next_host_port > host_port_max):
next_host_port += 1
if next_host_port > host_port_max:
raise RuntimeError(f"No available ports in host range ({host_port_min}-{host_port_max}) for server {server_str}")
config[server_str] = {'port': next_host_port, 'enabled': enable_new}
used_ports.add(next_host_port)
status = "enabled" if enable_new else "disabled - edit config to enable"
logger.info(f"Assigned host port {next_host_port} to {server_str} ({status})")
next_host_port += 1
# Assign ports to docker-compose files
if compose_files:
for compose_file in compose_files:
compose_str = str(compose_file)
if compose_str not in config:
# Find next available port in host range
while (next_host_port in used_ports or
next_host_port < host_port_min or
next_host_port > host_port_max):
next_host_port += 1
if next_host_port > host_port_max:
raise RuntimeError(f"No available ports in host range ({host_port_min}-{host_port_max}) for compose {compose_str}")
config[compose_str] = {'port': next_host_port, 'enabled': enable_new}
used_ports.add(next_host_port)
status = "enabled" if enable_new else "disabled - edit config to enable"
logger.info(f"Assigned host port {next_host_port} to {compose_str} ({status})")
next_host_port += 1
self.save_config(config)
return config
class MCPDeploymentManager:
"""Main deployment manager that orchestrates all components"""
def __init__(self, mcp_dir: str, workspace_dir: str, config_path: str):
self.mcp_dir = Path(mcp_dir)
self.workspace_dir = Path(workspace_dir)
# Generate unique instance ID from workspace path for Docker service isolation
self.instance_id = generate_instance_id(workspace_dir)
# Use instance-specific config file to support multiple concurrent deployments
# Convert "config.json" to "config_${INSTANCE_ID}.json"
if config_path == "config.json":
config_path = f"config_{self.instance_id}.json"
logger.info(f"Using instance-specific config: {config_path}")
self.process_manager = ProcessManager(self.workspace_dir, self.instance_id)
self.config_manager = ConfigManager(config_path)
# Set up signal handlers
signal.signal(signal.SIGINT, self._signal_handler)
signal.signal(signal.SIGTERM, self._signal_handler)
def _signal_handler(self, signum, frame):
"""Handle shutdown signals; exit 1 if any server had been abandoned"""
logger.info(f"Received signal {signum}, shutting down...")
self.process_manager.shutdown()
if self.process_manager.abandoned_servers:
self.process_manager.display_abandoned_summary()
sys.exit(1)
sys.exit(0)
def deploy(self, skip_docker: bool = False, starting_port: int = HOST_PORT_MIN,
host_port_min: int = HOST_PORT_MIN,
host_port_max: int = HOST_PORT_MAX,
enable_all: bool = False):
"""Deploy all MCP servers and Docker services"""
if not self.mcp_dir.exists():
raise FileNotFoundError(f"MCP directory {self.mcp_dir} does not exist")
# Discover all services first
compose_files = ServerDiscovery.find_docker_compose_files(self.mcp_dir)
server_files = ServerDiscovery.find_server_files(self.mcp_dir)
# Assign ports to all services
logger.info("Assigning ports to all services...")
port_config = self.config_manager.assign_ports(server_files, compose_files, starting_port, host_port_min, host_port_max, enable_new=enable_all)
# Start Docker services
if not skip_docker:
self._deploy_docker_services(compose_files, port_config, host_port_min, host_port_max)
# Start MCP servers
self._deploy_mcp_servers(server_files, port_config, host_port_min, host_port_max)
# Monitor processes
logger.info("All services started. Monitoring processes...")
logger.info(f"Workspace directory: {self.workspace_dir}")
logger.info("All MCP servers will create files directly in the workspace directory")
self.process_manager.monitor_processes()
def _deploy_docker_services(self, compose_files: List[Path], port_config: Dict[str, dict],
host_port_min: int = HOST_PORT_MIN, host_port_max: int = HOST_PORT_MAX):
"""Deploy Docker Compose services with assigned ports (only if enabled)"""
if not compose_files:
logger.info("No Docker Compose files found")
return
logger.info(f"Found {len(compose_files)} Docker Compose files")
started_count = 0
disabled_count = 0
config_updated = False
for compose_file in compose_files:
try:
compose_str = str(compose_file)
config_entry = port_config.get(compose_str, {})
if not config_entry.get('enabled', True):
logger.info(f"Skipping disabled Docker service: {compose_str}")
disabled_count += 1
continue
port = config_entry.get('port')
# Check if port is in use
if is_port_in_use(port):
logger.warning(f"Port {port} is already in use for {compose_str}")
# Get all currently used ports in config
used_ports = set(info['port'] for info in port_config.values())
# Find an available port
new_port = find_available_port(host_port_min, host_port_max, used_ports)
if new_port is None:
logger.error(f"No available ports in range {host_port_min}-{host_port_max}")
continue
logger.info(f"Reassigning {compose_str} from port {port} to {new_port}")
port_config[compose_str]['port'] = new_port
port = new_port
config_updated = True
self.process_manager.start_docker_compose(compose_file, port)
started_count += 1
except Exception as e:
logger.error(f"⚠️ Failed to start Docker service {compose_file}: {e}")
# Save config if any ports were reassigned
if config_updated:
logger.info("Updating config.json with new port assignments")
self.config_manager.save_config(port_config)
if started_count > 0:
logger.info(f"Started {started_count} Docker services ({disabled_count} disabled)")
logger.info("Waiting for Docker services to start...")
time.sleep(3)
elif disabled_count > 0:
logger.info(f"⚠️ All {disabled_count} Docker services are disabled. Change config.json to enable.")
def _deploy_mcp_servers(self, server_files: List[Path], port_config: Dict[str, dict],
host_port_min: int = HOST_PORT_MIN, host_port_max: int = HOST_PORT_MAX):
"""Deploy MCP Python servers with assigned ports (only if enabled)"""
if not server_files:
logger.info("⚠️ No MCP server files found")
return
logger.info(f"Found {len(server_files)} MCP servers")
started_count = 0
disabled_count = 0
config_updated = False
for server_file in server_files:
server_str = str(server_file)
config_entry = port_config.get(server_str, {})
if not config_entry.get('enabled', True):
logger.info(f"Skipping disabled MCP server: {server_str}")
disabled_count += 1
continue
port = config_entry.get('port')
# Check if port is in use
if is_port_in_use(port):
logger.warning(f"Port {port} is already in use for {server_str}")
# Get all currently used ports in config
used_ports = set(info['port'] for info in port_config.values())
# Find an available port
new_port = find_available_port(host_port_min, host_port_max, used_ports)
if new_port is None:
logger.error(f"No available ports in range {host_port_min}-{host_port_max}")
continue
logger.info(f"Reassigning {server_str} from port {port} to {new_port}")
port_config[server_str]['port'] = new_port
port = new_port
config_updated = True
try:
self.process_manager.start_python_server(server_file, port)
started_count += 1
except Exception as e:
logger.error(f"Failed to start server {server_file}: {e}")
# Save config if any ports were reassigned
if config_updated:
logger.info("Updating config.json with new port assignments")
self.config_manager.save_config(port_config)
logger.info(f"Started {started_count} MCP servers ({disabled_count} disabled)")
if started_count == 0:
raise Exception("⚠️ No MCP server enabled, change config.json and select MCP servers to enable.")
def main():
parser = argparse.ArgumentParser(description="Deploy MCP servers with centralized workspace file management")
parser.add_argument("--mcp-dir", default=DEFAULT_FOLDER, help=f"MCP servers directory (default: {DEFAULT_FOLDER})")
parser.add_argument("--workspace", default=WORKSPACE_FOLDER, help=f"Workspace directory where all MCP servers will create files (default: {WORKSPACE_FOLDER})")