MCPcopy Create free account
hub / github.com/modelcontextprotocol/go-sdk / checkResponse

Method checkResponse

mcp/streamable.go:2048–2071  ·  view source on GitHub ↗

checkResponse checks the status code of the provided response, and translates it into an error if the request was unsuccessful. The response body is close if a non-nil error is returned.

(requestSummary string, resp *http.Response)

Source from the content-addressed store, hash-verified

2046//
2047// The response body is close if a non-nil error is returned.
2048func (c *streamableClientConn) checkResponse(requestSummary string, resp *http.Response) (err error) {
2049 defer func() {
2050 if err != nil {
2051 resp.Body.Close()
2052 }
2053 }()
2054 // §2.5.3: "The server MAY terminate the session at any time, after
2055 // which it MUST respond to requests containing that session ID with HTTP
2056 // 404 Not Found."
2057 if resp.StatusCode == http.StatusNotFound {
2058 // Return an ErrSessionMissing to avoid sending a redundant DELETE when the
2059 // session is already gone.
2060 return fmt.Errorf("%s: failed to connect (session ID: %v): %w", requestSummary, c.sessionID, ErrSessionMissing)
2061 }
2062 // Transient server errors (502, 503, 504, 429) should not break the connection.
2063 // Wrap them with ErrRejected so the jsonrpc2 layer doesn't set writeErr.
2064 if isTransientHTTPStatus(resp.StatusCode) {
2065 return fmt.Errorf("%w: %s: %v", jsonrpc2.ErrRejected, requestSummary, http.StatusText(resp.StatusCode))
2066 }
2067 if resp.StatusCode < 200 || resp.StatusCode >= 300 {
2068 return fmt.Errorf("%s: %v", requestSummary, http.StatusText(resp.StatusCode))
2069 }
2070 return nil
2071}
2072
2073// processStream reads from a single response body, sending events to the
2074// incoming channel. It returns the ID of the last processed event and a flag

Callers 3

connectStandaloneSSEMethod · 0.95
WriteMethod · 0.95
handleSSEMethod · 0.95

Calls 2

isTransientHTTPStatusFunction · 0.85
CloseMethod · 0.65

Tested by

no test coverage detected