Skip to content

feat(manage_editor): add save_prefab_stage action#990

Open
zaferdace wants to merge 1 commit intoCoplayDev:betafrom
zaferdace:feat/save-prefab-stage
Open

feat(manage_editor): add save_prefab_stage action#990
zaferdace wants to merge 1 commit intoCoplayDev:betafrom
zaferdace:feat/save-prefab-stage

Conversation

@zaferdace
Copy link
Contributor

@zaferdace zaferdace commented Mar 26, 2026

Summary

Complements #968 (open_prefab_stage) by adding the missing save_prefab_stage action to manage_editor, completing the prefab stage edit workflow.

What save_prefab_stage does

  • Checks if a prefab stage is currently open via PrefabStageUtility.GetCurrentPrefabStage()
  • Returns ErrorResponse immediately if no stage is open (guides the caller to use open_prefab_stage first)
  • Marks the prefab stage scene dirty with EditorSceneManager.MarkSceneDirty
  • Calls EditorSceneManager.SaveScene and checks the return value
  • Returns ErrorResponse if the save failed (read-only file, full disk, etc.)
  • Returns SuccessResponse with prefabPath and saved on success

Full prefab stage workflow

manage_editor(action="open_prefab_stage", path="Assets/Prefabs/MyPrefab.prefab")
  → manage_gameobject / manage_components  (modify objects inside)
  → manage_editor(action="save_prefab_stage")
  → manage_editor(action="close_prefab_stage")

Changes

  • C#: SavePrefabStage() method + case "save_prefab_stage" in ManageEditor.cs
  • Python: save_prefab_stage added to action Literal and tool description in manage_editor.py
  • Tests: 3 new Python tests — description advertises the action, forwarding to Unity works, no spurious params included

Test plan

  • Python tests: 28 passed (includes 3 new save_prefab_stage tests)
  • Open a prefab via open_prefab_stage, modify an object, call save_prefab_stage, verify changes persist in the .prefab asset
  • Call save_prefab_stage with no stage open — verify ErrorResponse with helpful message
  • Verify close_prefab_stage still works unchanged after saving

Notes

open_prefab_stage was merged in #968. This PR only adds save_prefab_stage and is based on beta.

Summary by Sourcery

Add support for saving the currently open Unity prefab stage through the manage_editor tool and wire it through to the Unity editor backend.

New Features:

  • Expose a new save_prefab_stage action in the manage_editor tool API to save changes in the active prefab stage back to the prefab asset.

Enhancements:

  • Update the Unity editor command handler and error messaging to recognize save_prefab_stage alongside other prefab stage actions.

Tests:

  • Add Python tests to ensure save_prefab_stage is documented in the manage_editor tool description, correctly forwards to Unity, and omits unrelated parameters in its payload.

Summary by CodeRabbit

  • New Features

    • Added a new save prefab stage action, enabling users to save changes in the currently open prefab stage directly back to the original prefab asset.
  • Tests

    • Added comprehensive test coverage for the new prefab stage save functionality, including validation of action forwarding and parameter handling.

Adds save_prefab_stage to manage_editor to complete the prefab stage
workflow alongside the existing open_prefab_stage and close_prefab_stage.

- C#: SavePrefabStage() uses EditorSceneManager.MarkSceneDirty +
  SaveScene on the prefab stage scene, returns ErrorResponse when no
  stage is open or save fails
- Python: adds save_prefab_stage to action Literal and tool description
- Tests: 3 new tests covering forwarding, description, and clean params

open_prefab_stage was already merged in CoplayDev#968. This PR only adds
save_prefab_stage.
@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Mar 26, 2026

Reviewer's Guide

Adds a new manage_editor action save_prefab_stage that saves the currently open Unity prefab stage, wires it through the Python tool surface, and adds tests to verify description, forwarding behavior, and parameter filtering.

Sequence diagram for save_prefab_stage tool call workflow

sequenceDiagram
    actor User
    participant Server_manage_editor_tool
    participant UnityConnection
    participant Unity_ManageEditor

    User->>Server_manage_editor_tool: manage_editor(action=save_prefab_stage)
    Server_manage_editor_tool->>UnityConnection: async_send_command_with_retry(action=save_prefab_stage)
    UnityConnection->>Unity_ManageEditor: HandleCommand(params.action=save_prefab_stage)
    Unity_ManageEditor->>Unity_ManageEditor: SavePrefabStage()
    Unity_ManageEditor-->>UnityConnection: SuccessResponse | ErrorResponse
    UnityConnection-->>Server_manage_editor_tool: response JSON
    Server_manage_editor_tool-->>User: tool result (prefabPath, saved | error message)
Loading

Class diagram for ManageEditor SavePrefabStage integration

classDiagram
    class ManageEditor {
        +static object HandleCommand(JObject params)
        -static object OpenPrefabStage(string requestedPath)
        -static object SavePrefabStage()
        -static object ClosePrefabStage()
    }

    class ErrorResponse {
        +string message
        +ErrorResponse(string message)
    }

    class SuccessResponse {
        +string message
        +object data
        +SuccessResponse(string message, object data)
    }

    ManageEditor ..> ErrorResponse : returns
    ManageEditor ..> SuccessResponse : returns
Loading

Flow diagram for SavePrefabStage Unity editor logic

