""" ASGI application for Yargı MCP Server This module provides ASGI/HTTP access to the Yargı MCP server, allowing it to be deployed as a web service with FastAPI wrapper for Stripe webhook integration. Usage: uvicorn asgi_app:app --host 0.0.0.0 --port 8000 """ import os from fastapi import FastAPI, Request from fastapi.responses import JSONResponse from starlette.middleware import Middleware from starlette.middleware.cors import CORSMiddleware # Import the main MCP app from mcp_factory import create_app # Import Stripe webhook router from stripe_webhook import router as stripe_router # Create MCP server instance mcp_server = create_app() # Configure CORS middleware cors_origins = os.getenv("ALLOWED_ORIGINS", "*").split(",") custom_middleware = [ Middleware( CORSMiddleware, allow_origins=cors_origins, allow_credentials=True, allow_methods=["GET", "POST", "OPTIONS"], allow_headers=["Content-Type", "Authorization", "X-Request-ID"], ), ] # Create FastAPI wrapper application app = FastAPI( title="Yargı MCP Server", description="MCP server for Turkish legal databases with JWT authentication", version="0.1.0", middleware=custom_middleware ) # Add Stripe webhook router to FastAPI app.include_router(stripe_router, prefix="/api") # Create MCP Starlette sub-application mcp_app = mcp_server.http_app( path="/mcp", middleware=custom_middleware ) # Mount MCP app as sub-application app.mount("/mcp", mcp_app) # FastAPI health check endpoint @app.get("/health") async def health_check(): """Health check endpoint for monitoring""" return JSONResponse({ "status": "healthy", "service": "Yargı MCP Server", "version": "0.1.0", "tools_count": len(mcp_server._tool_manager._tools), "auth_enabled": os.getenv("ENABLE_AUTH", "false").lower() == "true" }) # FastAPI root endpoint @app.get("/") async def root(): """Root endpoint with service information""" return JSONResponse({ "service": "Yargı MCP Server", "description": "MCP server for Turkish legal databases with JWT authentication", "endpoints": { "mcp": "/mcp/", "health": "/health", "status": "/status", "stripe_webhook": "/api/stripe/webhook" }, "supported_databases": [ "Yargıtay (Court of Cassation)", "Danıştay (Council of State)", "Emsal (Precedent)", "Uyuşmazlık Mahkemesi (Court of Jurisdictional Disputes)", "Anayasa Mahkemesi (Constitutional Court)", "Kamu İhale Kurulu (Public Procurement Authority)", "Rekabet Kurumu (Competition Authority)", "Sayıştay (Court of Accounts)", "Bedesten API (Multiple courts)" ], "authentication": { "enabled": os.getenv("ENABLE_AUTH", "false").lower() == "true", "type": "JWT Bearer Token", "issuer": os.getenv("CLERK_ISSUER", "https://clerk.accounts.dev"), "required_scopes": ["yargi.read"] } }) # FastAPI status endpoint @app.get("/status") async def status(): """Status endpoint with detailed information""" tools = [] for tool in mcp_server._tool_manager._tools.values(): tools.append({ "name": tool.name, "description": tool.description[:100] + "..." if len(tool.description) > 100 else tool.description }) return JSONResponse({ "status": "operational", "tools": tools, "total_tools": len(tools), "transport": "streamable_http", "architecture": "FastAPI wrapper + MCP Starlette sub-app", "auth_status": "enabled" if os.getenv("ENABLE_AUTH", "false").lower() == "true" else "disabled" }) # Alternative: SSE transport (for compatibility) sse_app = mcp_server.http_app( path="/sse", transport="sse", middleware=custom_middleware ) # Export for uvicorn __all__ = ["app", "sse_app"]