From b0f151f58073c66767026266604612282d6322bd Mon Sep 17 00:00:00 2001 From: Daliys Date: Sun, 5 Jul 2026 19:01:36 +0200 Subject: [PATCH] fix: handle stale add_component targets --- CHANGELOG.md | 1 + Editor/MCPServerMethods.Component.cs | 1 + Tests~/Editor/ConsolidatedManagersTests.cs | 12 ++++++++++++ 3 files changed, 14 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ebcd5ba..a94ee51 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ All notable public changes to Nexus Unity are documented here. - Relicensed Nexus Unity from `GPL-3.0-only` to the `MIT` license to remove copyleft friction for commercial Unity studios. All prior contributors consented to the relicense. ### Fixed +- `add_component` now returns a clear `GameObject not found` error for stale instance IDs instead of throwing a raw Unity null reference. - `batch_execute` now caps batches at 50 requests and rejects nested batch execution. - `create_primitive` now validates parent, transform, and material inputs before creating the GameObject, and Vector3 inputs accept `[x, y, z]` arrays as well as `{x, y, z}` objects. - `get_game_object` now returns transform state and a compact component list so agents can verify basic write operations with the cheap read-back call. diff --git a/Editor/MCPServerMethods.Component.cs b/Editor/MCPServerMethods.Component.cs index d305e09..a91fad8 100644 --- a/Editor/MCPServerMethods.Component.cs +++ b/Editor/MCPServerMethods.Component.cs @@ -33,6 +33,7 @@ private static JToken AddComponent(JToken p) { if (p == null || p["instance_id"] == null || p["component_name"] == null) throw new Exception("instance_id and component_name required"); var go = MCPServerMethods.IdToObject(MCPServerMethods.ExtractId(p)) as GameObject; + if (go == null) throw new Exception("GameObject not found"); Type type = FindType(p["component_name"].ToString()); if (type == null) throw new Exception($"Type '{p["component_name"]}' not found"); return new JObject { ["status"] = "Success", ["message"] = $"Added {p["component_name"]} to {Undo.AddComponent(go, type).name}" }; diff --git a/Tests~/Editor/ConsolidatedManagersTests.cs b/Tests~/Editor/ConsolidatedManagersTests.cs index d57c71a..9bc7b4a 100644 --- a/Tests~/Editor/ConsolidatedManagersTests.cs +++ b/Tests~/Editor/ConsolidatedManagersTests.cs @@ -403,6 +403,18 @@ public void GetGameObject_ReturnsTransformAndComponentsForReadBackVerification() Assert.IsTrue(components.Any(c => c["type"]?.ToString() == "BoxCollider")); } + [Test] + public void AddComponent_StaleInstanceId_ReturnsGameObjectNotFound() { + var go = CreateTestGameObject(); + int staleId = go.GetInstanceID(); + GameObject.DestroyImmediate(go); + + var res = CallRaw("add_component", new JObject { ["instance_id"] = staleId, ["component_name"] = "BoxCollider" }); + + Assert.IsNotNull(res["error"], res.ToString()); + StringAssert.Contains("GameObject not found", res["error"]["message"].ToString()); + } + [Test] public void UnityComponentManager_Inspect_ReturnsSuccess() { var go = CreateTestGameObject();