63 lines
1.8 KiB
Python
63 lines
1.8 KiB
Python
import os
|
|
import subprocess
|
|
import sys
|
|
import re
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parent
|
|
PYTHON_PATH = (ROOT.parent / ROOT.parent / ".venv" / "Scripts" / "python.exe").resolve()
|
|
PID_FILE = ROOT / "mcps.pid"
|
|
|
|
# Carpeta de MCPs
|
|
MCPS_DIR = (ROOT / "../domains/Llms/MCPs/McpServers").resolve()
|
|
|
|
print("🚀 Iniciando todos los MCPs en el directorio:", MCPS_DIR)
|
|
|
|
# Verificar intérprete Python
|
|
if not PYTHON_PATH.exists():
|
|
print(f"❌ No se encontró Python en: {PYTHON_PATH}")
|
|
sys.exit(1)
|
|
|
|
# Limpiar archivo de PIDs previo
|
|
if PID_FILE.exists():
|
|
PID_FILE.unlink()
|
|
|
|
# Expresión regular para capturar la URL de uvicorn
|
|
URL_REGEX = re.compile(r"Uvicorn running on (?P<url>http://[^\s]+)")
|
|
|
|
# Ejecutar todos los archivos .py en MCPS_DIR
|
|
for script_path in sorted(MCPS_DIR.glob("*.py")):
|
|
if script_path.name.startswith("__"):
|
|
continue # Evita archivos como __init__.py
|
|
|
|
try:
|
|
proc = subprocess.Popen(
|
|
[str(PYTHON_PATH), str(script_path)],
|
|
stdout=subprocess.PIPE,
|
|
stderr=subprocess.STDOUT,
|
|
text=True
|
|
)
|
|
|
|
# Leer la salida para encontrar la URL
|
|
url = None
|
|
for line in proc.stdout:
|
|
match = URL_REGEX.search(line)
|
|
if match:
|
|
url = match.group("url")
|
|
break
|
|
|
|
# Guardar el PID
|
|
with open(PID_FILE, "a", encoding="utf-8") as f:
|
|
f.write(str(proc.pid) + "\n")
|
|
|
|
print(f"→ {script_path.relative_to(ROOT)} lanzado con PID {proc.pid}")
|
|
if url:
|
|
print(f" 🛰️ Servidor accesible en: {url}")
|
|
else:
|
|
print(" ⚠ No se detectó URL del servidor.")
|
|
|
|
except Exception as e:
|
|
print(f"❌ Error al lanzar {script_path.name}:\n{e}")
|
|
|
|
print("✅ Todos los scripts han sido procesados. PIDs guardados en mcps.pid.")
|