mcp: fix connectStandaloneSSE checking Content-Type header (#736)

Issue encountered: the MCP server request was unexpectedly redirected to
an HTML page. Because the client didn’t validate the response
Content-Type, it still entered handleSSE, which then kept treating the
HTML as an SSE stream and repeatedly re-requesting the same
page—creating an infinite request loop.
This commit is contained in:
Liu Shuang 2026-01-05 05:16:06 +08:00 • committed by GitHub
commit e66c23fae4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1555,6 +1555,14 @@ func (c *streamableClientConn) connectStandaloneSSE() {
resp.Body.Close()
return
}
if resp.Header.Get("Content-Type") != "text/event-stream" {
// modelcontextprotocol/go-sdk#736: some servers return 200 OK or redirect with
// non-SSE content type instead of text/event-stream for the standalone
// SSE stream.
c.logger.Warn(fmt.Sprintf("got Content-Type %s instead of text/event-stream for standalone SSE stream", resp.Header.Get("Content-Type")))
resp.Body.Close()
return
}
if resp.StatusCode >= 400 && resp.StatusCode < 500 && !c.strict {
// modelcontextprotocol/go-sdk#393,#610: some servers return NotFound or
// other status codes instead of MethodNotAllowed for the standalone SSE