Skip to content

Commit 71ffcb4

Browse files
mcp: add MCPGPDEBUG for opt-in Content-Type check (#1012)
Copy of existing [PR](#972) patching 1.6.0 version
1 parent d9728a8 commit 71ffcb4

4 files changed

Lines changed: 27 additions & 3 deletions

File tree

docs/mcpgodebug.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,15 @@ Options listed below were added and will be removed in the 1.9.0 version of the
5050
restoring the previous behavior. The default behavior was changed so that
5151
the client always attempts to surface the underlying JSON-RPC error.
5252

53+
### 1.6.1
54+
55+
Options listed below were added and will be removed in the 1.8.0 version of the SDK.
56+
57+
- `disablecontenttypecheck` added. If set to `1`, Content-Type validation on
58+
HTTP POST requests will be disabled, allowing requests with non-JSON or missing
59+
Content-Type headers. The default behavior is to validate that HTTP POST
60+
requests have Content-Type: application/json.
61+
5362
### 1.6.0
5463

5564
Options listed below were added and will be removed in the 1.8.0 version of the SDK.

internal/docs/mcpgodebug.src.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,15 @@ Options listed below were added and will be removed in the 1.9.0 version of the
4949
restoring the previous behavior. The default behavior was changed so that
5050
the client always attempts to surface the underlying JSON-RPC error.
5151

52+
### 1.6.1
53+
54+
Options listed below were added and will be removed in the 1.8.0 version of the SDK.
55+
56+
- `disablecontenttypecheck` added. If set to `1`, Content-Type validation on
57+
HTTP POST requests will be disabled, allowing requests with non-JSON or missing
58+
Content-Type headers. The default behavior is to validate that HTTP POST
59+
requests have Content-Type: application/json.
60+
5261
### 1.6.0
5362

5463
Options listed below were added and will be removed in the 1.8.0 version of the SDK.

mcp/sse.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ func (h *SSEHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
202202
}
203203

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

mcp/streamable.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,12 @@ var allowsessionsinstateless = mcpgodebug.Value("allowsessionsinstateless")
271271
// the client always attempts to surface the underlying JSON-RPC error.
272272
var noprotocolerrorbody = mcpgodebug.Value("noprotocolerrorbody")
273273

274+
// disablecontenttypecheck is a compatibility parameter that allows to disable
275+
// Content-Type validation on POST requests.
276+
// See the documentation for the mcpgodebug package for instructions how to enable it.
277+
// The option will be removed in the 1.8.0 version of the SDK.
278+
var disablecontenttypecheck = mcpgodebug.Value("disablecontenttypecheck")
279+
274280
// writeJSONRPCError writes a JSON-RPC error response with the given HTTP
275281
// status code, request ID (may be a zero ID for errors that occur before the
276282
// request body has been parsed), and JSON-RPC error.
@@ -342,7 +348,7 @@ func (h *StreamableHTTPHandler) serveStateless(w http.ResponseWriter, req *http.
342348
return
343349
}
344350

345-
if baseMediaType(req.Header.Get("Content-Type")) != "application/json" {
351+
if disablecontenttypecheck != "1" && baseMediaType(req.Header.Get("Content-Type")) != "application/json" {
346352
http.Error(w, "Content-Type must be 'application/json'", http.StatusUnsupportedMediaType)
347353
return
348354
}
@@ -560,7 +566,7 @@ func (h *StreamableHTTPHandler) serveStatefulDELETE(w http.ResponseWriter, req *
560566
// ID, a new session is created (this is the normal path for the first
561567
// initialize request).
562568
func (h *StreamableHTTPHandler) serveStatefulPOST(w http.ResponseWriter, req *http.Request) {
563-
if baseMediaType(req.Header.Get("Content-Type")) != "application/json" {
569+
if disablecontenttypecheck != "1" && baseMediaType(req.Header.Get("Content-Type")) != "application/json" {
564570
http.Error(w, "Content-Type must be 'application/json'", http.StatusUnsupportedMediaType)
565571
return
566572
}

0 commit comments

Comments
 (0)