@@ -29,6 +29,19 @@ type responseWriter struct {
2929 statusCode int
3030}
3131
32+ func (w * responseWriter ) WriteHeader (statusCode int ) {
33+ w .statusCode = statusCode
34+ w .ResponseWriter .WriteHeader (statusCode )
35+ }
36+
37+ func mcpHTTPServerAddr (port int ) string {
38+ return fmt .Sprintf ("127.0.0.1:%d" , port )
39+ }
40+
41+ func mcpHTTPServerDisplayURL (port int ) string {
42+ return fmt .Sprintf ("http://127.0.0.1:%d" , port )
43+ }
44+
3245func loggingHandler (handler http.Handler ) http.Handler {
3346 return http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
3447 start := time .Now ()
@@ -69,21 +82,26 @@ func runHTTPServer(server *mcp.Server, port int) error {
6982 handler := mcp .NewStreamableHTTPHandler (func (req * http.Request ) * mcp.Server {
7083 return server
7184 }, & mcp.StreamableHTTPOptions {
72- SessionTimeout : 2 * time .Hour , // Close idle sessions after 2 hours
73- Logger : logger .NewSlogLoggerWithHandler (mcpLog ),
85+ SessionTimeout : 2 * time .Hour , // Close idle sessions after 2 hours
86+ DisableLocalhostProtection : false , // Keep the SDK's localhost Host-header checks enabled.
87+ Logger : logger .NewSlogLoggerWithHandler (mcpLog ),
7488 })
7589
7690 handlerWithLogging := loggingHandler (handler )
7791
78- // Create HTTP server
79- addr := fmt .Sprintf (":%d" , port )
92+ // Bind to loopback only. Since v1.6.0, the SDK no longer enables
93+ // cross-origin protection by default, so binding to 127.0.0.1 (instead
94+ // of all interfaces) is the safest default for a local MCP server — it
95+ // prevents the server from being reachable on non-localhost interfaces.
96+ // The SDK's localhost Host-header checks stay enabled as a second layer.
97+ addr := mcpHTTPServerAddr (port )
8098 httpServer := & http.Server {
8199 Addr : addr ,
82100 Handler : handlerWithLogging ,
83101 ReadHeaderTimeout : MCPServerHTTPTimeout ,
84102 }
85103
86- fmt .Fprintln (os .Stderr , console .FormatInfoMessage ("Starting MCP server on http://localhost" + addr ))
104+ fmt .Fprintln (os .Stderr , console .FormatInfoMessage ("Starting MCP server on " + mcpHTTPServerDisplayURL ( port ) ))
87105 mcpLog .Printf ("HTTP server listening on %s" , addr )
88106
89107 // Run the HTTP server
0 commit comments