from fastapi import FastAPI, Depends, Request, Response
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from api.routes.events import router as events_router
from core.config import settings
import asyncio
import logging
from fastmcp import FastMCP
from fastmcp.server.openapi import RouteMap, RouteType
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
)
app = FastAPI(
title=settings.PROJECT_NAME,
description=settings.DESCRIPTION,
version=settings.VERSION,
openapi_tags=[
{"name": "events", "description": "Berghain events data"}
],
)
# CORS configuration
app.add_middleware(
CORSMiddleware,
allow_origins=settings.BACKEND_CORS_ORIGINS,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Health check endpoint (no auth required)
@app.get("/", tags=["health"])
async def health():
return {"status": "ok", "message": "Berghain Events API is running"}
# Include API routes without API key dependency
app.include_router(
events_router,
prefix=settings.API_V1_STR
)
async def check_mcp(mcp: FastMCP):
# List the components that were created
tools = await mcp.get_tools()
resources = await mcp.get_resources()
templates = await mcp.get_resource_templates()
print(
f"{len(tools)} Tool(s): {', '.join([t.name for t in tools.values()])}"
)
print(
f"{len(resources)} Resource(s): {', '.join([r.name for r in resources.values()])}"
)
print(
f"{len(templates)} Resource Template(s): {', '.join([t.name for t in templates.values()])}"
)
return mcp
custom_maps = [
RouteMap(methods=["GET"], pattern=r"^/api/v1/year/.*", route_type=RouteType.TOOL),
RouteMap(methods=["GET"], pattern=r"^/api/v1/location/.*", route_type=RouteType.TOOL),
RouteMap(methods=["GET"], pattern=r"^/api/v1/artist/.*", route_type=RouteType.TOOL),
]
if __name__ == "__main__":
async def main():
mcp = FastMCP.from_fastapi(app=app, route_maps=custom_maps)
await check_mcp(mcp)
await mcp.run_async(transport="sse", host="0.0.0.0", port=8000) # Usa la versión async
asyncio.run(main())