From 5a829a3e98fd146f0a9725ae2aa57618e7f56282 Mon Sep 17 00:00:00 2001 From: Husam Date: Thu, 8 Jan 2026 08:38:55 +0300 Subject: [PATCH] Fix: Handle ReflectionTypeLoadException when loading assemblies Assemblies with missing dependencies now get skipped instead of crashing the MCP plugin build. Co-Authored-By: Claude Opus 4.5 --- .../root/Runtime/UnityMcpPlugin.Build.cs | 31 ++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/Unity-MCP-Plugin/Assets/root/Runtime/UnityMcpPlugin.Build.cs b/Unity-MCP-Plugin/Assets/root/Runtime/UnityMcpPlugin.Build.cs index b6ac4105d..a53cda2e4 100644 --- a/Unity-MCP-Plugin/Assets/root/Runtime/UnityMcpPlugin.Build.cs +++ b/Unity-MCP-Plugin/Assets/root/Runtime/UnityMcpPlugin.Build.cs @@ -10,7 +10,9 @@ #nullable enable using System; +using System.Collections.Generic; using System.Linq; +using System.Reflection; using com.IvanMurzak.McpPlugin; using com.IvanMurzak.ReflectorNet; using com.IvanMurzak.Unity.MCP.Utils; @@ -95,12 +97,39 @@ protected virtual McpPlugin.Common.Version BuildVersion() return new UnityLoggerProvider(); } + /// + /// Filters assemblies to only include those that can successfully load their types. + /// This prevents ReflectionTypeLoadException from assemblies with missing dependencies. + /// + protected virtual IEnumerable GetSafeAssemblies() + { + foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) + { + if (assembly.IsDynamic) + continue; + + try + { + assembly.GetTypes(); + yield return assembly; + } + catch (ReflectionTypeLoadException) + { + _logger.LogWarning("Skipping assembly with missing dependencies: {assembly}", assembly.GetName().Name); + } + catch (Exception ex) + { + _logger.LogWarning("Skipping assembly {assembly}: {error}", assembly.GetName().Name, ex.Message); + } + } + } + protected virtual IMcpPlugin BuildMcpPlugin(McpPlugin.Common.Version version, Reflector reflector, ILoggerProvider? loggerProvider = null) { _logger.LogTrace("{method} called.", nameof(BuildMcpPlugin)); - var assemblies = AppDomain.CurrentDomain.GetAssemblies(); + var assemblies = GetSafeAssemblies().ToArray(); var mcpPlugin = new McpPluginBuilder(version, loggerProvider) .WithConfig(config => {