Update asgi_app.py
This commit is contained in:
+85
-64
@@ -2,77 +2,28 @@
|
|||||||
ASGI application for Yargı MCP Server
|
ASGI application for Yargı MCP Server
|
||||||
|
|
||||||
This module provides ASGI/HTTP access to the Yargı MCP server,
|
This module provides ASGI/HTTP access to the Yargı MCP server,
|
||||||
allowing it to be deployed as a web service.
|
allowing it to be deployed as a web service with FastAPI wrapper
|
||||||
|
for Stripe webhook integration.
|
||||||
|
|
||||||
Usage:
|
Usage:
|
||||||
uvicorn asgi_app:app --host 0.0.0.0 --port 8000
|
uvicorn asgi_app:app --host 0.0.0.0 --port 8000
|
||||||
|
|
||||||
Or with custom transport:
|
|
||||||
uvicorn asgi_app:sse_app --host 0.0.0.0 --port 8000
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
from fastapi import FastAPI, Request
|
||||||
|
from fastapi.responses import JSONResponse
|
||||||
from starlette.middleware import Middleware
|
from starlette.middleware import Middleware
|
||||||
from starlette.middleware.cors import CORSMiddleware
|
from starlette.middleware.cors import CORSMiddleware
|
||||||
from starlette.requests import Request
|
|
||||||
from starlette.responses import JSONResponse, PlainTextResponse
|
|
||||||
|
|
||||||
# Import the main MCP app
|
# Import the main MCP app
|
||||||
from mcp_factory import create_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()
|
mcp_server = create_app()
|
||||||
|
|
||||||
# Add a health check endpoint
|
|
||||||
@mcp_server.custom_route("/health", methods=["GET"])
|
|
||||||
async def health_check(request: Request) -> JSONResponse:
|
|
||||||
"""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)
|
|
||||||
})
|
|
||||||
|
|
||||||
@mcp_server.custom_route("/", methods=["GET"])
|
|
||||||
async def root(request: Request) -> JSONResponse:
|
|
||||||
"""Root endpoint with service information"""
|
|
||||||
return JSONResponse({
|
|
||||||
"service": "Yargı MCP Server",
|
|
||||||
"description": "MCP server for Turkish legal databases",
|
|
||||||
"endpoints": {
|
|
||||||
"mcp": "/mcp/",
|
|
||||||
"health": "/health",
|
|
||||||
"status": "/status"
|
|
||||||
},
|
|
||||||
"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)",
|
|
||||||
"Bedesten API (Multiple courts)"
|
|
||||||
]
|
|
||||||
})
|
|
||||||
|
|
||||||
@mcp_server.custom_route("/status", methods=["GET"])
|
|
||||||
async def status(request: Request) -> JSONResponse:
|
|
||||||
"""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"
|
|
||||||
})
|
|
||||||
|
|
||||||
# Configure CORS middleware
|
# Configure CORS middleware
|
||||||
cors_origins = os.getenv("ALLOWED_ORIGINS", "*").split(",")
|
cors_origins = os.getenv("ALLOWED_ORIGINS", "*").split(",")
|
||||||
custom_middleware = [
|
custom_middleware = [
|
||||||
@@ -85,19 +36,89 @@ custom_middleware = [
|
|||||||
),
|
),
|
||||||
]
|
]
|
||||||
|
|
||||||
# Import Stripe webhook router
|
# Create FastAPI wrapper application
|
||||||
from stripe_webhook import router as stripe_router
|
app = FastAPI(
|
||||||
|
title="Yargı MCP Server",
|
||||||
|
description="MCP server for Turkish legal databases with JWT authentication",
|
||||||
|
version="0.1.0",
|
||||||
|
middleware=custom_middleware
|
||||||
|
)
|
||||||
|
|
||||||
# Create ASGI apps with different transports
|
# Add Stripe webhook router to FastAPI
|
||||||
|
app.include_router(stripe_router, prefix="/api")
|
||||||
|
|
||||||
# Recommended: Streamable HTTP transport
|
# Create MCP Starlette sub-application
|
||||||
app = mcp_server.http_app(
|
mcp_app = mcp_server.http_app(
|
||||||
path="/mcp",
|
path="/mcp",
|
||||||
middleware=custom_middleware
|
middleware=custom_middleware
|
||||||
)
|
)
|
||||||
|
|
||||||
# Add Stripe webhook router
|
# Mount MCP app as sub-application
|
||||||
app.include_router(stripe_router, prefix="/api")
|
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)
|
# Alternative: SSE transport (for compatibility)
|
||||||
sse_app = mcp_server.http_app(
|
sse_app = mcp_server.http_app(
|
||||||
|
|||||||
Reference in New Issue
Block a user