Route MCP without shadowing site endpoints
This commit is contained in:
@@ -5557,7 +5557,27 @@ async def _web_with_mcp_lifespan(app_instance):
|
||||
|
||||
|
||||
app.router.lifespan_context = _web_with_mcp_lifespan
|
||||
app.mount("/", _mcp_http_app)
|
||||
|
||||
|
||||
class _MCPDispatch:
|
||||
"""Pass only protocol/OAuth URLs to the SDK; preserve FastAPI route ordering."""
|
||||
|
||||
def __init__(self, next_app):
|
||||
self.next_app = next_app
|
||||
|
||||
async def __call__(self, scope, receive, send):
|
||||
path = scope.get("path", "")
|
||||
if scope["type"] == "http" and (
|
||||
path == "/mcp"
|
||||
or path in {"/authorize", "/token", "/register", "/revoke"}
|
||||
or path.startswith("/.well-known/oauth-")
|
||||
):
|
||||
await _mcp_http_app(scope, receive, send)
|
||||
else:
|
||||
await self.next_app(scope, receive, send)
|
||||
|
||||
|
||||
app.add_middleware(_MCPDispatch)
|
||||
|
||||
if __name__ == "__main__":
|
||||
import uvicorn
|
||||
|
||||
Reference in New Issue
Block a user