from fastapi import FastAPI
import contextlib
import logging
import uvicorn
import os
from master_mcp import master_MCP, discover_tools
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
async def lifespan(app: FastAPI):
"""Manage application lifespan - connect/disconnect database."""
# Discover tools from sub-servers
await discover_tools()
async with contextlib.AsyncExitStack() as stack:
await stack.enter_async_context(master_MCP.session_manager.run())
yield
app = FastAPI(lifespan=lifespan)
app.mount("/mcp",master_MCP.streamable_http_app())
# Mount MCP app at root with the /mcp prefix handled by FastMCP
# @app.mount("/mcp", )
# Get PORT from environment variable and ensure it's an integer
PORT = int(os.environ.get("PORT", 8000))
if __name__ == "__main__":
logger.info(f"🚀 Starting MCP MySQL Server on port {PORT}")
logger.info(f"📡 Server endpoint: http://localhost:{PORT}/mcp")
uvicorn.run(app, host="0.0.0.0", port=PORT)