googleapis/mcp-toolbox · error

failed to create http request: %w

Error message

failed to create http request: %w

What it means

FetchQueryStats builds an outbound POST request with http.NewRequestWithContext. If request construction fails (malformed URL, invalid context), this wrapped error is returned before any network call is made.

Source

Thrown at internal/sources/databaseinsights/databaseinsights.go:373

}

// BatchQueryIndexRecommendationsResponse contains index recommendations.
type BatchQueryIndexRecommendationsResponse struct {
	DatabaseIndexRecommendations []DatabaseIndexRecommendation `json:"databaseIndexRecommendations"`
}

// FetchQueryStats executes the FetchQueryStats REST API method.
func (s *Source) FetchQueryStats(ctx context.Context, req *FetchQueryStatsRequest) (*FetchQueryStatsResponse, error) {
	url := fmt.Sprintf("%s/v1beta/%s/queryStats:fetch", s.getEndpointForParent(req.Parent), req.Parent)

	bodyBytes, err := json.Marshal(req)
	if err != nil {
		return nil, fmt.Errorf("failed to marshal request: %w", err)
	}

	httpReq, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewBuffer(bodyBytes))
	if err != nil {
		return nil, fmt.Errorf("failed to create http request: %w", err)
	}
	httpReq.Header.Set("Content-Type", "application/json")

	resp, err := s.httpClient.Do(httpReq)
	if err != nil {
		return nil, fmt.Errorf("failed to execute request: %w", err)
	}
	defer resp.Body.Close()

	if resp.StatusCode != http.StatusOK {
		respBody, _ := io.ReadAll(resp.Body)
		return nil, fmt.Errorf("request failed with status %s: %s", resp.Status, string(respBody))
	}

	var fetchResp FetchQueryStatsResponse
	if err := json.NewDecoder(resp.Body).Decode(&fetchResp); err != nil {
		return nil, fmt.Errorf("failed to decode response: %w", err)
	}

View on GitHub (pinned to 8cc6e09de2)

Solutions

  1. Log/inspect the wrapped error and the full URL being built
  2. Validate s.getEndpointForParent output is a well-formed https:// URL without trailing spaces
  3. Validate req.Parent matches the expected 'projects/*/locations/*/...' resource format
  4. Ensure the parent is URL-escaped if it contains user-provided segments

Example fix

// before
req.Parent = "projects/my project/locations/us" // space breaks URL parsing
// after
req.Parent = "projects/my-project/locations/us"
Defensive patterns

Strategy: validation

Validate before calling

func validParent(p string) bool {
  re := regexp.MustCompile(`^projects/[A-Za-z0-9-]+/locations/[a-z0-9-]+$`)
  return re.MatchString(p)
}
if !validParent(req.Parent) { return errors.New("invalid parent resource name") }

Prevention

When it happens

Trigger: http.NewRequestWithContext fails, almost always because the composed URL '%s/v1beta/%s/queryStats:fetch' is malformed — e.g. an endpoint string containing spaces/invalid characters or an improperly escaped parent resource name.

Common situations: A misconfigured custom endpoint (getEndpointForParent returning a bad base URL); a req.Parent containing characters that break URL parsing; a canceled/invalid context in exotic setups.

Related errors


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