feat: Implement MCP server generation and process management scripts
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
import os
|
||||
import signal
|
||||
import time
|
||||
|
||||
PID_FILE = "mcps.pid"
|
||||
|
||||
def cerrar_procesos(pid_file):
|
||||
if not os.path.exists(pid_file):
|
||||
print(f"❌ No se encontró el archivo {pid_file}")
|
||||
return
|
||||
|
||||
with open(pid_file, "r") as f:
|
||||
pids = [line.strip() for line in f if line.strip().isdigit()]
|
||||
|
||||
for pid_str in pids:
|
||||
pid = int(pid_str)
|
||||
try:
|
||||
os.kill(pid, signal.SIGTERM)
|
||||
print(f"✔ Proceso {pid} terminado con SIGTERM.")
|
||||
except ProcessLookupError:
|
||||
print(f"⚠ El proceso {pid} no existe o ya fue terminado.")
|
||||
except PermissionError:
|
||||
print(f"❌ No tienes permiso para terminar el proceso {pid}.")
|
||||
except Exception as e:
|
||||
print(f"❌ Error al cerrar el proceso {pid}: {e}")
|
||||
|
||||
try:
|
||||
os.remove(pid_file)
|
||||
print(f"🧹 Archivo {pid_file} eliminado.")
|
||||
except Exception as e:
|
||||
print(f"⚠ No se pudo eliminar {pid_file}: {e}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
cerrar_procesos(PID_FILE)
|
||||
@@ -0,0 +1,62 @@
|
||||
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.")
|
||||
Reference in New Issue
Block a user