googleapis/mcp-toolbox · error

failed to create HTTP request: %w

Error message

failed to create HTTP request: %w

What it means

This error wraps a failure from http.NewRequestWithContext inside ExecuteMQL, which sends an MQL (MongoDB Query Language) pipeline to the Firestore executePipeline REST endpoint. It means the HTTP request object could not be constructed before any network traffic occurred — typically a malformed URL or an invalid body reader. Since the request never left the process, no server-side status or response exists.

Source

Thrown at internal/sources/firestore/firestore.go:917

							"args": []map[string]any{
								{
									"stringValue": mqlQuery,
								},
							},
						},
					},
				},
			},
		}
		bodyBytes, err = json.Marshal(payload)
		if err != nil {
			return nil, fmt.Errorf("failed to marshal pipeline payload: %w", err)
		}
	}

	req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(bodyBytes))
	if err != nil {
		return nil, fmt.Errorf("failed to create HTTP request: %w", err)
	}
	req.Header.Set("Content-Type", "application/json")
	req.Header.Set("User-Agent", userAgent)
	req.Header.Set("x-goog-request-params", fmt.Sprintf("project_id=%s&database_id=%s", s.GetProjectId(), s.GetDatabaseId()))
	req.Header.Set("x-goog-firestore-api-requester", "querydata")

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

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

	if resp.StatusCode < 200 || resp.StatusCode >= 300 {

View on GitHub (pinned to 8cc6e09de2)

Solutions

  1. Print/inspect the composed url just before http.NewRequestWithContext and verify the project and database IDs are valid (non-empty, URL-safe)
  2. Check the context passed to ExecuteMQL is live (not already cancelled/expired) at call time
  3. Log the wrapped err (%w) to see the underlying url.Parse error and fix the offending component

Example fix

// before
client.ExecuteMQL(ctx, pipeline) // ctx already cancelled upstream
// after
if err := ctx.Err(); err != nil { return err }
result, err := client.ExecuteMQL(ctx, pipeline)
Defensive patterns

Strategy: validation

Validate before calling

if ctx.Err() != nil { return fmt.Errorf("context already done: %w", ctx.Err()) }
if project == "" || database == "" { return fmt.Errorf("project and database must be set") }
// url is built internally; ensure IDs are URL-safe:

Try / catch

res, err := src.ExecuteMQL(ctx, pipeline)
if err != nil && strings.Contains(err.Error(), "failed to create HTTP request") {
    log.Printf("request construction failed (check URL/ctx): %v", err)
}

Prevention

When it happens

Trigger: http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(bodyBytes)) returns non-nil err — practically only when the composed executePipeline URL (built from project/database IDs) fails to parse, or the context passed to ExecuteMQL is already cancelled/deadlined.

Common situations: A misconfigured project or database id producing an invalid URL (e.g. empty project id, illegal characters); passing a ctx that was cancelled before the call; URL-encoding bugs in the endpoint assembly.

Related errors


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