-
Notifications
You must be signed in to change notification settings - Fork 4k
Allow browser-based MCP clients via CORS and cross-origin bypass #2359
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
base: main
Are you sure you want to change the base?
Changes from 6 commits
6b28af5
e98016c
c9573f2
fe7ecba
057d1a3
1284cb4
c850a88
8b52418
6baed53
fc09a99
b519cfc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,11 +3,13 @@ package http | |
| import ( | ||
| "context" | ||
| "errors" | ||
| "strings" | ||
| "log/slog" | ||
| "net/http" | ||
|
|
||
| ghcontext "github.com/github/github-mcp-server/pkg/context" | ||
| "github.com/github/github-mcp-server/pkg/github" | ||
| "github.com/github/github-mcp-server/pkg/http/headers" | ||
| "github.com/github/github-mcp-server/pkg/http/middleware" | ||
| "github.com/github/github-mcp-server/pkg/http/oauth" | ||
| "github.com/github/github-mcp-server/pkg/inventory" | ||
|
|
@@ -226,7 +228,8 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | |
| mcpHandler := mcp.NewStreamableHTTPHandler(func(_ *http.Request) *mcp.Server { | ||
| return ghServer | ||
| }, &mcp.StreamableHTTPOptions{ | ||
| Stateless: true, | ||
| Stateless: true, | ||
| CrossOriginProtection: h.config.CrossOriginProtection, | ||
| }) | ||
|
|
||
| mcpHandler.ServeHTTP(w, r) | ||
|
|
@@ -412,3 +415,39 @@ func PATScopeFilter(b *inventory.Builder, r *http.Request, fetcher scopes.Fetche | |
|
|
||
| return b | ||
| } | ||
|
|
||
| // corsAllowHeaders is the precomputed Access-Control-Allow-Headers value. | ||
| var corsAllowHeaders = strings.Join([]string{ | ||
| "Content-Type", | ||
| "Mcp-Session-Id", | ||
| "Mcp-Protocol-Version", | ||
| "Last-Event-ID", | ||
| headers.AuthorizationHeader, | ||
| headers.MCPReadOnlyHeader, | ||
| headers.MCPToolsetsHeader, | ||
| headers.MCPToolsHeader, | ||
| headers.MCPExcludeToolsHeader, | ||
| headers.MCPFeaturesHeader, | ||
| headers.MCPLockdownHeader, | ||
| headers.MCPInsidersHeader, | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was actually going to say define this inside the SetCorsHeaders (before returning the closure), so it's defined when you create the middleware and then used each time. Also more importantly, we have a middleware package and this should move there.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks for the feedback, this logic now sits in cors.go and defines the allowed headers inside SetCorsHeaders like you suggested |
||
| }, ", ") | ||
|
|
||
| // SetCorsHeaders is middleware that sets CORS headers to allow browser-based | ||
| // MCP clients to connect from any origin. This is safe because the server | ||
| // authenticates via bearer tokens (not cookies), so cross-origin requests | ||
| // cannot exploit ambient credentials. | ||
| func SetCorsHeaders(h http.Handler) http.Handler { | ||
| return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| w.Header().Set("Access-Control-Allow-Origin", "*") | ||
| w.Header().Set("Access-Control-Allow-Methods", "GET, POST, DELETE, OPTIONS") | ||
| w.Header().Set("Access-Control-Max-Age", "86400") | ||
| w.Header().Set("Access-Control-Expose-Headers", "Mcp-Session-Id, WWW-Authenticate") | ||
| w.Header().Set("Access-Control-Allow-Headers", corsAllowHeaders) | ||
|
|
||
| if r.Method == http.MethodOptions { | ||
| w.WriteHeader(http.StatusOK) | ||
| return | ||
| } | ||
| h.ServeHTTP(w, r) | ||
| }) | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.