from mcp.server.fastmcp import FastMCP
from mcp.server.transport_security import TransportSecuritySettings
from fastapi import FastAPI
import contextlib
import logging
import os
import uvicorn
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Create FastMCP instance - this will be imported by tools
MCP_2 = FastMCP(
"mcp-server-2",
stateless_http=True,
transport_security=TransportSecuritySettings(
enable_dns_rebinding_protection=False,
)
)
logger.info("FastMCP instance created successfully")
@MCP_2.tool(description="Add two numbers")
def add(a: float, b: float):
return a + b
async def lifespan(app: FastAPI):
"""Manage application lifespan - connect/disconnect database."""
async with contextlib.AsyncExitStack() as stack:
await stack.enter_async_context(MCP_2.session_manager.run())
yield
app = FastAPI(lifespan=lifespan)
# Mount MCP app at root with the /mcp prefix handled by FastMCP
app.mount("/mcp",MCP_2.streamable_http_app())
# Get PORT from environment variable and ensure it's an integer
PORT = int(os.environ.get("PORT", 8002))
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)