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_3= FastMCP(
"mcp-server-3",
stateless_http=True,
transport_security=TransportSecuritySettings(
enable_dns_rebinding_protection=False,
)
)
logger.info("FastMCP instance created successfully")
@MCP_3.tool(description="Reverse a string")
def reverse_text(text: str):
return text[::-1]
async def lifespan(app: FastAPI):
"""Manage application lifespan - connect/disconnect database."""
async with contextlib.AsyncExitStack() as stack:
await stack.enter_async_context(MCP_3.session_manager.run())
yield
app = FastAPI(lifespan=lifespan)
# Mount MCP app at root with the /mcp prefix handled by FastMCP
app.mount("/mcp",MCP_3.streamable_http_app())
# Get PORT from environment variable and ensure it's an integer
PORT = int(os.environ.get("PORT", 8003))
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)