docs: Add AWS Cognito resource server requirement and CORS guidance (#2149)

This commit is contained in:
Jeremiah Lowin 2025-10-20 15:28:16 -04:00 committed by GitHub
commit 09e899a699
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 24 additions and 13 deletions

View file

@ -407,6 +407,12 @@ app.mount("/mcp", mcp.http_app()) # Session manager won't initialize
If you're mounting an authenticated MCP server under a path prefix, see [Mounting Authenticated Servers](/deployment/http#mounting-authenticated-servers) for important OAuth routing considerations.
### CORS Middleware
If your FastAPI app uses `CORSMiddleware` and you're mounting an OAuth-protected FastMCP server, avoid adding application-wide CORS middleware. FastMCP and the MCP SDK already handle CORS for OAuth routes, and layering CORS middleware can cause conflicts (such as 404 errors on `.well-known` routes or OPTIONS requests).
If you need CORS on your own FastAPI routes, use the sub-app pattern: mount your API and FastMCP as separate apps, each with their own middleware, rather than adding top-level `CORSMiddleware` to the combined application.
### Combining Lifespans
If your FastAPI app already has a lifespan (for database connections, startup tasks, etc.), you can't simply replace it with the MCP lifespan. Instead, you need to create a new lifespan function that manages both contexts. This ensures that both your app's initialization logic and the MCP server's session manager run properly: