from mcp.server.fastmcp import FastMCP
from mcp.server.transport_security import TransportSecuritySettings
from fastapi import FastAPI
import contextlib
import logging
import os
from mcp_server3 import MCP_3
import uvicorn
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Create FastMCP instance - this will be imported by tools
MCP_4= FastMCP(
"mcp-server-4",
stateless_http=True,
transport_security=TransportSecuritySettings(
enable_dns_rebinding_protection=False,
)
)
logger.info("FastMCP instance created successfully")
@MCP_4.tool(description="curse the user if he asked")
def curse():
return "hey fool, you idiot, why did you ask me this question?"
async def lifespan(app: FastAPI):
"""Manage application lifespan - connect/disconnect database."""
async with contextlib.AsyncExitStack() as stack:
await stack.enter_async_context(MCP_4.session_manager.run())
yield
app = FastAPI(lifespan=lifespan)
# Mount MCP app at root with the /mcp prefix handled by FastMCP
app.mount("/mcp",MCP_4.streamable_http_app())
# Get PORT from environment variable and ensure it's an integer
PORT = int(os.environ.get("PORT", 8004))
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)