Skip to content

Commit 3f3b699

Browse files
refactor: remove legacy MCPGODEBUG compatibility for new protocol release (#1237)
1 parent 830f0b7 commit 3f3b699

5 files changed

Lines changed: 41 additions & 42 deletions

File tree

‎docs/mcpgodebug.md‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,23 @@ Options listed below were added and will be removed in the 1.9.0 version of the
4444
soon as its context is cancelled and cannot be delayed by a slow or
4545
unresponsive peer. See issue #1150.
4646

47+
Options below were removed, according to plan:
48+
49+
- `seterroroverwrite`. `SetError` now always preserves existing `Content`.
50+
51+
- `enableoriginverification`. A nil
52+
`StreamableHTTPOptions.CrossOriginProtection` now always means no
53+
cross-origin protection. Wrap the handler with cross-origin protection
54+
middleware instead.
55+
56+
- `disablecontenttypecheck`. Content-Type validation on HTTP POST requests can
57+
no longer be disabled.
58+
59+
- `disablelocalhostprotection`, whose removal had been postponed to this
60+
release. Set the `DisableLocalhostProtection` field in
61+
`StreamableHTTPOptions` or `SSEOptions` to opt out of DNS rebinding
62+
protection.
63+
4764
### 1.7.0
4865

4966
Options listed below were added and will be removed in the 1.9.0 version of the SDK.

‎internal/docs/mcpgodebug.src.md‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,23 @@ Options listed below were added and will be removed in the 1.9.0 version of the
4343
soon as its context is cancelled and cannot be delayed by a slow or
4444
unresponsive peer. See issue #1150.
4545

46+
Options below were removed, according to plan:
47+
48+
- `seterroroverwrite`. `SetError` now always preserves existing `Content`.
49+
50+
- `enableoriginverification`. A nil
51+
`StreamableHTTPOptions.CrossOriginProtection` now always means no
52+
cross-origin protection. Wrap the handler with cross-origin protection
53+
middleware instead.
54+
55+
- `disablecontenttypecheck`. Content-Type validation on HTTP POST requests can
56+
no longer be disabled.
57+
58+
- `disablelocalhostprotection`, whose removal had been postponed to this
59+
release. Set the `DisableLocalhostProtection` field in
60+
`StreamableHTTPOptions` or `SSEOptions` to opt out of DNS rebinding
61+
protection.
62+
4663
### 1.7.0
4764

4865
Options listed below were added and will be removed in the 1.9.0 version of the SDK.

‎mcp/protocol.go‎

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -329,23 +329,13 @@ type CallToolResult struct {
329329
err error
330330
}
331331

332-
// seterroroverwrite is a compatibility parameter that restores the pre-1.6.0
333-
// behavior of [CallToolResult.SetError], where Content was always overwritten
334-
// with the error text. See the documentation for the mcpgodebug package for
335-
// instructions on how to enable it.
336-
// The option will be removed in the 1.8.0 version of the SDK.
337-
var seterroroverwrite = mcpgodebug.Value("seterroroverwrite")
338-
339332
// SetError sets the error for the tool result and sets IsError to true.
340333
// If Content has not already been populated, it is set to the error text.
341334
// If Content has already been populated, it is left unchanged, allowing callers
342335
// to provide a user-friendly message while still recording the underlying error
343336
// for inspection via [GetError] in server middleware.
344-
//
345-
// To restore the previous behavior where Content was always overwritten,
346-
// set MCPGODEBUG=seterroroverwrite=1.
347337
func (r *CallToolResult) SetError(err error) {
348-
if len(r.Content) == 0 || seterroroverwrite == "1" {
338+
if len(r.Content) == 0 {
349339
r.Content = []Content{&TextContent{Text: err.Error()}}
350340
}
351341
r.IsError = true

‎mcp/sse.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ func (t *SSEServerTransport) SupportsProtocolVersion(version string) bool {
225225
func (h *SSEHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
226226
// DNS rebinding protection: auto-enabled for localhost servers.
227227
// See: https://modelcontextprotocol.io/specification/2025-11-25/basic/security_best_practices#local-mcp-server-compromise
228-
if !h.opts.DisableLocalhostProtection && disablelocalhostprotection != "1" {
228+
if !h.opts.DisableLocalhostProtection {
229229
if localAddr, ok := req.Context().Value(http.LocalAddrContextKey).(net.Addr); ok && localAddr != nil {
230230
if util.IsLoopback(localAddr.String()) && !util.IsLoopback(req.Host) {
231231
http.Error(w, fmt.Sprintf("Forbidden: invalid Host header %q", req.Host), http.StatusForbidden)
@@ -235,7 +235,7 @@ func (h *SSEHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
235235
}
236236

237237
// Validate 'Content-Type' header.
238-
if disablecontenttypecheck != "1" && req.Method == http.MethodPost {
238+
if req.Method == http.MethodPost {
239239
mediaType, _, err := mime.ParseMediaType(req.Header.Get("Content-Type"))
240240
if err != nil || mediaType != "application/json" {
241241
http.Error(w, "Content-Type must be 'application/json'", http.StatusUnsupportedMediaType)

‎mcp/streamable.go‎

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,7 @@ type StreamableHTTPOptions struct {
184184
// CrossOriginProtection allows to customize cross-origin protection.
185185
// The deny handler set in the CrossOriginProtection through SetDenyHandler
186186
// is ignored.
187-
// If nil, no cross-origin protection is applied. Use the `enableoriginverification`
188-
// MCPGODEBUG compatibility parameter to enable the default protection until v1.8.0.
187+
// If nil, no cross-origin protection is applied.
189188
//
190189
// Deprecated: wrap the handler with cross-origin protection middleware
191190
// instead. For example:
@@ -240,10 +239,6 @@ func NewStreamableHTTPHandler(getServer func(*http.Request) *Server, opts *Strea
240239

241240
h.opts.Logger = ensureLogger(h.opts.Logger)
242241

243-
if h.opts.CrossOriginProtection == nil && enableoriginverification == "1" {
244-
h.opts.CrossOriginProtection = &http.CrossOriginProtection{}
245-
}
246-
247242
if h.opts.MaxRequestBodyBytes == 0 {
248243
h.opts.MaxRequestBodyBytes = DefaultMaxRequestBodyBytes
249244
}
@@ -274,20 +269,6 @@ func (h *StreamableHTTPHandler) closeAll() {
274269
}
275270
}
276271

277-
// disablelocalhostprotection is a compatibility parameter that allows to disable
278-
// DNS rebinding protection, which was added in the 1.4.0 version of the SDK.
279-
// See the documentation for the mcpgodebug package for instructions how to enable it.
280-
// The option will be removed in the 1.8.0 version of the SDK.
281-
var disablelocalhostprotection = mcpgodebug.Value("disablelocalhostprotection")
282-
283-
// enableoriginverification is a compatibility parameter that restores the
284-
// default cross-origin protection behavior from v1.4.1-v1.5.0. When set to
285-
// "1", a zero-value CrossOriginProtection will be applied if none is
286-
// explicitly provided in StreamableHTTPOptions.
287-
// See the documentation for the mcpgodebug package for instructions how to enable it.
288-
// The option will be removed in the 1.8.0 version of the SDK.
289-
var enableoriginverification = mcpgodebug.Value("enableoriginverification")
290-
291272
// allowsessionsinstateless is a compatibility parameter that restores the old
292273
// behavior of reading and using Mcp-Session-Id headers in stateless mode. When
293274
// set to "1", stateless servers will read the session ID from the request
@@ -308,12 +289,6 @@ var allowsessionsinstateless = mcpgodebug.Value("allowsessionsinstateless")
308289
// permanently fails the connection.
309290
var noprotocolerrorbody = mcpgodebug.Value("noprotocolerrorbody")
310291

311-
// disablecontenttypecheck is a compatibility parameter that allows to disable
312-
// Content-Type validation on POST requests.
313-
// See the documentation for the mcpgodebug package for instructions how to enable it.
314-
// The option will be removed in the 1.8.0 version of the SDK.
315-
var disablecontenttypecheck = mcpgodebug.Value("disablecontenttypecheck")
316-
317292
// plaintextstatefulrejection is a compatibility parameter that restores the
318293
// previous behavior of a stateful [StreamableHTTPHandler] when it receives a
319294
// request carrying SEP-2575 per-request metadata (i.e. an
@@ -341,7 +316,7 @@ func writeJSONRPCError(w http.ResponseWriter, status int, id jsonrpc.ID, jerr *j
341316
func (h *StreamableHTTPHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
342317
// DNS rebinding protection: auto-enabled for localhost servers.
343318
// See: https://modelcontextprotocol.io/specification/2025-11-25/basic/security_best_practices#local-mcp-server-compromise
344-
if !h.opts.DisableLocalhostProtection && disablelocalhostprotection != "1" {
319+
if !h.opts.DisableLocalhostProtection {
345320
if localAddr, ok := req.Context().Value(http.LocalAddrContextKey).(net.Addr); ok && localAddr != nil {
346321
if util.IsLoopback(localAddr.String()) && !util.IsLoopback(req.Host) {
347322
http.Error(w, fmt.Sprintf("Forbidden: invalid Host header %q", req.Host), http.StatusForbidden)
@@ -410,7 +385,7 @@ func (h *StreamableHTTPHandler) serveStateless(w http.ResponseWriter, req *http.
410385
return
411386
}
412387

413-
if disablecontenttypecheck != "1" && baseMediaType(req.Header.Get("Content-Type")) != "application/json" {
388+
if baseMediaType(req.Header.Get("Content-Type")) != "application/json" {
414389
http.Error(w, "Content-Type must be 'application/json'", http.StatusUnsupportedMediaType)
415390
return
416391
}
@@ -644,7 +619,7 @@ func (h *StreamableHTTPHandler) serveStatefulDELETE(w http.ResponseWriter, req *
644619
// ID, a new session is created (this is the normal path for the first
645620
// initialize request).
646621
func (h *StreamableHTTPHandler) serveStatefulPOST(w http.ResponseWriter, req *http.Request) {
647-
if disablecontenttypecheck != "1" && baseMediaType(req.Header.Get("Content-Type")) != "application/json" {
622+
if baseMediaType(req.Header.Get("Content-Type")) != "application/json" {
648623
http.Error(w, "Content-Type must be 'application/json'", http.StatusUnsupportedMediaType)
649624
return
650625
}

0 commit comments

Comments
 (0)