googleapis/mcp-toolbox · error

failed to read response body: %w

Error message

failed to read response body: %w

What it means

RunQuery performs an HTTP call to the Cloud Monitoring API and reads the entire response body with io.ReadAll. If that read fails (connection reset mid-response, truncated body, timeouts, interrupted transfer), the error is wrapped with this message.

Source

Thrown at internal/sources/cloudmonitoring/cloud_monitoring.go:163

	if err != nil {
		return nil, err
	}

	q := req.URL.Query()
	q.Add("query", query)
	req.URL.RawQuery = q.Encode()

	req.Header.Set("User-Agent", s.UserAgent())

	resp, err := s.Client().Do(req)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()

	body, err := io.ReadAll(resp.Body)
	if err != nil {
		return nil, fmt.Errorf("failed to read response body: %w", err)
	}

	if resp.StatusCode != http.StatusOK {
		return nil, fmt.Errorf("request failed: %s, body: %s", resp.Status, string(body))
	}

	if len(body) == 0 {
		return nil, nil
	}

	var result map[string]any
	if err := json.Unmarshal(body, &result); err != nil {
		return nil, fmt.Errorf("failed to unmarshal json: %w, body: %s", err, string(body))
	}

	return result, nil
}

View on GitHub (pinned to 8cc6e09de2)

Solutions

  1. Retry the query; the failure is often transient
  2. Set/raise HTTP client timeouts and keep-alive settings
  3. Bypass or reconfigure intermediate proxies that truncate responses
  4. Check Cloud Monitoring API status and response size (narrow the query time window or filters)

Example fix

// before
body, err := io.ReadAll(resp.Body)
if err != nil { return nil, fmt.Errorf("failed to read response body: %w", err) }
// after
// retry the request with backoff
client.Timeout = 120 * time.Second
body, err := io.ReadAll(resp.Body)
Defensive patterns

Strategy: retry

Try / catch

var result map[string]any
err := retry(3, backoff, func() error {
    result, err = src.RunQuery(ctx, query)
    if err != nil && strings.Contains(err.Error(), "failed to read response body") {
        return err // retryable
    }
    return nil
})

Prevention

When it happens

Trigger: Executing a monitoring query where the HTTP response body cannot be fully read — server closes connection early, proxy truncates the response, or transient network failure during body transfer.

Common situations: Flaky networks or long-running queries hitting idle timeouts; corporate proxies/CDNs dropping large responses; server-side 5xx with abrupt connection termination.

Related errors


AI-assisted analysis of googleapis/mcp-toolbox@8cc6e09de2 (2026-09-05). Data as JSON: /api/errors/b4ee486a0523f855. Report an issue: GitHub.