|
| 1 | +""" |
| 2 | +Tool to list all available Unity Editor instances. |
| 3 | +""" |
| 4 | +from typing import Annotated, Any |
| 5 | + |
| 6 | +from fastmcp import Context |
| 7 | +from registry import mcp_for_unity_tool |
| 8 | +from unity_connection import get_unity_connection_pool |
| 9 | + |
| 10 | + |
| 11 | +@mcp_for_unity_tool(description="List all running Unity Editor instances with their details.") |
| 12 | +def list_unity_instances( |
| 13 | + ctx: Context, |
| 14 | + force_refresh: Annotated[bool, "Force refresh the instance list, bypassing cache"] = False |
| 15 | +) -> dict[str, Any]: |
| 16 | + """ |
| 17 | + List all available Unity Editor instances. |
| 18 | +
|
| 19 | + Returns information about each instance including: |
| 20 | + - id: Unique identifier (ProjectName@hash) |
| 21 | + - name: Project name |
| 22 | + - path: Full project path |
| 23 | + - hash: 8-character hash of project path |
| 24 | + - port: TCP port number |
| 25 | + - status: Current status (running, reloading, etc.) |
| 26 | + - last_heartbeat: Last heartbeat timestamp |
| 27 | + - unity_version: Unity version (if available) |
| 28 | +
|
| 29 | + Args: |
| 30 | + force_refresh: If True, bypass cache and scan immediately |
| 31 | +
|
| 32 | + Returns: |
| 33 | + Dictionary containing list of instances and metadata |
| 34 | + """ |
| 35 | + ctx.info(f"Listing Unity instances (force_refresh={force_refresh})") |
| 36 | + |
| 37 | + try: |
| 38 | + pool = get_unity_connection_pool() |
| 39 | + instances = pool.discover_all_instances(force_refresh=force_refresh) |
| 40 | + |
| 41 | + # Check for duplicate project names |
| 42 | + name_counts = {} |
| 43 | + for inst in instances: |
| 44 | + name_counts[inst.name] = name_counts.get(inst.name, 0) + 1 |
| 45 | + |
| 46 | + duplicates = [name for name, count in name_counts.items() if count > 1] |
| 47 | + |
| 48 | + result = { |
| 49 | + "success": True, |
| 50 | + "instance_count": len(instances), |
| 51 | + "instances": [inst.to_dict() for inst in instances], |
| 52 | + } |
| 53 | + |
| 54 | + if duplicates: |
| 55 | + result["warning"] = ( |
| 56 | + f"Multiple instances found with duplicate project names: {duplicates}. " |
| 57 | + f"Use full format (e.g., 'ProjectName@hash') to specify which instance." |
| 58 | + ) |
| 59 | + |
| 60 | + return result |
| 61 | + |
| 62 | + except Exception as e: |
| 63 | + ctx.error(f"Error listing Unity instances: {e}") |
| 64 | + return { |
| 65 | + "success": False, |
| 66 | + "error": f"Failed to list Unity instances: {str(e)}", |
| 67 | + "instance_count": 0, |
| 68 | + "instances": [] |
| 69 | + } |
| 70 | + |
0 commit comments