googleapis/mcp-toolbox · error

error building req for endpoint [%v] : %v

Error message

error building req for endpoint [%v] : %v

What it means

doLogin returns this error when http.NewRequest fails to construct the POST request to the Dgraph /login endpoint. http.NewRequest validates the URL and method; the most common cause is an invalid hc.baseUrl (unparseable or unsupported scheme) combined via getUrl. The failing URL is embedded in the message via [%v] to aid diagnosis.

Source

Thrown at internal/sources/dgraph/dgraph.go:299

	credentials := map[string]interface{}{
		"refreshJWT": hc.RefreshToken,
		"namespace":  hc.Namespace,
	}
	return hc.doLogin(credentials)
}

func (hc *DgraphClient) doLogin(creds map[string]interface{}) error {
	url, err := getUrl(hc.baseUrl, "/login", nil)
	if err != nil {
		return err
	}
	payload, err := json.Marshal(creds)
	if err != nil {
		return fmt.Errorf("failed to marshal credentials: %v", err)
	}
	req, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(payload))
	if err != nil {
		return fmt.Errorf("error building req for endpoint [%v] : %v", url, err)
	}
	req.Header.Add("Content-Type", "application/json")
	if hc.apiKey != "" {
		req.Header.Set("Dg-Auth", hc.apiKey)
	}

	resp, err := hc.doReq(req)
	if err != nil {
		if strings.Contains(err.Error(), "Token is expired") &&
			!strings.Contains(err.Error(), "unable to authenticate the refresh token") {
			return hc.loginWithToken()
		}
		return err
	}

	if err := checkError(resp); err != nil {
		return err
	}

View on GitHub (pinned to 8cc6e09de2)

Solutions

  1. Check the dgraph source config and set a valid absolute baseURL, e.g. http://localhost:8080 (http/https only, no trailing garbage).
  2. Print or inspect the endpoint shown between [%v ...] in the message and paste it into a browser/curl to validate the URL syntax.
  3. Ensure any environment-variable placeholders in the config resolve to non-empty values before the toolbox starts.
  4. If the base URL comes from user input, validate it with url.ParseRequestURI at source initialization time.

Example fix

// before (config)
baseURL: dgraph://localhost:8080
// after (config)
baseURL: http://localhost:8080
Defensive patterns

Strategy: validation

Validate before calling

u, err := url.ParseRequestURI(baseURL)
if err != nil || u.Scheme == "" || u.Host == "" {
  return fmt.Errorf("invalid dgraph baseURL: %q", baseURL)
}

Type guard

func isValidHTTPURL(s string) bool {
  u, err := url.Parse(s)
  return err == nil && (u.Scheme == "http" || u.Scheme == "https") && u.Host != ""
}

Try / catch

err := loginWithToken(ctx, token)
if err != nil && strings.Contains(err.Error(), "error building req for endpoint") {
  // extract URL between [%v] and fix baseURL in the source config
  log.Fatalf("invalid dgraph endpoint: %v", err)
}

Prevention

When it happens

Trigger: hc.baseUrl is malformed (bad scheme, control characters, spaces) or nil/empty such that the joined login URL cannot be parsed by url.Parse, causing http.NewRequest(http.MethodPost, url, bytes.NewBuffer(payload)) to error.

Common situations: Misconfigured source YAML where the dgraph source's baseURL is empty, misspelled (e.g. 'dgraph://host:port' instead of 'http://host:port'), or contains whitespace; env-substituted values left unresolved.

Understand the failure class

Background: "Invalid URL" / "URL cannot be empty": fix the malformed or missing URL behind request-construction failures — this error's family across 50 libraries.

Related errors


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