CoplayDev/unity-mcp · error · NotSupportedException

Unsupported image type '{ext}' for image input. Use .png, .j

Error message

Unsupported image type '{ext}' for image input. Use .png, .jpg, .jpeg, .webp, or .gif.

What it means

Thrown by LocalImage.MimeFromExtension (via ToDataUri) when the image file extension is not one of the five supported types: .png, .jpg, .jpeg, .webp, .gif. The switch has no default case that succeeds, so any other extension (or no extension) reaches the default throw. ResolveExisting checks the same set non-throwingly via SupportedExtensions, so the throw indicates ToDataUri was called without that pre-check.

Source

Thrown at MCPForUnity/Editor/Services/AssetGen/Providers/LocalImage.cs:70

            if (!AssetGenPaths.TryGetAssetsRelativePath(absPath, out string rel))
                throw new UnauthorizedAccessException("image_path must point to a file under the project's Assets folder.");
            absPath = AssetGenPaths.ToAbsolute(rel);
            string mime = MimeFromExtension(Path.GetExtension(absPath));
            byte[] bytes = File.ReadAllBytes(absPath);
            return "data:" + mime + ";base64," + Convert.ToBase64String(bytes);
        }

        private static string MimeFromExtension(string ext)
        {
            switch ((ext ?? string.Empty).ToLowerInvariant())
            {
                case ".png": return "image/png";
                case ".jpg":
                case ".jpeg": return "image/jpeg";
                case ".webp": return "image/webp";
                case ".gif": return "image/gif";
                default:
                    throw new NotSupportedException(
                        $"Unsupported image type '{ext}' for image input. Use .png, .jpg, .jpeg, .webp, or .gif.");
            }
        }
    }
}

View on GitHub (pinned to c21bf496bc)

Solutions

  1. Re-export/convert the image to PNG or JPEG (widest provider compatibility).
  2. If you need WebP/GIF animation, ensure the provider accepts that type (all five are accepted for data-URI inlining).
  3. Pre-validate the extension with LocalImage.ResolveExisting before calling ToDataUri.

Example fix

// before
string dataUri = LocalImage.ToDataUri(absPath); // throws NotSupportedException for .bmp

// after
if (!LocalImage.ResolveExisting(req.ImagePath, out string abs, out string err))
    return ErrorResponse(err); // err names the bad extension
string dataUri = LocalImage.ToDataUri(abs);
Defensive patterns

Strategy: validation

Validate before calling

string ext = System.IO.Path.GetExtension(absPath);
string[] ok = {".png",".jpg",".jpeg",".webp",".gif"};
if (Array.IndexOf(ok, (ext ?? "").ToLowerInvariant()) < 0)
    return ErrorResponse($"Unsupported image type '{ext}'. Use .png/.jpg/.jpeg/.webp/.gif.");

Type guard

static readonly HashSet<string> SupportedImageExt =
    new(StringComparer.OrdinalIgnoreCase){".png",".jpg",".jpeg",".webp",".gif"};
static bool IsSupportedImage(string path)
    => SupportedImageExt.Contains(System.IO.Path.GetExtension(path));

Try / catch

try { string uri = LocalImage.ToDataUri(absPath); }
catch (NotSupportedException ex)
{ /* convert the image to png/jpg, or host it as image_url */ }

Prevention

When it happens

Trigger: Calling ToDataUri on a file with extension .bmp, .tiff, .tga, .heic, .svg, .exr, .psd, or a file with no extension.

Common situations: Unity-native texture formats exported (.tga/.psd/.exr); screenshot saved as .heic on macOS/iOS; an asset with no extension; wrong export preset.

Related errors


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