Update asgi_app.py
This commit is contained in:
+27
-10
@@ -54,13 +54,23 @@ mcp_app = mcp_server.http_app(
|
|||||||
middleware=custom_middleware
|
middleware=custom_middleware
|
||||||
)
|
)
|
||||||
|
|
||||||
# Create FastAPI wrapper application with MCP app's lifespan
|
# Create combined lifespan that handles both HTTP and SSE apps
|
||||||
|
from contextlib import asynccontextmanager
|
||||||
|
|
||||||
|
@asynccontextmanager
|
||||||
|
async def combined_lifespan(app):
|
||||||
|
# Start both MCP apps
|
||||||
|
async with mcp_app.lifespan(app):
|
||||||
|
async with sse_mcp_app.lifespan(app):
|
||||||
|
yield
|
||||||
|
|
||||||
|
# Create FastAPI wrapper application with combined lifespan
|
||||||
app = FastAPI(
|
app = FastAPI(
|
||||||
title="Yargı MCP Server",
|
title="Yargı MCP Server",
|
||||||
description="MCP server for Turkish legal databases with OAuth authentication",
|
description="MCP server for Turkish legal databases with OAuth authentication",
|
||||||
version="0.1.0",
|
version="0.1.0",
|
||||||
middleware=custom_middleware,
|
middleware=custom_middleware,
|
||||||
lifespan=mcp_app.lifespan # Critical: Get lifespan from mcp_app, not mcp_server
|
lifespan=combined_lifespan # Combined lifespan for both HTTP and SSE apps
|
||||||
)
|
)
|
||||||
|
|
||||||
# Add Stripe webhook router to FastAPI
|
# Add Stripe webhook router to FastAPI
|
||||||
@@ -206,7 +216,17 @@ async def mcp_protocol_handler(request: Request):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
# Add custom route to handle /sse requests for Server-Sent Events
|
# Create separate SSE app for legacy clients
|
||||||
|
sse_mcp_app = mcp_server.http_app(
|
||||||
|
path="/",
|
||||||
|
transport="sse",
|
||||||
|
middleware=custom_middleware
|
||||||
|
)
|
||||||
|
|
||||||
|
# Mount SSE app at /sse-server to avoid path conflicts
|
||||||
|
app.mount("/sse-server", sse_mcp_app)
|
||||||
|
|
||||||
|
# Add custom route to handle /sse requests for Server-Sent Events (legacy transport)
|
||||||
@app.api_route("/sse", methods=["GET", "POST", "OPTIONS"])
|
@app.api_route("/sse", methods=["GET", "POST", "OPTIONS"])
|
||||||
@app.api_route("/sse/", methods=["GET", "POST", "OPTIONS"])
|
@app.api_route("/sse/", methods=["GET", "POST", "OPTIONS"])
|
||||||
async def sse_protocol_handler(request: Request):
|
async def sse_protocol_handler(request: Request):
|
||||||
@@ -305,8 +325,8 @@ async def sse_protocol_handler(request: Request):
|
|||||||
elif message["type"] == "http.response.body":
|
elif message["type"] == "http.response.body":
|
||||||
response_parts["body"] += message.get("body", b"")
|
response_parts["body"] += message.get("body", b"")
|
||||||
|
|
||||||
# Call the main MCP app (same as HTTP endpoint)
|
# Call the dedicated SSE app (not the main HTTP app)
|
||||||
await mcp_app(scope, receive, send)
|
await sse_mcp_app(scope, receive, send)
|
||||||
|
|
||||||
# Return the response
|
# Return the response
|
||||||
from starlette.responses import Response
|
from starlette.responses import Response
|
||||||
@@ -316,11 +336,8 @@ async def sse_protocol_handler(request: Request):
|
|||||||
for name, value in response_parts["headers"]:
|
for name, value in response_parts["headers"]:
|
||||||
headers[name.decode()] = value.decode()
|
headers[name.decode()] = value.decode()
|
||||||
|
|
||||||
# Add SSE-specific headers only if not already JSON response
|
# SSE headers are already set by the SSE transport
|
||||||
if headers.get("Content-Type") != "application/json":
|
# Don't override them unless it's a JSON error response
|
||||||
headers["Content-Type"] = "text/event-stream"
|
|
||||||
headers["Cache-Control"] = "no-cache"
|
|
||||||
headers["Connection"] = "keep-alive"
|
|
||||||
|
|
||||||
# Always add CORS header
|
# Always add CORS header
|
||||||
headers["Access-Control-Allow-Origin"] = "*"
|
headers["Access-Control-Allow-Origin"] = "*"
|
||||||
|
|||||||
Reference in New Issue
Block a user