alibaba/open-code-review · error

invalid MCP server URL %q: %w

Error message

invalid MCP server URL %q: %w

What it means

Fires when url.Parse fails on the value given for mcp_servers.<name>.url — the string is syntactically not a URL (e.g. contains control characters or a malformed scheme). The raw parse error is wrapped and the invalid value quoted so the user can see which input failed.

Source

Thrown at cmd/opencodereview/config_cmd.go:891

	case "env":
		var env []string
		if err := json.Unmarshal([]byte(value), &env); err != nil {
			return fmt.Errorf("invalid JSON array for %s: %w", key, err)
		}
		for _, e := range env {
			idx := strings.Index(e, "=")
			if idx <= 0 {
				return fmt.Errorf("invalid env entry %q: must be in KEY=VALUE format", e)
			}
		}
		entry.Env = env
	case "url":
		if value == "" {
			return fmt.Errorf("MCP server URL cannot be empty")
		}
		parsed, err := url.Parse(value)
		if err != nil {
			return fmt.Errorf("invalid MCP server URL %q: %w", value, err)
		}
		if parsed.Scheme != "http" && parsed.Scheme != "https" {
			return fmt.Errorf("MCP server URL must use http or https scheme, got %q", parsed.Scheme)
		}
		if parsed.Host == "" {
			return fmt.Errorf("MCP server URL %q must include a host", value)
		}
		entry.URL = value
	case "headers":
		parsed, err := parseMCPHeaders(value)
		if err != nil {
			return fmt.Errorf("invalid headers for %s: %w", key, err)
		}
		entry.Headers = parsed
	case "tools":
		var tools []string
		if err := json.Unmarshal([]byte(value), &tools); err != nil {
			return fmt.Errorf("invalid JSON array for %s: %w", key, err)

View on GitHub (pinned to 5cf97d0d15)

Solutions

  1. Percent-encode special characters properly in the URL
  2. Test the URL with a browser or `curl -I` first
  3. Ensure valid percent escapes: %20 not %zz

Example fix

// before
ocr config set mcp_servers.api.url 'http://host/path%zz'
// after
ocr config set mcp_servers.api.url 'http://host/path%20x'
Defensive patterns

Strategy: validation

Validate before calling

if _, err := url.Parse(rawURL); err != nil {
	return fmt.Errorf("malformed URL: %w", err)
}

Try / catch

if err := setMCPServerValue(cfg, key, value); err != nil {
	fmt.Fprintf(os.Stderr, "config set failed: %v\n", err)
	os.Exit(1)
}

Prevention

When it happens

Trigger: Calling setMCPServerValue with field "url" and a value that net/url.Parse rejects, e.g. containing raw control characters or invalid percent-escapes like 'http://x/%zz'.

Common situations: Pasting URLs with unescaped special characters from terminals or docs; corrupted values from shell escaping of '%' sequences.

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 alibaba/open-code-review@5cf97d0d15 (2026-09-02). Data as JSON: /api/errors/93e7e3858dbc6d32. Report an issue: GitHub.