Skip to content

fix(prefabs): wire missing C# handler for editor/prefab-stage resource#1136

Open
slikk66 wants to merge 1 commit into
CoplayDev:betafrom
slikk66:fix-get-prefab-stage-resource
Open

fix(prefabs): wire missing C# handler for editor/prefab-stage resource#1136
slikk66 wants to merge 1 commit into
CoplayDev:betafrom
slikk66:fix-get-prefab-stage-resource

Conversation

@slikk66
Copy link
Copy Markdown

@slikk66 slikk66 commented May 19, 2026

Description

The mcpforunity://editor/prefab-stage resource was wired Python-side in PR #1013 ("Prefab stages integration") via Server/src/services/resources/prefab_stage.py, but its dispatched get_prefab_stage command name was never given a C# handler. Reading the resource hits CommandRegistry's not-found throw with:

Unknown or unsupported command type: get_prefab_stage

This change adds the missing handler at MCPForUnity/Editor/Resources/Editor/GetPrefabStage.cs, returning the 5 fields the Python PrefabStageResponse pydantic model expects (isOpen, assetPath, prefabRootName, mode, isDirty) via PrefabStageUtility.GetCurrentPrefabStage(). Mirrors the EditorState.cs sibling resource's shape exactly.

No Python-side or schema changes required — the existing Python resource already dispatches the correct command name.

Type of Change

  • Bug fix (non-breaking change that fixes an issue)

Changes Made

  • Added MCPForUnity/Editor/Resources/Editor/GetPrefabStage.cs — new [McpForUnityResource("get_prefab_stage")] static class with HandleCommand(JObject @params) method
    • Calls PrefabStageUtility.GetCurrentPrefabStage()
    • Returns { isOpen = false } when no stage open
    • Returns the 5 documented fields when a stage IS open (mode.ToString() produces "InIsolation" or "InContext")
    • Null-guards prefabContentsRoot for the (rare) domain-reload edge case
  • Added MCPForUnity/Editor/Resources/Editor/GetPrefabStage.cs.meta — Unity asset meta with fresh GUID

Testing/Screenshots/Recordings

Tested end-to-end against a local Unity 6 (Unity 6000.4.0f1) project running the patched package via UPM git URL pointing at this branch.

Test 1 — prefab open in isolation mode (Assets/UI/Prefabs/Buttons/SmallCardFrame.prefab double-clicked):

ReadMcpResourceTool(server="UnityMCP", uri="mcpforunity://editor/prefab-stage")
→ {
    "success": true,
    "message": "Retrieved prefab stage info.",
    "error": null,
    "data": {
      "isOpen": true,
      "assetPath": "Assets/UI/Prefabs/Buttons/SmallCardFrame.prefab",
      "prefabRootName": "SmallCardFrame",
      "mode": "InIsolation",
      "isDirty": false
    },
    "hint": null
  }

All 5 fields populate correctly. mode="InIsolation" confirms stage.mode.ToString() produces the string form the pydantic model expects. Python PrefabStageResponse.data validates cleanly through parse_resource_response.

Test 2 — no prefab open: trivially covered by the single null-check branch (if (stage == null) return new SuccessResponse(..., new { isOpen = false })); not exercised live in testing since no logic could differ from the inspection-evident path.

Documentation Updates

  • I have added/removed/modified tools or resources
  • If yes, I have updated all documentation files using:
    • The LLM prompt at tools/UPDATE_DOCS_PROMPT.md (recommended)
    • Manual updates following the guide at tools/UPDATE_DOCS.md

