{"record":{"id":"46b9046403bf6204","repo":"CoplayDev/unity-mcp","slug":"parameter-encodedcontents-must-be-valid-base64-w","errorCode":null,"errorMessage":"Parameter 'encodedContents' must be valid base64 when 'contentsEncoded' is true.","messagePattern":"Parameter 'encodedContents' must be valid base64 when 'contentsEncoded' is true\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"MCPForUnity/Editor/Tools/ManageUI.cs","lineNumber":1808,"sourceCode":"            return $\"#{ColorUtility.ToHtmlStringRGBA(c)}\";\n        }\n\n        private static string GetDecodedContents(ToolParams p)\n        {\n            bool isEncoded = p.GetBool(\"contents_encoded\") || p.GetBool(\"contentsEncoded\");\n\n            if (isEncoded)\n            {\n                string encoded = p.Get(\"encoded_contents\") ?? p.Get(\"encodedContents\");\n                if (!string.IsNullOrEmpty(encoded))\n                {\n                    try\n                    {\n                        return Encoding.UTF8.GetString(Convert.FromBase64String(encoded));\n                    }\n                    catch (FormatException ex)\n                    {\n                        throw new ArgumentException(\n                            \"Parameter 'encodedContents' must be valid base64 when 'contentsEncoded' is true.\",\n                            ex);\n                    }\n                }\n            }\n\n            return p.Get(\"contents\");\n        }\n\n        /// <summary>\n        /// Validates UXML content before writing to disk.\n        /// Returns null if valid, or an error message if malformed.\n        /// Populates warnings list with non-fatal issues.\n        /// Uses XmlParserContext to pre-declare common UXML namespace prefixes\n        /// (ui, uie, engine, editor) since Unity's parser is more lenient than System.Xml.\n        /// </summary>\n\n        /// <summary>","sourceCodeStart":1790,"sourceCodeEnd":1826,"githubUrl":"https://github.com/CoplayDev/unity-mcp/blob/c21bf496bca87d54e75bad048563c3adb1782081/MCPForUnity/Editor/Tools/ManageUI.cs#L1790-L1826","documentation":"Thrown by GetDecodedContents when 'contentsEncoded' (or 'contents_encoded') is true and the supplied 'encodedContents' string is not valid Base64. Convert.FromBase64String raises a FormatException that is wrapped in this ArgumentException. The method expects UTF-8 text that was Base64-encoded client-side before transport.","triggerScenarios":"Calling a ManageUI tool (e.g. create_uxml, write_visual_element) with contentsEncoded=true but providing a plain-text, partially-encoded, or whitespace-padded string in encodedContents/encoded_contents. Also triggered by passing a data-URI prefix like 'data:text/plain;base64,...' without stripping the header.","commonSituations":"AI assistant sends raw UXML instead of Base64; encoding library adds newlines every 76 chars that confuse the decoder; copy-paste truncates the Base64 string; client uses URL-safe Base64 (- and _) instead of standard (+ and /).","solutions":["Base64-encode the raw UTF-8 string before sending it (e.g. Python: base64.b64encode(content.encode('utf-8')).decode('ascii')).","If the content is plain text, set contentsEncoded=false (or omit it) and pass the raw string in 'contents' instead.","Verify the encoded value round-trips: decode it locally and confirm the output matches the original before sending.","Strip any data-URI prefix or whitespace/newlines from the encoded string before passing it."],"exampleFix":"// before\nparams = {\"contentsEncoded\": true, \"encodedContents\": \"<UXML>...</UXML>\"}\n// after\nimport base64\nparams = {\"contentsEncoded\": true, \"encodedContents\": base64.b64encode(uxml.encode('utf-8')).decode('ascii')}","handlingStrategy":"validation","validationCode":"import base64\n\ndef validate_encoded_contents(encoded: str) -> bool:\n    \"\"\"Returns True if the string is valid Base64.\"\"\"\n    try:\n        base64.b64decode(encoded, validate=True)\n        return True\n    except Exception:\n        return False\n\n# before sending:\nif not validate_encoded_contents(params['encodedContents']):\n    raise ValueError('encodedContents is not valid base64')","typeGuard":null,"tryCatchPattern":"// C# caller wrapping the tool invocation\ntry\n{\n    var decoded = GetDecodedContents(p);\n}\ncatch (ArgumentException ex) when (ex.Message.Contains(\"must be valid base64\"))\n{\n    // Log and fall back to plain 'contents' or re-prompt the caller\n    logger.Warn($\"Base64 decode failed; falling back to raw contents.\");\n    return p.Get(\"contents\");\n}","preventionTips":["Always Base64-encode with standard encoding (+ and /) not URL-safe (- and _).","Strip data-URI prefixes and whitespace before encoding for transport.","If the content is small and ASCII, consider passing it raw via 'contents' instead.","Validate base64 round-trip on the client before sending."],"tags":["base64","encoding","ui-toolkit","validation","manage-ui"],"backgroundTag":null,"analyzedSha":"c21bf496bca87d54e75bad048563c3adb1782081","analyzedAt":"2026-08-13T17:36:56.095Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}