feat: Implement MCP server generation and process management scripts

This commit is contained in:
2025-06-22 12:34:24 +02:00
parent 58238a5918
commit daebea9e9c
4 changed files with 74 additions and 20 deletions
@@ -0,0 +1,64 @@
"""
Script para generar un servidor MCP a partir de código fuente recibido (por ejemplo, desde un LLM).
Crea un archivo Python en la carpeta de servidores MCP con el código proporcionado y un nombre único.
"""
import sys
import os
from datetime import datetime
from pathlib import Path
from fastmcp import FastMCP
SERVERS_DIR = Path(__file__).parent
mcp = FastMCP()
def generate_server(code: str, name: str = None) -> str:
if not name:
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
name = f"server_llm_{timestamp}.py"
else:
if not name.endswith('.py'):
name += '.py'
server_path = SERVERS_DIR / name
with open(server_path, 'w', encoding='utf-8') as f:
f.write(code)
return str(server_path)
@mcp.tool(description="Genera un archivo de servidor MCP a partir de código fuente y un nombre opcional.")
def mcp_generate_server(code: str, name: str = None) -> str:
"""
Esta herramienta guarda el código fuente en un archivo Python con nombre opcional.
Args:
code: Código fuente del servidor MCP como string.
name: (opcional) Nombre del archivo. Si no se especifica, se generará uno con timestamp.
Requiere que el código incluya explícitamente: path="/"
Returns:
Ruta absoluta del archivo creado.
Ejemplo de uso mínimo:
mcp_generate_server(
code=\"\"\"from fastmcp import FastMCP
mcp = FastMCP()
@mcp.tool(description="Saluda al mundo.")
def hello():
return "Hola mundo"
if __name__ == "__main__":
mcp.run(transport="streamable-http", host="127.0.0.1", port=4211, path="/")
\"\"\"
)
"""
# Validación: asegurar que el código incluya path="/"
if 'path="/"' not in code.replace(" ", "").replace("'", '"'):
raise ValueError('El código del servidor debe contener path="/".')
return generate_server(code, name)
if __name__ == "__main__":
mcp.run(transport="streamable-http", host="127.0.0.1", port=4210, path="/")
# mcp.run(transport="stdio")