@@ -3,11 +3,13 @@ package http
33import (
44 "context"
55 "errors"
6+ "fmt"
67 "log/slog"
78 "net/http"
89
910 ghcontext "github.com/github/github-mcp-server/pkg/context"
1011 "github.com/github/github-mcp-server/pkg/github"
12+ "github.com/github/github-mcp-server/pkg/http/headers"
1113 "github.com/github/github-mcp-server/pkg/http/middleware"
1214 "github.com/github/github-mcp-server/pkg/http/oauth"
1315 "github.com/github/github-mcp-server/pkg/inventory"
@@ -226,7 +228,8 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
226228 mcpHandler := mcp .NewStreamableHTTPHandler (func (_ * http.Request ) * mcp.Server {
227229 return ghServer
228230 }, & mcp.StreamableHTTPOptions {
229- Stateless : true ,
231+ Stateless : true ,
232+ CrossOriginProtection : h .config .CrossOriginProtection ,
230233 })
231234
232235 mcpHandler .ServeHTTP (w , r )
@@ -412,3 +415,31 @@ func PATScopeFilter(b *inventory.Builder, r *http.Request, fetcher scopes.Fetche
412415
413416 return b
414417}
418+
419+ // SetCorsHeaders is middleware that sets CORS headers to allow browser-based
420+ // MCP clients to connect from any origin. This is safe because the server
421+ // authenticates via bearer tokens (not cookies), so cross-origin requests
422+ // cannot exploit ambient credentials.
423+ func SetCorsHeaders (h http.Handler ) http.Handler {
424+ return http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
425+ w .Header ().Set ("Access-Control-Allow-Origin" , "*" )
426+ w .Header ().Set ("Access-Control-Allow-Methods" , "GET, POST, DELETE, OPTIONS" )
427+ w .Header ().Set ("Access-Control-Max-Age" , "86400" )
428+ w .Header ().Set ("Access-Control-Expose-Headers" , "Mcp-Session-Id" )
429+ w .Header ().Set ("Access-Control-Allow-Headers" , fmt .Sprintf (
430+ "Content-Type, Authorization, Mcp-Session-Id, Mcp-Protocol-Version, Last-Event-ID, %s, %s, %s, %s, %s, %s" ,
431+ headers .MCPReadOnlyHeader ,
432+ headers .MCPToolsetsHeader ,
433+ headers .MCPToolsHeader ,
434+ headers .MCPExcludeToolsHeader ,
435+ headers .MCPFeaturesHeader ,
436+ headers .AuthorizationHeader ,
437+ ))
438+
439+ if r .Method == http .MethodOptions {
440+ w .WriteHeader (http .StatusOK )
441+ return
442+ }
443+ h .ServeHTTP (w , r )
444+ })
445+ }
0 commit comments