CoplayDev/unity-mcp · error · ValueError

{context} is required

Error message

{context} is required

What it means

Raised by _normalize_color (texture.py:106) when the color value is None. The 'context' is a label identifying which field is missing (e.g. 'color', 'set-pixels color', 'palette item', 'set-pixels pixels pixel'). _normalize_color is called from the create/sprite --color options, from _normalize_palette per item, from _normalize_pixels per pixel, and from _normalize_set_pixels for the color key. The CLI wraps these in try/except ValueError -> print_error -> exit(1).

Source

Thrown at Server/src/cli/commands/texture.py:106

    has_fractional = any(0 < v < 1 for v in numeric_values)
    all_binary = all(v in (0, 1, 0.0, 1.0) for v in numeric_values)

    return has_fractional or all_binary


def _parse_hex_color(value: str) -> list[int]:
    h = value.lstrip("#")
    if len(h) == 6:
        return [int(h[i:i + 2], 16) for i in (0, 2, 4)] + [255]
    if len(h) == 8:
        return [int(h[i:i + 2], 16) for i in (0, 2, 4, 6)]
    raise ValueError(f"Invalid hex color: {value}")


def _normalize_color(value: Any, context: str) -> list[int]:
    if value is None:
        raise ValueError(f"{context} is required")

    if isinstance(value, str):
        if value.startswith("#"):
            return _parse_hex_color(value)
        value = try_parse_json(value, context)

    # Handle dict with r/g/b keys (e.g., {"r": 1, "g": 0, "b": 0} or {"r": 1, "g": 0, "b": 0, "a": 1})
    if isinstance(value, dict):
        if all(k in value for k in ("r", "g", "b")):
            try:
                color = [value["r"], value["g"], value["b"]]
                if "a" in value:
                    color.append(value["a"])
                else:
                    color.append(1.0 if _is_normalized_color(color) else 255)
                if _is_normalized_color(color):
                    return [int(round(float(c) * 255)) for c in color]
                return [int(c) for c in color]

View on GitHub (pinned to c21bf496bc)

Solutions

  1. Provide a concrete color (hex string, [r,g,b] list, or {r,g,b} dict) for every required color entry.
  2. Filter nulls out of palette/pixel arrays before passing them.
  3. Validate the JSON structure of --palette/--set-pixels before invoking the CLI.

Example fix

# before
unity-mcp texture create Assets/Tex.png --pattern checkerboard --palette '[null, "#FF0000"]'

# after
unity-mcp texture create Assets/Tex.png --pattern checkerboard --palette '["#FFFFFF", "#FF0000"]'
Defensive patterns

Strategy: validation

Validate before calling

# Python - ensure required colors are not None before _normalize_color
if value is None:
    raise ValueError(f"{context} is required")

Type guard

def has_color_value(v) -> bool: return v is not None

Try / catch

try:
    color = _normalize_color(value, context)
except ValueError as e:
    print_error(str(e)); sys.exit(1)

Prevention

When it happens

Trigger: Passing null/None for a color where one is required: a palette entry that is null (`--palette '[null]'`), a set-pixels color of null, or a pixels array containing null entries. Note the top-level --color flag itself defaults to white if omitted, so the top-level create/sprite path rarely hits this; it is more common in palette/pixel/set-pixels arrays.

Common situations: A JSON palette or pixels array with a null element; a set-pixels payload missing the color value but flagged as having one; programmatic construction that leaves a color entry as None.

Related errors


AI-assisted analysis of CoplayDev/unity-mcp@c21bf496bc (2026-08-13). Data as JSON: /api/errors/9f8ee9129ae6cf83. Report an issue: GitHub.