Skip to content

Public UnityEngine.Object JsonConverter can disrupt existing Newtonsoft.Json serialization #1138

@n-starshov

Description

@n-starshov

Summary

MCPForUnity.Runtime.Serialization.UnityEngineObjectConverter is a public JsonConverter<UnityEngine.Object> with a default constructor.
This makes it discoverable by packages/tools that scan loaded assemblies for Newtonsoft JsonConverter implementations and add them to global JsonConvert.DefaultSettings.
As a result, installing Unity MCP can unintentionally change unrelated project serialization behavior.

Problem

The converter serializes Unity assets as asset paths:

if (UnityEditor.AssetDatabase.Contains(value))
{
    string path = UnityEditor.AssetDatabase.GetAssetPath(value);
    writer.WriteValue(path);
}

That behavior is useful for MCP-internal serialization, but it is risky when the converter becomes globally discoverable.
Any unrelated code that serializes an object containing a ScriptableObject, Material, Texture, prefab reference, or other UnityEngine.Object may start receiving asset paths instead of normal object JSON.

Minimal Example

public class SomeConfig : ScriptableObject
{
    public List<Row> Rows = new();
}
public struct Container
{
    public SomeConfig Config;
}
var json = JsonConvert.SerializeObject(container);

Expected by unrelated project code:

{
  "Config": {
    "Rows": []
  }
}

Possible result after global converter auto-discovery:

{
  "Config": "Assets/Path/To/SomeConfig.asset"
}

Why This Is Surprising

The affected code does not call MCP APIs or MCP serializer settings directly. The behavior can change just because the MCP package is installed and its converter type is visible to external converter discovery.
One common trigger is jillejr.newtonsoft.json-for-unity.converters, which can scan loaded assemblies for public Newtonsoft converters and inject them into JsonConvert.DefaultSettings.
Suggested Fixes
Possible options:

  1. Make UnityEngineObjectConverter internal if it is only intended for MCP internals.
  2. Remove the public parameterless constructor so generic converter scanners do not instantiate it automatically.
  3. Move MCP-specific converters into an internal/editor-only assembly not intended for global discovery.
  4. Avoid exposing broad converters such as JsonConverter<UnityEngine.Object> publicly.
  5. Keep the converter public but require explicit MCP serializer settings to use it.

Environment

  • Unity 6000.0.60f1
  • com.coplaydev.unity-mcp
  • Newtonsoft.Json via Unity package
  • jillejr.newtonsoft.json-for-unity.converters

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions