feat(manage_editor): add open_prefab_stage and save_prefab_stage actions#988
feat(manage_editor): add open_prefab_stage and save_prefab_stage actions#988zaferdace wants to merge 2 commits intoCoplayDev:mainfrom
Conversation
Adds two new actions to manage_editor to complete the prefab stage workflow: - `open_prefab_stage`: Opens a prefab in Prefab Mode using PrefabStageUtility.OpenPrefab(). After opening, existing tools (manage_gameobject, manage_components) can modify objects inside the prefab stage. - `save_prefab_stage`: Saves changes made in the currently open prefab stage back to the prefab asset. Combined with the existing `close_prefab_stage`, this enables a full programmatic workflow: open_prefab_stage → manage_components/manage_gameobject → save_prefab_stage → close_prefab_stage Also updates the supported actions list in the error message. Closes CoplayDev#976
Reviewer's GuideAdds prefab stage workflow support to the manage_editor MCP tool by introducing open and save actions around the existing close_prefab_stage, plus wiring them into the command dispatcher and error messaging. Sequence diagram for prefab stage MCP workflowsequenceDiagram
actor User
participant MCPClient
participant ManageEditor
participant ManageGameObject
participant ManageComponents
participant AssetDatabase
participant PrefabUtility
participant PrefabStageUtility
participant EditorSceneManager
User->>MCPClient: manage_editor(action=open_prefab_stage, path=Assets/Prefabs/MyPrefab.prefab)
MCPClient->>ManageEditor: HandleCommand(action=open_prefab_stage, path)
ManageEditor->>ManageEditor: OpenPrefabStage(prefabPath)
ManageEditor->>AssetDatabase: LoadMainAssetAtPath(prefabPath)
AssetDatabase-->>ManageEditor: asset
ManageEditor->>PrefabUtility: GetPrefabAssetType(asset)
PrefabUtility-->>ManageEditor: prefabAssetType
alt asset missing or not a prefab
ManageEditor-->>MCPClient: ErrorResponse
else valid prefab
ManageEditor->>PrefabStageUtility: OpenPrefab(prefabPath)
PrefabStageUtility-->>ManageEditor: prefabStage
alt open failed
ManageEditor-->>MCPClient: ErrorResponse
else opened
ManageEditor-->>MCPClient: SuccessResponse(prefabPath, rootName)
end
end
User->>MCPClient: manage_gameobject / manage_components
MCPClient->>ManageGameObject: modify objects inside prefab stage
MCPClient->>ManageComponents: set_property, etc.
User->>MCPClient: manage_editor(action=save_prefab_stage)
MCPClient->>ManageEditor: HandleCommand(action=save_prefab_stage)
ManageEditor->>ManageEditor: SavePrefabStage()
ManageEditor->>PrefabStageUtility: GetCurrentPrefabStage()
PrefabStageUtility-->>ManageEditor: prefabStage
alt no current prefab stage
ManageEditor-->>MCPClient: SuccessResponse("Nothing to save")
else stage open
ManageEditor->>EditorSceneManager: MarkSceneDirty(prefabStage.scene)
ManageEditor->>EditorSceneManager: SaveScene(prefabStage.scene)
EditorSceneManager-->>ManageEditor: saveResult
ManageEditor-->>MCPClient: SuccessResponse(prefabPath)
end
User->>MCPClient: manage_editor(action=close_prefab_stage)
MCPClient->>ManageEditor: HandleCommand(action=close_prefab_stage)
ManageEditor->>PrefabStageUtility: GetCurrentPrefabStage() / close logic
ManageEditor-->>MCPClient: SuccessResponse or ErrorResponse
File-Level Changes
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Hey - I've found 1 issue, and left some high level feedback:
- In
SavePrefabStage, returning aSuccessResponsewhen no prefab stage is open may make it harder for callers to distinguish a no-op from a real save; consider using an error response or including an explicitsaved: falseflag in the payload to make this state machine clearer for tools. - When opening the prefab stage, you validate that the asset is a prefab but don’t distinguish between regular, variant, or model prefabs; if these types should be handled differently by agents, consider returning the specific
PrefabAssetTypein the success payload so callers can adapt behavior.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- In `SavePrefabStage`, returning a `SuccessResponse` when no prefab stage is open may make it harder for callers to distinguish a no-op from a real save; consider using an error response or including an explicit `saved: false` flag in the payload to make this state machine clearer for tools.
- When opening the prefab stage, you validate that the asset is a prefab but don’t distinguish between regular, variant, or model prefabs; if these types should be handled differently by agents, consider returning the specific `PrefabAssetType` in the success payload so callers can adapt behavior.
## Individual Comments
### Comment 1
<location path="MCPForUnity/Editor/Tools/ManageEditor.cs" line_range="455" />
<code_context>
+
+ // Mark the prefab stage scene as dirty then save
+ EditorSceneManager.MarkSceneDirty(prefabStage.scene);
+ EditorSceneManager.SaveScene(prefabStage.scene);
+
+ return new SuccessResponse($"Saved prefab stage changes for '{prefabPath}'.", new { prefabPath });
</code_context>
<issue_to_address>
**issue (bug_risk):** Handle the `SaveScene` return value to detect save failures.
`EditorSceneManager.SaveScene` returns a `bool` indicating whether the save succeeded. The current code always returns success if no exception is thrown, so cases like write-permission errors or disk-full conditions could be misreported. Please check the return value and surface an appropriate error or message when the save fails.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
- SavePrefabStage: return ErrorResponse when no stage is open - SavePrefabStage: check SaveScene return value for save failures - OpenPrefabStage: include PrefabAssetType in success payload
|
Closing — open_prefab_stage was already merged in #968. Rebased onto beta with only save_prefab_stage in a new PR. |
Summary
Closes #976
Adds
open_prefab_stageandsave_prefab_stageactions tomanage_editor, completing the prefab stage workflow alongside the existingclose_prefab_stage.Problem
There was no way to programmatically open a prefab in Prefab Mode via MCP. Users had to manually double-click prefabs in the Editor before AI agents could modify objects inside them. The existing
manage_gameobjectandmanage_componentstools already support prefab stage (viaGameObjectLookup.GetAllSceneObjects()which checksPrefabStageUtility.GetCurrentPrefabStage()), but there was no MCP action to enter the stage.Changes
New actions in
manage_editor:open_prefab_stagepath(required)PrefabStageUtility.OpenPrefab(). Validates the asset exists and is a prefab.save_prefab_stageUpdated: Supported actions list in the error message now includes the new actions.
Workflow
Alternative headless approach (already works, no stage needed):
Implementation Details
PrefabStageUtility.OpenPrefab(path)(notAssetDatabase.OpenAsset) as it is purpose-built for Prefab ModeEditorSceneManager.MarkSceneDirty+SaveSceneon the prefab stage sceneclose_prefab_stageTest Plan
open_prefab_stage, verify stage is activemanage_componentsset_propertysave_prefab_stage, verify changes persist in the.prefabassetclose_prefab_stage, verify return to main sceneclose_prefab_stagestill works unchangedSummary by Sourcery
Add support in the editor management tool for opening and saving Unity prefab stages to complete the prefab editing workflow.
New Features:
Enhancements: