-
Notifications
You must be signed in to change notification settings - Fork 2k
feat: Add CORS configuration for browser-based MCP clients #1059
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jerome3o-anthropic
wants to merge
1
commit into
main
Choose a base branch
from
jerome/cors-browser-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+39
−0
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Kludex
reviewed
Jul 4, 2025
Comment on lines
+135
to
+142
|
||
# Add CORS middleware to expose Mcp-Session-Id header for browser-based clients | ||
starlette_app.add_middleware( | ||
CORSMiddleware, | ||
allow_origins=["*"], # Allow all origins - adjust as needed for production | ||
allow_methods=["GET", "POST", "DELETE"], # MCP streamable HTTP methods | ||
expose_headers=["Mcp-Session-Id"], | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You need to wrap the whole application instead of using add_middleware
, otherwise 500s don't get in this.
Comment on lines
+723
to
+729
# Add CORS middleware to your Starlette app | ||
app.add_middleware( | ||
CORSMiddleware, | ||
allow_origins=["*"], # Configure appropriately for production | ||
allow_methods=["GET", "POST", "DELETE"], # MCP streamable HTTP methods | ||
expose_headers=["Mcp-Session-Id"], | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggested change
# Add CORS middleware to your Starlette app | |
app.add_middleware( | |
CORSMiddleware, | |
allow_origins=["*"], # Configure appropriately for production | |
allow_methods=["GET", "POST", "DELETE"], # MCP streamable HTTP methods | |
expose_headers=["Mcp-Session-Id"], | |
) | |
# Add CORS middleware to your Starlette app | |
app = CORSMiddleware( | |
app, | |
allow_origins=["*"], # Configure appropriately for production | |
allow_methods=["GET", "POST", "DELETE"], # MCP streamable HTTP methods | |
expose_headers=["Mcp-Session-Id"], | |
) |
- Add CORSMiddleware to streamable HTTP example servers - Configure minimal CORS with Mcp-Session-Id exposed - Add CORS documentation section to README This enables browser-based clients to connect to MCP servers by properly exposing the Mcp-Session-Id header required for session management. Reported-by: Jerome
7a554e7
to
28aaa02
Compare
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Mcp-Session-Id
headerProblem
Browser-based MCP clients cannot access the
Mcp-Session-Id
header from initialization responses due to CORS restrictions. Without this header, they cannot establish sessions with MCP servers.Solution
This PR adds Starlette's
CORSMiddleware
to the example servers and configures it to expose theMcp-Session-Id
header viaexpose_headers
. The configuration is minimal, only allowing the HTTP methods required by the MCP protocol (GET, POST, DELETE).Changes
CORSMiddleware
import and configuration to:examples/servers/simple-streamablehttp/mcp_simple_streamablehttp/server.py
examples/servers/simple-streamablehttp-stateless/mcp_simple_streamablehttp_stateless/server.py
Test plan
Mcp-Session-Id
header from responsesReported-by: Jerome