alibaba/open-code-review · error
expected JSON object: %w
Error message
expected JSON object: %w
What it means
parseMCPHeaders wraps the json.Unmarshal failure with "expected JSON object: <cause>" when the headers value cannot be decoded into map[string]string. The headers field must be a flat JSON object whose keys and values are strings; arrays, bare strings, or nested objects fail here, and the error is then surfaced by setMCPServerValue as "invalid headers for ...".
Source
Thrown at cmd/opencodereview/config_cmd.go:939
filtered = append(filtered, t)
}
entry.Tools = filtered
case "setup":
entry.Setup = value
default:
return fmt.Errorf("unknown MCP server field %q: supported fields are type, command, args, env, url, headers, tools, setup", field)
}
cfg.MCPServers[name] = entry
return nil
}
// parseMCPHeaders parses a JSON object of header key-value pairs.
// Example: {"Authorization": "Bearer $TOKEN", "X-Custom": "value"}
func parseMCPHeaders(value string) (map[string]string, error) {
var m map[string]string
if err := json.Unmarshal([]byte(value), &m); err != nil {
return nil, fmt.Errorf("expected JSON object: %w", err)
}
for k, v := range m {
if k == "" {
return nil, fmt.Errorf("header name must not be empty")
}
if v == "" {
return nil, fmt.Errorf("header value for %q must not be empty", k)
}
}
return m, nil
}
func (c *Config) ensureTelemetry() {
if c.Telemetry == nil {
c.Telemetry = &TelemetryConfig{}
}
}
View on GitHub (pinned to 5cf97d0d15)
Solutions
- Pass a flat JSON object of strings: '{"Authorization":"Bearer t"}'
- Quote all JSON keys and ensure values are strings (use "123" not 123)
- Read the wrapped json error after `expected JSON object:` for the exact offset
Example fix
// before
ocr config set mcp-servers.myserver.headers '{"X-Retries": 3}'
// after
ocr config set mcp-servers.myserver.headers '{"X-Retries": "3"}' Defensive patterns
Strategy: validation
Validate before calling
func validHeaderObject(s string) bool {
var m map[string]string
return json.Unmarshal([]byte(s), &m) == nil
} Type guard
func asHeaderMap(v any) (map[string]string, bool) { m, ok := v.(map[string]string); return m, ok } Try / catch
_, err := parseMCPHeaders(raw)
if err != nil {
var se *json.SyntaxError
if errors.As(err, &se) { fmt.Fprintf(os.Stderr, "bad JSON at offset %d", se.Offset) }
return err
} Prevention
- Ensure all JSON keys are quoted and values are strings
- Pipe the candidate value through `jq type` to confirm it is an object
- Avoid nested header values — flatten to strings first
When it happens
Trigger: `ocr config set mcp-servers.myserver.headers '["a"]'` (array), `'{"a": 123}'` (non-string value), or a bare unquoted word; also malformed JSON like a missing brace or unquoted key.
Common situations: Non-string header values (numbers/booleans) in the JSON; single quotes inside the JSON unquoted by the shell; forgetting the braces around key-value pairs.
Related errors
- invalid JSON array for %s: %w
- invalid MCP server key %q: expected mcp_servers.<name>.<fiel
- invalid MCP server type %q: must be "stdio" or "remote"
- MCP server command cannot be empty
- invalid env entry %q: must be in KEY=VALUE format
AI-assisted analysis of alibaba/open-code-review@5cf97d0d15 (2026-09-02).
Data as JSON: /api/errors/f31e62828f83c362.
Report an issue: GitHub.