Files
ghostnet-openclaw/tools/router/app/main.py
T

83 lines
1.3 KiB
Python

import asyncio
from contextlib import asynccontextmanager
from fastapi import FastAPI
from mcp.server.sse import SseServerTransport
from app.config.loader import load_gateway_config
from app.logging.setup import configure_logging
from app.registry.skill_registry import SkillRegistry
from app.services.skill_manager import SkillManager
from app.mcp.server import master_server
from app.mcp.handlers import register_handlers
from app.api.routes import register_routes
configure_logging()
registry = SkillRegistry()
register_handlers(
master_server,
registry
)
skill_manager = SkillManager(
registry
)
@asynccontextmanager
async def lifespan(app):
config = load_gateway_config()
tasks = []
for name, cfg in config.get(
"mcpServers",
{}
).items():
if "url" not in cfg:
continue
tasks.append(
asyncio.create_task(
skill_manager.connect_skill(
name,
cfg["url"]
)
)
)
yield
for task in tasks:
task.cancel()
await asyncio.gather(
*tasks,
return_exceptions=True
)
app = FastAPI(
title="GhostNet-Router",
lifespan=lifespan
)
transport = SseServerTransport(
"/messages"
)
register_routes(
app,
transport,
master_server
)