@@ -97,23 +97,44 @@ public override JObject Execute(JObject parameters)
9797 }
9898
9999 component = Undo . AddComponent ( gameObject , componentType ) ;
100+
101+ // Ensure changes are saved
102+ EditorUtility . SetDirty ( gameObject ) ;
103+ if ( PrefabUtility . IsPartOfAnyPrefab ( gameObject ) )
104+ {
105+ PrefabUtility . RecordPrefabInstancePropertyModifications ( component ) ;
106+ }
100107 wasAdded = true ;
101108 McpLogger . LogInfo ( $ "[MCP Unity] Added component '{ componentName } ' to GameObject '{ gameObject . name } '") ;
102109 }
103-
104110 // Update component fields
105111 if ( componentData != null && componentData . Count > 0 )
106112 {
107- UpdateComponentData ( component , componentData ) ;
108- }
109-
110- // Ensure changes are saved
111- EditorUtility . SetDirty ( gameObject ) ;
112- if ( PrefabUtility . IsPartOfAnyPrefab ( gameObject ) )
113- {
114- PrefabUtility . RecordPrefabInstancePropertyModifications ( component ) ;
113+ bool success = UpdateComponentData ( component , componentData , out string errorMessage ) ;
114+ // If update failed, return error
115+ if ( ! success )
116+ {
117+ if ( wasAdded )
118+ {
119+ return McpUnitySocketHandler . CreateErrorResponse (
120+ $ "Successfully added component '{ componentName } ' to GameObject '{ gameObject . name } ' BUT\n " +
121+ errorMessage , "component_error" ) ;
122+ }
123+ else
124+ {
125+ return McpUnitySocketHandler . CreateErrorResponse ( errorMessage , "update_error" ) ;
126+ }
127+ }
128+
129+ // Ensure field changes are saved
130+ EditorUtility . SetDirty ( gameObject ) ;
131+ if ( PrefabUtility . IsPartOfAnyPrefab ( gameObject ) )
132+ {
133+ PrefabUtility . RecordPrefabInstancePropertyModifications ( component ) ;
134+ }
135+
115136 }
116-
137+
117138 // Create the response
118139 return new JObject
119140 {
@@ -236,27 +257,29 @@ private Type FindComponentType(string componentName)
236257 /// <param name="component">The component to update</param>
237258 /// <param name="componentData">The data to apply to the component</param>
238259 /// <returns>True if the component was updated successfully</returns>
239- private bool UpdateComponentData ( Component component , JObject componentData )
260+ private bool UpdateComponentData ( Component component , JObject componentData , out string errorMessage )
240261 {
262+ errorMessage = "" ;
241263 if ( component == null || componentData == null )
242264 {
265+ errorMessage = "Component or component data is null" ;
243266 return false ;
244267 }
245-
268+
246269 Type componentType = component . GetType ( ) ;
247- bool anySuccess = false ;
248-
270+ bool fullSuccess = true ;
271+
249272 // Record object for undo
250273 Undo . RecordObject ( component , $ "Update { componentType . Name } fields") ;
251-
274+
252275 // Process each field in the component data
253276 foreach ( var property in componentData . Properties ( ) )
254277 {
255278 string fieldName = property . Name ;
256279 JToken fieldValue = property . Value ;
257280
258281 // Skip null values
259- if ( fieldValue . Type == JTokenType . Null )
282+ if ( string . IsNullOrEmpty ( fieldName ) || fieldValue . Type == JTokenType . Null )
260283 {
261284 continue ;
262285 }
@@ -269,18 +292,19 @@ private bool UpdateComponentData(Component component, JObject componentData)
269292 {
270293 object value = ConvertJTokenToValue ( fieldValue , fieldInfo . FieldType ) ;
271294 fieldInfo . SetValue ( component , value ) ;
272- anySuccess = true ;
273295 continue ;
274296 }
275297 else
276298 {
277- McpLogger . LogWarning ( $ "Field '{ fieldName } ' not found on component '{ componentType . Name } '") ;
299+ errorMessage = $ "Field '{ fieldName } ' not found on component '{ componentType . Name } '";
300+ McpLogger . LogError ( errorMessage ) ;
301+ fullSuccess = false ;
278302 }
279303 }
280-
281- return anySuccess ;
304+
305+ return fullSuccess ;
282306 }
283-
307+
284308 /// <summary>
285309 /// Convert a JToken to a value of the specified type
286310 /// </summary>
0 commit comments