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
- Check the dgraph source config and set a valid absolute baseURL, e.g. http://localhost:8080 (http/https only, no trailing garbage).
- Print or inspect the endpoint shown between [%v ...] in the message and paste it into a browser/curl to validate the URL syntax.
- Ensure any environment-variable placeholders in the config resolve to non-empty values before the toolbox starts.
- 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
- Always use absolute http:// or https:// URLs for the dgraph baseURL in source config.
- Validate the URL with url.ParseRequestURI at source initialization, not at request time.
- Resolve environment-variable placeholders in config before startup and fail fast if empty.
- Avoid trailing whitespace and stray characters in config values; trim before use.
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
- error creating request: %w
- failed to parse BaseUrl %v
- failed to create request: %w
- failed to construct introspection URL: %w
- failed to parse introspection URL: %w
AI-assisted analysis of googleapis/mcp-toolbox@8cc6e09de2 (2026-09-05).
Data as JSON: /api/errors/c339008309b6f370.
Report an issue: GitHub.