flowchart TD
    Start([Start SavePrefabStage])
    GetStage[Get current prefab stage via PrefabStageUtility.GetCurrentPrefabStage]
    NoStage[Return ErrorResponse: not in prefab editing mode]
    GetPath[Read prefabPath from prefabStage.assetPath]
    MarkDirty["MarkSceneDirty(prefabStage.scene)"]
    SaveScene["Call EditorSceneManager.SaveScene(prefabStage.scene)"]
    SaveFailed[Return ErrorResponse: save failed]
    SaveSuccess[Return SuccessResponse with prefabPath and saved=true]
    ExceptionNode[Return ErrorResponse with exception message]

    Start --> GetStage
    GetStage -->|prefabStage is null| NoStage
    GetStage -->|prefabStage exists| GetPath
    GetPath --> MarkDirty --> SaveScene
    SaveScene -->|saved == false| SaveFailed
    SaveScene -->|saved == true| SaveSuccess

    Start --> ExceptionCheck{Exception thrown?}
    ExceptionCheck -->|yes| ExceptionNode
    ExceptionCheck -->|no| GetStage
Loading

File-Level Changes

Change Details Files
Expose new save_prefab_stage action from the Python manage_editor tool to Unity and document it in the tool description and action Literal.
  • Extend manage_editor tool description string to mention save_prefab_stage and describe its behavior in the prefab workflow.
  • Add save_prefab_stage to the Literal action union so callers can invoke it from Python/clients.
  • Keep the existing optional parameters (tool_name, tag_name, layer_name, path) unchanged so the new action reuses the same call surface without sending them if unused.
Server/src/services/tools/manage_editor.py
Implement SavePrefabStage behavior in the Unity-side ManageEditor handler and register the new action in the dispatcher and error message.
  • Add a new switch case for save_prefab_stage in HandleCommand that calls SavePrefabStage().
  • Extend the unknown-action ErrorResponse message to list save_prefab_stage as a supported action.
  • Implement SavePrefabStage to validate an open prefab stage, mark the scene dirty, call EditorSceneManager.SaveScene, and return either ErrorResponse on failure or SuccessResponse with prefabPath and saved on success, with a generic catch for unexpected exceptions.
MCPForUnity/Editor/Tools/ManageEditor.cs
Add tests to ensure save_prefab_stage is advertised, correctly forwarded to Unity, and does not send spurious parameters.
  • Include save_prefab_stage in UNITY_FORWARDED_ACTIONS so it is treated as a Unity-forwarded manage_editor action.
  • Add a test that manage_editor’s tool description string contains save_prefab_stage.
  • Add a test that calling manage_editor(action="save_prefab_stage") forwards the action to Unity successfully.
  • Add a test asserting that the forwarded params omit irrelevant keys like toolName, tagName, layerName, prefabPath, and path for save_prefab_stage.
Server/tests/test_manage_editor.py

Possibly linked issues

  • #(unlisted): PR’s save_prefab_stage, together with open_prefab_stage, enables editing prefab contents and persisting serialized field changes.

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 26, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: ab170624-d632-4701-af07-f6b78a0b37f1

📥 Commits

Reviewing files that changed from the base of the PR and between c348a3d and fea9ab8.

📒 Files selected for processing (3)
  • MCPForUnity/Editor/Tools/ManageEditor.cs
  • Server/src/services/tools/manage_editor.py
  • Server/tests/test_manage_editor.py

📝 Walkthrough

Walkthrough

This PR introduces a new save_prefab_stage action to the ManageEditor tool, enabling the saving of the currently open prefab stage back to disk. The implementation spans the C# Unity editor plugin (command handler and save logic), the server-side tool definition with updated type annotations, and comprehensive test coverage validating both tool documentation and command forwarding behavior.

Changes

Cohort / File(s) Summary
Unity Editor Plugin
MCPForUnity/Editor/Tools/ManageEditor.cs
Added save_prefab_stage command handler that retrieves the active PrefabStage, saves the scene via EditorSceneManager.SaveScene, and returns a response with prefabPath and saved status. Includes error handling for missing stages and save failures. Updated error message to list the new action in supported actions.
Server Tool Definition
Server/src/services/tools/manage_editor.py
Extended the manage_editor function's action parameter Literal type to include "save_prefab_stage". Updated the tool description to advertise the new save capability for the currently open prefab stage.
Test Coverage
Server/tests/test_manage_editor.py
Added tests verifying that save_prefab_stage is included in the tool's advertised capabilities, that the action is correctly forwarded to Unity with proper parameters, and that nullable parameters (toolName, tagName, layerName, prefabPath, path) are omitted from the command payload.

Sequence Diagram

sequenceDiagram
    participant Client as Client/LLM
    participant Server as MCP Server
    participant Tool as manage_editor Tool
    participant Unity as Unity Editor
    participant Editor as EditorSceneManager

    Client->>Server: save_prefab_stage action
    Server->>Tool: invoke manage_editor(action="save_prefab_stage")
    Tool->>Unity: forward save_prefab_stage command
    Unity->>Unity: retrieve current PrefabStage
    alt PrefabStage exists
        Unity->>Unity: mark stage scene as dirty
        Unity->>Editor: SaveScene(stage.scene)
        Editor-->>Unity: scene saved
        Unity-->>Tool: success response {prefabPath, saved: true}
    else No PrefabStage active
        Unity-->>Tool: error response
    end
    Tool-->>Server: response
    Server-->>Client: result
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related PRs

Poem

🐰 A prefab waits in the editor's embrace,
I save it with grace to its rightful place,
The stage holds content, the scene glows bright,
Now persisted to disk—unity takes flight! ✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately and concisely describes the main change: adding a new save_prefab_stage action to the manage_editor tool.
Description check ✅ Passed The description is comprehensive and well-structured, covering the feature purpose, implementation details, testing, and workflow context beyond the template requirements.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant