Skip to content

Commit 9745885

Browse files
feat: update_component will now return an error with proper message if it tries to update a field that does not exist
1 parent 2d8d853 commit 9745885

File tree

1 file changed

+31
-11
lines changed

1 file changed

+31
-11
lines changed

Editor/Tools/UpdateComponentTool.cs

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,27 @@ public override JObject Execute(JObject parameters)
101101
McpLogger.LogInfo($"[MCP Unity] Added component '{componentName}' to GameObject '{gameObject.name}'");
102102
}
103103

104+
string errorMessage = "";
105+
bool success = false;
104106
// Update component fields
105107
if (componentData != null && componentData.Count > 0)
106108
{
107-
UpdateComponentData(component, componentData);
109+
success = UpdateComponentData(component, componentData, out errorMessage);
110+
}
111+
112+
// If update failed, return error
113+
if (!success)
114+
{
115+
if (wasAdded)
116+
{
117+
return McpUnitySocketHandler.CreateErrorResponse(
118+
$"Successfully added component '{componentName}' to GameObject '{gameObject.name}' BUT\n" +
119+
errorMessage, "component_error");
120+
}
121+
else
122+
{
123+
return McpUnitySocketHandler.CreateErrorResponse(errorMessage, "update_error");
124+
}
108125
}
109126

110127
// Ensure changes are saved
@@ -236,19 +253,21 @@ private Type FindComponentType(string componentName)
236253
/// <param name="component">The component to update</param>
237254
/// <param name="componentData">The data to apply to the component</param>
238255
/// <returns>True if the component was updated successfully</returns>
239-
private bool UpdateComponentData(Component component, JObject componentData)
256+
private bool UpdateComponentData(Component component, JObject componentData, out string errorMessage)
240257
{
258+
errorMessage = "";
241259
if (component == null || componentData == null)
242260
{
261+
errorMessage = "Component or component data is null";
243262
return false;
244263
}
245-
264+
246265
Type componentType = component.GetType();
247-
bool anySuccess = false;
248-
266+
bool gotFailure = false;
267+
249268
// Record object for undo
250269
Undo.RecordObject(component, $"Update {componentType.Name} fields");
251-
270+
252271
// Process each field in the component data
253272
foreach (var property in componentData.Properties())
254273
{
@@ -269,18 +288,19 @@ private bool UpdateComponentData(Component component, JObject componentData)
269288
{
270289
object value = ConvertJTokenToValue(fieldValue, fieldInfo.FieldType);
271290
fieldInfo.SetValue(component, value);
272-
anySuccess = true;
273291
continue;
274292
}
275293
else
276294
{
277-
McpLogger.LogWarning($"Field '{fieldName}' not found on component '{componentType.Name}'");
295+
errorMessage = $"Field '{fieldName}' not found on component '{componentType.Name}'";
296+
McpLogger.LogError(errorMessage);
297+
gotFailure = true;
278298
}
279299
}
280-
281-
return anySuccess;
300+
301+
return !gotFailure;
282302
}
283-
303+
284304
/// <summary>
285305
/// Convert a JToken to a value of the specified type
286306
/// </summary>

0 commit comments

Comments
 (0)