alibaba/open-code-review · error

invalid JSON array for %s: %w

Error message

invalid JSON array for %s: %w

What it means

Fires in setMCPServerValue when the value for the args field of an MCP server is not a JSON array of strings (json.Unmarshal into []string fails). CLI args for an MCP server must be a JSON array like ["serve","--port","8080"]; anything else would produce an unusable arg list, so the setter rejects it, wrapping the unmarshal error and naming the full key.

Source

Thrown at cmd/opencodereview/config_cmd.go:870

		cfg.MCPServers = make(map[string]MCPServerConfig)
	}
	entry := cfg.MCPServers[name]

	switch field {
	case "type":
		if value != "stdio" && value != "remote" {
			return fmt.Errorf("invalid MCP server type %q: must be \"stdio\" or \"remote\"", value)
		}
		entry.Type = value
	case "command":
		if value == "" {
			return fmt.Errorf("MCP server command cannot be empty")
		}
		entry.Command = value
	case "args":
		var args []string
		if err := json.Unmarshal([]byte(value), &args); err != nil {
			return fmt.Errorf("invalid JSON array for %s: %w", key, err)
		}
		entry.Args = args
	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")
		}

View on GitHub (pinned to 5cf97d0d15)

Solutions

  1. Wrap the arguments in a valid JSON array of strings, e.g. '["-y","@modelcontextprotocol/server-filesystem","/tmp"]'
  2. Validate the JSON with a linter or `echo '<value>' | jq .` before setting
  3. Escape inner double quotes for your shell (e.g. use single quotes on POSIX shells)

Example fix

// before
ocr config set mcp_servers.fs.args -y server-filesystem
// after
ocr config set mcp_servers.fs.args '["-y","server-filesystem","/tmp"]'
Defensive patterns

Strategy: validation

Validate before calling

var args []string
if err := json.Unmarshal([]byte(value), &args); err != nil {
	return fmt.Errorf("args must be a JSON string array: %w", err)
}

Type guard

func isStringArray(v string) bool {
	var a []string
	return json.Unmarshal([]byte(v), &a) == nil
}

Try / catch

if err := setMCPServerValue(cfg, key, value); err != nil {
	var jsonErr *json.SyntaxError
	if errors.As(err, &jsonErr) {
		fmt.Fprintf(os.Stderr, "bad JSON at offset %d: %v\n", jsonErr.Offset, err)
	}
	os.Exit(1)
}

Prevention

When it happens

Trigger: Calling setMCPServerValue with field "args" and a value that is not valid JSON, not an array, or an array with non-string elements, e.g. "-y @modelcontextprotocol/server" (bare string), "{\"a\":1}" (object), or "[1,2]" (non-strings).

Common situations: Passing shell-style argument lists without JSON quoting; forgetting that quotes inside the value must survive shell escaping; using single quotes in JSON on Windows cmd.

Understand the failure class

Related errors


AI-assisted analysis of alibaba/open-code-review@5cf97d0d15 (2026-09-02). Data as JSON: /api/errors/bf72c0432df311f4. Report an issue: GitHub.