manage_prefabs
Auto-generated from the Python tool registry. Do not hand-edit outside
<!-- examples:start --><!-- examples:end -->blocks — the generator (tools/generate_docs_reference.py) will overwrite them.
Group: legacy · Module: services.tools.manage_prefabs
Maturity: stable (support status; independent of enabled state, readiness and permissions).
Description
Manages Unity Prefab assets. get_hierarchy returns bounded sibling-index depth-first pages: page_size defaults to 50 (1-500), max_depth defaults to 64 (0-128, root depth 0), and cursor continues the identical revision/options. Deeper descendants are excluded from that depth scope. Partial totals are unknown. Pages also bound managed work, metadata and output bytes. Native LoadPrefabContents still loads the full asset synchronously. Cursors expire after 30 seconds and editor reload; restart after source changes. Prefab instance IDs belong to one load; hierarchyKey identifies sibling paths across pages. Actions: get_info, get_hierarchy, create_from_gameobject, modify_contents, open_prefab_stage, save_prefab_stage, close_prefab_stage. create_from_gameobject accepts a scene/stage instance ID, unique name or full hierarchy path. Its response includes rootObjectName/rootObjectPath from the saved prefab; use these for subsequent prefab edits, or omit target to edit its root. Choose isolated modify_contents for asset edits or prefab-stage operations for interactive edits, preserving unrelated scene/stage state. Limit saves to the requested asset. create_child/delete_child accept one item or a batch; component_properties changes existing components. Resolve property names, types and reference identities before writing. Set preview_only=true with modify_contents to inspect a bounded serialized-field diff from the imported source asset to modified isolated contents without saving. This includes observed load/edit layout changes, supports component references and child edits, and reports unsupported fields and truncation. It defaults to preview_scope='prefab'. For component-property edits, preview_scope='components' observes only the selected target's named components. LoadPrefabContents still loads the full asset. Inspect scope, complete, whole_prefab_complete and observed counts. Snapshot limits are 2048 objects, 20000 properties and 4 MiB; limit errors include bounded recovery guidance. It does not issue an automatic refresh or create an apply token. Save-time callbacks and a later run may differ; custom callbacks can have external side effects. Supports object references via {"guid": "..."}, {"path": "Assets/..."}, or {"instanceID": 123}. Use asset search with filter_type='Prefab' to find the intended asset. preview_modify_contents supports only root local position/rotation/scale and set_active on a prefab without nested instances or custom components. Root name edits are refused because Unity normalizes the name to the asset filename. It returns before/after impact and a source/dependency hash. apply_preview accepts only its one-attempt token; source or dependency drift rejects before saving. Asset saves do not promise Undo or callback rollback. For scene prefab instances, trace_property follows one exact serialized leaf through its source variants and reports per-layer values, override flags and the supplying layer. list_overrides returns paged entries and groups with explicit overrideId values, property paths/current and prefab values, and added/removed components and GameObjects. revert_overrides/apply_overrides require override_ids from that listing; apply also requires the returned prefab_path. The nearest nested instance root defines the scope. Select one structural override per call or up to 100 properties. Array/compound properties and non-Transform RequireComponent structural dependencies are rejected before mutation. Check removedGameObjectCapabilities in list_overrides for the active Editor's removed GameObject list, apply and revert support. Override IDs are editor-session identifiers; list again after structural edits.
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
action | Literal['create_from_gameobject', 'get_info', 'get_hierarchy', 'modify_contents', 'preview_modify_contents', 'apply_preview', 'open_prefab_stage', 'save_prefab_stage', 'close_prefab_stage', 'list_overrides', 'trace_property', 'revert_overrides', 'apply_overrides'] | yes | Prefab operation to perform. |
prefab_path | str | None | — | Prefab asset path (e.g., Assets/Prefabs/MyPrefab.prefab). For save/close, when supplied it must match the current Prefab Stage. |
target | str | int | None | — | For create_from_gameobject: instance ID (integer or numeric string), unique name, or full hierarchy path (e.g. 'Parent/Child' or '/Parent/Child') in the active scene/current prefab stage. Stage names/paths take precedence. Numeric targets are IDs; ambiguous names/paths fail. For modify_contents: saved prefab object name or child path; omit to target the root. Use returned rootObjectName/rootObjectPath, which may differ from the source instance name. IDs do not apply to isolated prefab contents. |
allow_overwrite | bool | None | — | Allow replacing existing prefab. |
search_inactive | bool | None | — | Include inactive GameObjects in search. |
unlink_if_instance | bool | None | — | Unpack the outermost prefab instance root before creating a new prefab; child targets are rejected. A failed save may leave the scene unpacked. |
position | list[float] | dict[str, float] | str | None | — | New local position [x, y, z] or {x, y, z} for modify_contents. |
rotation | list[float] | dict[str, float] | str | None | — | New local rotation (euler angles) [x, y, z] or {x, y, z} for modify_contents. |
scale | list[float] | dict[str, float] | str | None | — | New local scale [x, y, z] or {x, y, z} for modify_contents. |
name | str | None | — | Source name for create_from_gameobject when target is omitted. In modify_contents, renames a child. The root uses the prefab asset filename. |
tag | str | None | — | New tag for the target object in modify_contents. |
layer | str | None | — | New layer name for the target object in modify_contents. |
set_active | bool | None | — | Set active state of target object in modify_contents. |
parent | str | None | — | New parent object name/path within prefab for modify_contents. |
components_to_add | list[str] | None | — | Component types to add in modify_contents. |
components_to_remove | list[str] | None | — | Remove original target components by unique type or zero-based Type[index], e.g. BoxCollider[1]. |
create_child | dict[str, Any] | list[dict[str, Any]] | None | — | Create child GameObject(s) in the prefab. Single object or array of objects, each with: name (required), parent (optional, defaults to target), source_prefab_path (optional: asset path to instantiate as nested prefab, e.g. 'Assets/Prefabs/Bullet.prefab'), primitive_type (optional: Cube, Sphere, Capsule, Cylinder, Plane, Quad), position, rotation, scale, components_to_add, tag, layer, set_active. source_prefab_path and primitive_type are mutually exclusive. |
delete_child | str | list[str] | None | — | Child name(s) or path(s) to remove from the prefab. Supports single string or array for batch deletion (e.g. 'Child1' or ['Child1', 'Child1/Grandchild']). |
component_properties | dict[str, dict[str, Any]] | None | — | Map a unique component type or zero-based Type[index] to properties. Indices count matching components after requested additions and removals. Example: {"Rigidbody": {"mass": 5.0}, "BoxCollider[1]": {"isTrigger": true}}. Supports object references via {"guid": "..."}, {"path": "Assets/..."}, or {"instanceID": 123}. For Sprite sub-assets: {"guid": "...", "spriteName": " |
override_ids | list[str] | None | — | Explicit overrideId values from list_overrides; 1-100 unique values. Required for revert_overrides/apply_overrides. Select object/component/property entries via their IDs; no implicit apply-all. |
object_id | int | None | — | list_overrides filter, or exact component ID on the trace_property target. |
property_filter | str | None | — | list_overrides: case-insensitive property-path substring filter. |
offset | int | — | list_overrides offset, zero or greater. |
page_size | int | None | — | Page size 1-500. get_hierarchy defaults to 50; list_overrides defaults to 100. |
cursor | str | None | — | get_hierarchy opaque cursor; keep identical page_size/max_depth. |
preview_id | str | None | — | One-attempt token from preview_modify_contents, valid 10 minutes. |
preview_only | bool | None | — | modify_contents only: inspect changes without saving; includes load/edit side effects, not save callbacks. No apply token; inspect complete/truncated coverage in data.preview. |
preview_scope | Literal['prefab', 'components'] | None | — | Requires preview_only=true. Default prefab observes the whole prefab. components observes only components named by component_properties on the selected target and accepts only component-property edits. Inspect data.preview.scope, complete and whole_prefab_complete. |
max_depth | int | — | get_hierarchy inclusive depth scope, root=0; default 64, maximum 128. |
property_path | str | None | — | trace_property only: exact serialized leaf path. |
unity_instance | str | int | None | — | Target Unity instance for this call: Name@hash or hash prefix; an integer selects only that exact port over stdio and is rejected over HTTP. Read mcpforunity://instances for available targets. Omit to use the legacy active selection or the sole available instance. |
Returns
A dict containing the Unity response. The exact shape depends on the action.
Examples
See the scene and prefab editing guide,
the focused prefab_create request, and the
prefab_contents preview/apply workflow.
Create from a scene object by instance ID (integer or numeric string), unique name, or full hierarchy path in the active scene/current prefab stage:
created = manage_prefabs(action="create_from_gameobject", target=-19340,
prefab_path="Assets/Prefabs/ProbeCube.prefab")
# Alternatives: target="McpProbeCube" or target="Parent/McpProbeCube".
Numeric targets are interpreted as instance IDs. Names/paths in the current prefab
stage take precedence over the active scene. Ambiguous names/paths fail; use an ID
to select exactly one object. Set search_inactive=True to include inactive objects.
The scene instance keeps its name and hierarchy. Unity's saved asset root can have
a different name (for example, ProbeCube from the file name). The create response
returns data.rootObjectName and data.rootObjectPath from the saved asset, alongside
instanceId and instanceName. Use the returned root path for subsequent edits:
manage_prefabs(action="modify_contents", prefab_path=created["data"]["prefabPath"],
target=created["data"]["rootObjectPath"], scale=[0.75, 0.75, 0.75])
# Omitting target also edits the prefab root. Child names/paths remain supported.
modify_contents loads isolated prefab contents, so scene instance IDs are not valid
targets for that action.