Rationale for unchecked docs sub-boxes: This is a bug-fix wiring a previously-shipped resource (editor_prefab_stage is already listed in README.md line 103's Available Resources, added by PR #1013). No new resource is introduced. Verified that recent bug-fix PRs (#1106, #1103) do not touch README.md or manifest.json — those documents are reserved for feature releases per repo convention. manifest.json tools array doesn't list resources, so no entry to add. docs/i18n/README-zh.md mirrors README and needs no change either. Happy to add a "Recent Updates" entry if reviewer prefers — current convention seems to reserve those for new tools/resources.

Related Issues

No existing GitHub issue references this bug (searched via gh search prs --repo CoplayDev/unity-mcp 'prefab stage'). Considered filing one but decided a direct PR with the fix is lower-friction since the root cause and remedy are both trivial to verify from the source.

Additional Notes

Root cause analysis (in case it helps future maintainers): the orphan came from PR #1013. That PR introduced the prefab-stages feature, adding the Python resource (Server/src/services/resources/prefab_stage.py) and three Unity-side action handlers in ManagePrefabs.cs (open_prefab_stage, save_prefab_stage, close_prefab_stage). The fourth action — get_prefab_stage — was scoped as a read-state RESOURCE (not a tool action), so it sits in MCPForUnity/Editor/Resources/Editor/ alongside EditorState.cs rather than as an action under ManagePrefabs.HandleCommand. The resource description in mcpforunity://prefab-api describes the expected 5-field return shape, which this PR delivers.

Tests: No test files added. Verified that none of the 19 sibling [McpForUnityResource] handlers across the codebase have associated C# unit tests (MCPForUnity/Editor/Resources/** has no *Tests.cs files for any handler; TestProjects/UnityMCPTests/Assets/Tests/EditMode/ covers Tools/ only). No Python test exists for prefab_stage.py either. Following established precedent of no per-handler tests for resources.

Compatibility: PrefabStageUtility.GetCurrentPrefabStage() and the PrefabStage.assetPath / prefabContentsRoot / mode / scene.isDirty API surface have all been stable since Unity 2018.3 and exist on the documented minimum supported version (Unity 2021.3 LTS+ per README.md). No backwards-compatibility shim needed.

Summary by CodeRabbit

  • New Features
    • Added ability to query the current prefab stage state in the Unity Editor, including whether a prefab stage is open, asset path, edit mode, and unsaved changes status.

Review Change Stack

The mcpforunity://editor/prefab-stage resource was wired Python-side
in PR CoplayDev#1013 ("Prefab stages integration") via
Server/src/services/resources/prefab_stage.py, but its dispatched
`get_prefab_stage` command name was never given a C# handler.
Reading the resource hits CommandRegistry's not-found throw:

    Unknown or unsupported command type: get_prefab_stage

This change adds GetPrefabStage at
MCPForUnity/Editor/Resources/Editor/GetPrefabStage.cs, returning the
5 fields the Python PrefabStageResponse pydantic model expects
(isOpen, assetPath, prefabRootName, mode, isDirty) via
PrefabStageUtility.GetCurrentPrefabStage(). Mirrors the EditorState.cs
sibling resource's shape.

No Python-side or schema changes required.
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 19, 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: a2514b04-474f-46ce-84d5-4d4c2f8b13a5

📥 Commits

Reviewing files that changed from the base of the PR and between 4c29526 and 2731ad2.

📒 Files selected for processing (2)
  • MCPForUnity/Editor/Resources/Editor/GetPrefabStage.cs
  • MCPForUnity/Editor/Resources/Editor/GetPrefabStage.cs.meta

📝 Walkthrough

Walkthrough

A new GetPrefabStage resource handler was added to expose the currently open Unity prefab stage's metadata (asset path, root name, edit mode, and dirty state) through the MCP resource interface. The handler returns isOpen=false when no stage is active, otherwise returns full stage details with exception handling for safety.

Changes

Prefab Stage Query Handler

Layer / File(s) Summary
GetPrefabStage resource handler
MCPForUnity/Editor/Resources/Editor/GetPrefabStage.cs, MCPForUnity/Editor/Resources/Editor/GetPrefabStage.cs.meta
New GetPrefabStage class decorated with [McpForUnityResource("get_prefab_stage")] exposes HandleCommand(JObject) that queries the active prefab stage via PrefabStageUtility.GetCurrentPrefabStage(). Returns SuccessResponse with isOpen=true and stage metadata (assetPath, prefabRootName, mode, isDirty), or isOpen=false when no stage is open. Wraps logic in try/catch to convert exceptions to ErrorResponse.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

  • CoplayDev/unity-mcp#627: Marks prefab stages dirty after component edits, whose dirty-flag state is now exposed by the new GetPrefabStage.isDirty reporting.

Poem

🐰 A stage awaits, with prefabs in place,
The handler queries with steady grace,
Returns the path, the mode, the state—
Is it open? Clean? Or should we wait?
MCP listens, the rabbit commits!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically describes the main change: adding a missing C# handler for the editor/prefab-stage resource.
Description check ✅ Passed The description comprehensively covers all template sections including Type of Change, Changes Made, Testing, Documentation Updates, Related Issues, and Additional Notes with extensive detail.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ 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.

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