Skip to content

Commit 992df59

Browse files
Fix CORS middleware implementation to wrap entire application
- Change from add_middleware() to CORSMiddleware wrapper pattern - Ensures 500 errors get proper CORS headers for browser clients - Update both streamable HTTP example servers - Fix README documentation to show complete example Reported-by: Jerome
1 parent 7b67d78 commit 992df59

File tree

3 files changed

+15
-9
lines changed
  • examples/servers
    • simple-streamablehttp-stateless/mcp_simple_streamablehttp_stateless
    • simple-streamablehttp/mcp_simple_streamablehttp

3 files changed

+15
-9
lines changed

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -718,11 +718,15 @@ The streamable HTTP transport supports:
718718
If you'd like your server to be accessible by browser-based MCP clients, you'll need to configure CORS headers. The `Mcp-Session-Id` header must be exposed for browser clients to access it:
719719

720720
```python
721+
from starlette.applications import Starlette
721722
from starlette.middleware.cors import CORSMiddleware
722723

723-
# Add CORS middleware to your Starlette app
724-
app = CORSMiddleware(
725-
app,
724+
# Create your Starlette app first
725+
starlette_app = Starlette(routes=[...])
726+
727+
# Then wrap it with CORS middleware
728+
starlette_app = CORSMiddleware(
729+
starlette_app,
726730
allow_origins=["*"], # Configure appropriately for production
727731
allow_methods=["GET", "POST", "DELETE"], # MCP streamable HTTP methods
728732
expose_headers=["Mcp-Session-Id"],

examples/servers/simple-streamablehttp-stateless/mcp_simple_streamablehttp_stateless/server.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,10 @@ async def lifespan(app: Starlette) -> AsyncIterator[None]:
133133
lifespan=lifespan,
134134
)
135135

136-
# Add CORS middleware to expose Mcp-Session-Id header for browser-based clients
137-
starlette_app.add_middleware(
138-
CORSMiddleware,
136+
# Wrap ASGI application with CORS middleware to expose Mcp-Session-Id header
137+
# for browser-based clients (ensures 500 errors get proper CORS headers)
138+
starlette_app = CORSMiddleware(
139+
starlette_app,
139140
allow_origins=["*"], # Allow all origins - adjust as needed for production
140141
allow_methods=["GET", "POST", "DELETE"], # MCP streamable HTTP methods
141142
expose_headers=["Mcp-Session-Id"],

examples/servers/simple-streamablehttp/mcp_simple_streamablehttp/server.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,10 @@ async def lifespan(app: Starlette) -> AsyncIterator[None]:
161161
lifespan=lifespan,
162162
)
163163

164-
# Add CORS middleware to expose Mcp-Session-Id header for browser-based clients
165-
starlette_app.add_middleware(
166-
CORSMiddleware,
164+
# Wrap ASGI application with CORS middleware to expose Mcp-Session-Id header
165+
# for browser-based clients (ensures 500 errors get proper CORS headers)
166+
starlette_app = CORSMiddleware(
167+
starlette_app,
167168
allow_origins=["*"], # Allow all origins - adjust as needed for production
168169
allow_methods=["GET", "POST", "DELETE"], # MCP streamable HTTP methods
169170
expose_headers=["Mcp-Session-Id"],

0 commit comments

Comments
 (0)