|
13 | 13 | from starlette.datastructures import MutableHeaders |
14 | 14 | from starlette.exceptions import HTTPException as StarletteHTTPException |
15 | 15 | from starlette.middleware.cors import CORSMiddleware |
16 | | -from starlette.middleware.base import BaseHTTPMiddleware |
| 16 | +from starlette.types import ASGIApp, Receive, Scope, Send |
17 | 17 |
|
18 | 18 | from alembic import command |
19 | 19 | from apps.api import api_router |
@@ -98,18 +98,21 @@ def custom_generate_unique_id(route: APIRoute) -> str: |
98 | 98 | ) |
99 | 99 |
|
100 | 100 |
|
101 | | -class McpClientIpForwardMiddleware(BaseHTTPMiddleware): |
102 | | - async def dispatch(self, request: Request, call_next): |
103 | | - client_host = request.client.host if request.client else None |
104 | | - if client_host: |
105 | | - headers = MutableHeaders(scope=request.scope) |
106 | | - if not headers.get("x-real-ip"): |
107 | | - headers["x-real-ip"] = client_host |
108 | | - if not headers.get("x-forwarded-for"): |
109 | | - headers["x-forwarded-for"] = client_host |
110 | | - if not headers.get("x-client-ip"): |
111 | | - headers["x-client-ip"] = client_host |
112 | | - return await call_next(request) |
| 101 | +class McpClientIpForwardMiddleware: |
| 102 | + def __init__(self, app: ASGIApp) -> None: |
| 103 | + self.app = app |
| 104 | + |
| 105 | + async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None: |
| 106 | + # MCP SSE uses the ASGI send channel directly. Do not wrap its response |
| 107 | + # in BaseHTTPMiddleware's response-body iterator. |
| 108 | + if scope["type"] == "http": |
| 109 | + client = scope.get("client") |
| 110 | + if client: |
| 111 | + headers = MutableHeaders(scope=scope) |
| 112 | + for name in ("x-real-ip", "x-forwarded-for", "x-client-ip"): |
| 113 | + if not headers.get(name): |
| 114 | + headers[name] = client[0] |
| 115 | + await self.app(scope, receive, send) |
113 | 116 |
|
114 | 117 | # cache docs for different text |
115 | 118 | _openapi_cache: Dict[str, Dict[str, Any]] = {} |
|
0 commit comments