2025-08-10 06:08:28 +08:00
|
|
|
from mcp.server.fastmcp import FastMCP, Context
|
|
|
|
|
from typing import Dict, Any
|
2025-08-11 13:49:24 +08:00
|
|
|
from unity_connection import get_unity_connection, send_command_with_retry
|
|
|
|
|
from config import config
|
2025-08-10 06:08:28 +08:00
|
|
|
import time
|
|
|
|
|
|
2025-09-09 07:39:47 +08:00
|
|
|
from telemetry_decorator import telemetry_tool
|
|
|
|
|
|
2025-08-10 06:08:28 +08:00
|
|
|
def register_manage_scene_tools(mcp: FastMCP):
|
|
|
|
|
"""Register all scene management tools with the MCP server."""
|
|
|
|
|
|
|
|
|
|
@mcp.tool()
|
2025-09-09 07:39:47 +08:00
|
|
|
@telemetry_tool("manage_scene")
|
2025-08-10 06:08:28 +08:00
|
|
|
def manage_scene(
|
2025-09-10 09:46:42 +08:00
|
|
|
ctx: Context,
|
2025-08-10 06:08:28 +08:00
|
|
|
action: str,
|
|
|
|
|
name: str,
|
|
|
|
|
path: str,
|
2025-09-09 11:06:04 +08:00
|
|
|
build_index: Any,
|
2025-08-10 06:08:28 +08:00
|
|
|
) -> Dict[str, Any]:
|
|
|
|
|
"""Manages Unity scenes (load, save, create, get hierarchy, etc.).
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
action: Operation (e.g., 'load', 'save', 'create', 'get_hierarchy').
|
|
|
|
|
name: Scene name (no extension) for create/load/save.
|
|
|
|
|
path: Asset path for scene operations (default: "Assets/").
|
|
|
|
|
build_index: Build index for load/build settings actions.
|
|
|
|
|
# Add other action-specific args as needed (e.g., for hierarchy depth)
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
Dictionary with results ('success', 'message', 'data').
|
|
|
|
|
"""
|
|
|
|
|
try:
|
2025-09-09 11:06:04 +08:00
|
|
|
# Coerce numeric inputs defensively
|
|
|
|
|
def _coerce_int(value, default=None):
|
|
|
|
|
if value is None:
|
|
|
|
|
return default
|
|
|
|
|
try:
|
|
|
|
|
if isinstance(value, bool):
|
|
|
|
|
return default
|
|
|
|
|
if isinstance(value, int):
|
|
|
|
|
return int(value)
|
|
|
|
|
s = str(value).strip()
|
|
|
|
|
if s.lower() in ("", "none", "null"):
|
|
|
|
|
return default
|
|
|
|
|
return int(float(s))
|
|
|
|
|
except Exception:
|
|
|
|
|
return default
|
|
|
|
|
|
|
|
|
|
build_index = _coerce_int(build_index, default=0)
|
|
|
|
|
|
2025-08-10 06:08:28 +08:00
|
|
|
params = {
|
|
|
|
|
"action": action,
|
|
|
|
|
"name": name,
|
|
|
|
|
"path": path,
|
|
|
|
|
"buildIndex": build_index
|
|
|
|
|
}
|
|
|
|
|
params = {k: v for k, v in params.items() if v is not None}
|
|
|
|
|
|
2025-08-11 13:49:24 +08:00
|
|
|
# Use centralized retry helper
|
|
|
|
|
response = send_command_with_retry("manage_scene", params)
|
2025-08-10 06:08:28 +08:00
|
|
|
|
2025-08-11 13:49:24 +08:00
|
|
|
# Preserve structured failure data; unwrap success into a friendlier shape
|
|
|
|
|
if isinstance(response, dict) and response.get("success"):
|
2025-08-10 06:08:28 +08:00
|
|
|
return {"success": True, "message": response.get("message", "Scene operation successful."), "data": response.get("data")}
|
2025-08-11 13:49:24 +08:00
|
|
|
return response if isinstance(response, dict) else {"success": False, "message": str(response)}
|
2025-08-10 06:08:28 +08:00
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
return {"success": False, "message": f"Python error managing scene: {str(e)}"}
|