from mcp.server.fastmcp import FastMCP
from mcp.server.transport_security import TransportSecuritySettings
from fastapi import FastAPI
import contextlib
import os
import logging
import uvicorn
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Create FastMCP instance - this will be imported by tools
MCP_1 = FastMCP(
"mcp-server-1",
stateless_http=True,
transport_security=TransportSecuritySettings(
enable_dns_rebinding_protection=False,
)
)
logger.info("FastMCP instance created successfully")
@MCP_1.tool(description="Greet a user by name")
def greet(name: str = "there"):
return f"Hello, {name}! — from MCP Server 1"
async def lifespan(app: FastAPI):
"""Manage application lifespan - connect/disconnect database."""
async with contextlib.AsyncExitStack() as stack:
await stack.enter_async_context(MCP_1.session_manager.run())
yield
app = FastAPI(lifespan=lifespan)
# Mount MCP app at root with the /mcp prefix handled by FastMCP
app.mount("/mcp",MCP_1.streamable_http_app())
# Get PORT from environment variable and ensure it's an integer
PORT = int(os.environ.get("PORT", 8001))
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)
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)