flipped-aurora/gin-vue-admin · error

构建上游请求地址失败: %w

Error message

构建上游请求地址失败: %w

What it means

After joining upstreamBaseURL() with the endpoint, url.Parse failed — the combined URL is malformed. The wrap preserves the underlying url.Parse error (e.g. invalid control characters, bad scheme).

Source

Thrown at server/mcp/http_client.go:171

func doUpstream[T any](ctx context.Context, method, endpoint string, query url.Values, body any) (*upstreamEnvelope[T], error) {
	token := authTokenFromContext(ctx)
	if token == "" {
		return nil, fmt.Errorf("缺少MCP鉴权请求头: %s", configuredAuthHeader())
	}

	endpoint = strings.TrimSpace(endpoint)
	if endpoint == "" {
		return nil, fmt.Errorf("上游接口路径不能为空")
	}
	if !strings.HasPrefix(endpoint, "/") {
		endpoint = "/" + endpoint
	}

	baseURL := upstreamBaseURL()
	requestURL, err := url.Parse(baseURL + endpoint)
	if err != nil {
		return nil, fmt.Errorf("构建上游请求地址失败: %w", err)
	}
	if len(query) > 0 {
		requestURL.RawQuery = query.Encode()
	}

	var reader io.Reader
	if body != nil {
		payload, err := json.Marshal(body)
		if err != nil {
			return nil, fmt.Errorf("序列化上游请求失败: %w", err)
		}
		reader = bytes.NewReader(payload)
	}

	timeoutCtx, cancel := context.WithTimeout(ctx, requestTimeout())
	defer cancel()

	req, err := http.NewRequestWithContext(timeoutCtx, method, requestURL.String(), reader)

View on GitHub (pinned to 3136500ef3)

Solutions

  1. Print upstreamBaseURL() and the endpoint at failure time to spot invisible characters or placeholders
  2. Ensure the base URL includes scheme and host, e.g. http://127.0.0.1:8888, with no quotes/whitespace
  3. Validate the base URL at startup with url.Parse and fail fast
  4. Sanitize env/config values with strings.TrimSpace before use

Example fix

// before
baseURL := os.Getenv("MCP_UPSTREAM_BASE") // "  http://127.0.0.1:8888\n"
// after
baseURL := strings.TrimSpace(os.Getenv("MCP_UPSTREAM_BASE"))
if _, err := url.Parse(baseURL); err != nil {
	panic(fmt.Sprintf("invalid MCP_UPSTREAM_BASE: %v", err))
}
Defensive patterns

Strategy: validation

Validate before calling

func validateBaseURL(raw string) (string, error) {
	raw = strings.TrimSpace(raw)
	u, err := url.Parse(raw)
	if err != nil {
		return "", fmt.Errorf("invalid upstream base URL %q: %w", raw, err)
	}
	if u.Scheme == "" || u.Host == "" {
		return "", fmt.Errorf("upstream base URL needs scheme and host: %q", raw)
	}
	return raw, nil
}

Try / catch

result, err := getUpstream[Data](ctx, "/api/x", nil)
if err != nil {
	if strings.Contains(err.Error(), "构建上游请求地址失败") {
		// fix MCP upstream base URL configuration
	}
	return err
}

Prevention

When it happens

Trigger: upstreamBaseURL() from config/env contains spaces, control characters, or garbage; endpoint contains unescaped characters that form an invalid URL; base URL missing its scheme so the concatenation is unparseable in an unexpected way.

Common situations: Upstream base URL env var set with trailing whitespace/newline or quotes; config placeholder like "http://<host>:<port>" not replaced; copy-pasting a URL with invisible characters.

Related errors


AI-assisted analysis of flipped-aurora/gin-vue-admin@3136500ef3 (2026-08-31). Data as JSON: /api/errors/0e5b46c9129eb223. Report an issue: GitHub.