mcpforunityserver 9.4.0b20260203025228__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (105) hide show
  1. cli/__init__.py +3 -0
  2. cli/commands/__init__.py +3 -0
  3. cli/commands/animation.py +84 -0
  4. cli/commands/asset.py +280 -0
  5. cli/commands/audio.py +125 -0
  6. cli/commands/batch.py +171 -0
  7. cli/commands/code.py +182 -0
  8. cli/commands/component.py +190 -0
  9. cli/commands/editor.py +447 -0
  10. cli/commands/gameobject.py +487 -0
  11. cli/commands/instance.py +93 -0
  12. cli/commands/lighting.py +123 -0
  13. cli/commands/material.py +239 -0
  14. cli/commands/prefab.py +248 -0
  15. cli/commands/scene.py +231 -0
  16. cli/commands/script.py +222 -0
  17. cli/commands/shader.py +226 -0
  18. cli/commands/texture.py +540 -0
  19. cli/commands/tool.py +58 -0
  20. cli/commands/ui.py +258 -0
  21. cli/commands/vfx.py +421 -0
  22. cli/main.py +281 -0
  23. cli/utils/__init__.py +31 -0
  24. cli/utils/config.py +58 -0
  25. cli/utils/confirmation.py +37 -0
  26. cli/utils/connection.py +254 -0
  27. cli/utils/constants.py +23 -0
  28. cli/utils/output.py +195 -0
  29. cli/utils/parsers.py +112 -0
  30. cli/utils/suggestions.py +34 -0
  31. core/__init__.py +0 -0
  32. core/config.py +67 -0
  33. core/constants.py +4 -0
  34. core/logging_decorator.py +37 -0
  35. core/telemetry.py +551 -0
  36. core/telemetry_decorator.py +164 -0
  37. main.py +845 -0
  38. mcpforunityserver-9.4.0b20260203025228.dist-info/METADATA +328 -0
  39. mcpforunityserver-9.4.0b20260203025228.dist-info/RECORD +105 -0
  40. mcpforunityserver-9.4.0b20260203025228.dist-info/WHEEL +5 -0
  41. mcpforunityserver-9.4.0b20260203025228.dist-info/entry_points.txt +3 -0
  42. mcpforunityserver-9.4.0b20260203025228.dist-info/licenses/LICENSE +21 -0
  43. mcpforunityserver-9.4.0b20260203025228.dist-info/top_level.txt +7 -0
  44. models/__init__.py +4 -0
  45. models/models.py +56 -0
  46. models/unity_response.py +70 -0
  47. services/__init__.py +0 -0
  48. services/api_key_service.py +235 -0
  49. services/custom_tool_service.py +499 -0
  50. services/registry/__init__.py +22 -0
  51. services/registry/resource_registry.py +53 -0
  52. services/registry/tool_registry.py +51 -0
  53. services/resources/__init__.py +86 -0
  54. services/resources/active_tool.py +48 -0
  55. services/resources/custom_tools.py +57 -0
  56. services/resources/editor_state.py +304 -0
  57. services/resources/gameobject.py +243 -0
  58. services/resources/layers.py +30 -0
  59. services/resources/menu_items.py +35 -0
  60. services/resources/prefab.py +191 -0
  61. services/resources/prefab_stage.py +40 -0
  62. services/resources/project_info.py +40 -0
  63. services/resources/selection.py +56 -0
  64. services/resources/tags.py +31 -0
  65. services/resources/tests.py +88 -0
  66. services/resources/unity_instances.py +125 -0
  67. services/resources/windows.py +48 -0
  68. services/state/external_changes_scanner.py +245 -0
  69. services/tools/__init__.py +83 -0
  70. services/tools/batch_execute.py +93 -0
  71. services/tools/debug_request_context.py +86 -0
  72. services/tools/execute_custom_tool.py +43 -0
  73. services/tools/execute_menu_item.py +32 -0
  74. services/tools/find_gameobjects.py +110 -0
  75. services/tools/find_in_file.py +181 -0
  76. services/tools/manage_asset.py +119 -0
  77. services/tools/manage_components.py +131 -0
  78. services/tools/manage_editor.py +64 -0
  79. services/tools/manage_gameobject.py +260 -0
  80. services/tools/manage_material.py +111 -0
  81. services/tools/manage_prefabs.py +209 -0
  82. services/tools/manage_scene.py +111 -0
  83. services/tools/manage_script.py +645 -0
  84. services/tools/manage_scriptable_object.py +87 -0
  85. services/tools/manage_shader.py +71 -0
  86. services/tools/manage_texture.py +581 -0
  87. services/tools/manage_vfx.py +120 -0
  88. services/tools/preflight.py +110 -0
  89. services/tools/read_console.py +151 -0
  90. services/tools/refresh_unity.py +153 -0
  91. services/tools/run_tests.py +317 -0
  92. services/tools/script_apply_edits.py +1006 -0
  93. services/tools/set_active_instance.py +120 -0
  94. services/tools/utils.py +348 -0
  95. transport/__init__.py +0 -0
  96. transport/legacy/port_discovery.py +329 -0
  97. transport/legacy/stdio_port_registry.py +65 -0
  98. transport/legacy/unity_connection.py +910 -0
  99. transport/models.py +68 -0
  100. transport/plugin_hub.py +787 -0
  101. transport/plugin_registry.py +182 -0
  102. transport/unity_instance_middleware.py +262 -0
  103. transport/unity_transport.py +94 -0
  104. utils/focus_nudge.py +589 -0
  105. utils/module_discovery.py +55 -0
main.py ADDED
@@ -0,0 +1,845 @@
1
+ from starlette.requests import Request
2
+ from transport.unity_instance_middleware import (
3
+ UnityInstanceMiddleware,
4
+ get_unity_instance_middleware
5
+ )
6
+ from services.api_key_service import ApiKeyService
7
+ from transport.legacy.unity_connection import get_unity_connection_pool, UnityConnectionPool
8
+ from services.tools import register_all_tools
9
+ from core.telemetry import record_milestone, record_telemetry, MilestoneType, RecordType, get_package_version
10
+ from services.resources import register_all_resources
11
+ from transport.plugin_registry import PluginRegistry
12
+ from transport.plugin_hub import PluginHub
13
+ from services.custom_tool_service import (
14
+ CustomToolService,
15
+ resolve_project_id_for_unity_instance,
16
+ )
17
+ from core.config import config
18
+ from starlette.routing import WebSocketRoute
19
+ from starlette.responses import JSONResponse
20
+ import argparse
21
+ import asyncio
22
+ import logging
23
+ from contextlib import asynccontextmanager
24
+ import os
25
+ import threading
26
+ import time
27
+ from typing import AsyncIterator, Any
28
+ from urllib.parse import urlparse
29
+
30
+ # Workaround for environments where tool signature evaluation runs with a globals
31
+ # dict that does not include common `typing` names (e.g. when annotations are strings
32
+ # and evaluated via `eval()` during schema generation).
33
+ # Making these names available in builtins avoids `NameError: Annotated/Literal/... is not defined`.
34
+ try: # pragma: no cover - startup safety guard
35
+ import builtins
36
+ import typing as _typing
37
+
38
+ _typing_names = (
39
+ "Annotated",
40
+ "Literal",
41
+ "Any",
42
+ "Union",
43
+ "Optional",
44
+ "Dict",
45
+ "List",
46
+ "Tuple",
47
+ "Set",
48
+ "FrozenSet",
49
+ )
50
+ for _name in _typing_names:
51
+ if not hasattr(builtins, _name) and hasattr(_typing, _name):
52
+ # type: ignore[attr-defined]
53
+ setattr(builtins, _name, getattr(_typing, _name))
54
+ except Exception:
55
+ pass
56
+
57
+ from fastmcp import FastMCP
58
+ from logging.handlers import RotatingFileHandler
59
+
60
+
61
+ class WindowsSafeRotatingFileHandler(RotatingFileHandler):
62
+ """RotatingFileHandler that gracefully handles Windows file locking during rotation."""
63
+
64
+ def doRollover(self):
65
+ """Override to catch PermissionError on Windows when log file is locked."""
66
+ try:
67
+ super().doRollover()
68
+ except PermissionError:
69
+ # On Windows, another process may have the log file open.
70
+ # Skip rotation this time - we'll try again on the next rollover.
71
+ pass
72
+
73
+
74
+ # Configure logging using settings from config
75
+ logging.basicConfig(
76
+ level=getattr(logging, config.log_level),
77
+ format=config.log_format,
78
+ stream=None, # None -> defaults to sys.stderr; avoid stdout used by MCP stdio
79
+ force=True # Ensure our handler replaces any prior stdout handlers
80
+ )
81
+ logger = logging.getLogger("mcp-for-unity-server")
82
+
83
+ # Also write logs to a rotating file so logs are available when launched via stdio
84
+ try:
85
+ _log_dir = os.path.join(os.path.expanduser(
86
+ "~/Library/Application Support/UnityMCP"), "Logs")
87
+ os.makedirs(_log_dir, exist_ok=True)
88
+ _file_path = os.path.join(_log_dir, "unity_mcp_server.log")
89
+ _fh = WindowsSafeRotatingFileHandler(
90
+ _file_path, maxBytes=512*1024, backupCount=2, encoding="utf-8")
91
+ _fh.setFormatter(logging.Formatter(config.log_format))
92
+ _fh.setLevel(getattr(logging, config.log_level))
93
+ logger.addHandler(_fh)
94
+ logger.propagate = False # Prevent double logging to root logger
95
+ # Also route telemetry logger to the same rotating file and normal level
96
+ try:
97
+ tlog = logging.getLogger("unity-mcp-telemetry")
98
+ tlog.setLevel(getattr(logging, config.log_level))
99
+ tlog.addHandler(_fh)
100
+ tlog.propagate = False # Prevent double logging for telemetry too
101
+ except Exception as exc:
102
+ # Never let logging setup break startup
103
+ logger.debug("Failed to configure telemetry logger", exc_info=exc)
104
+ except Exception as exc:
105
+ # Never let logging setup break startup
106
+ logger.debug("Failed to configure main logger file handler", exc_info=exc)
107
+ # Quieten noisy third-party loggers to avoid clutter during stdio handshake
108
+ for noisy in ("httpx", "urllib3", "mcp.server.lowlevel.server"):
109
+ try:
110
+ logging.getLogger(noisy).setLevel(
111
+ max(logging.WARNING, getattr(logging, config.log_level)))
112
+ logging.getLogger(noisy).propagate = False
113
+ except Exception:
114
+ pass
115
+
116
+ # Import telemetry only after logging is configured to ensure its logs use stderr and proper levels
117
+ # Ensure a slightly higher telemetry timeout unless explicitly overridden by env
118
+ try:
119
+
120
+ # Ensure generous timeout unless explicitly overridden by env
121
+ if not os.environ.get("UNITY_MCP_TELEMETRY_TIMEOUT"):
122
+ os.environ["UNITY_MCP_TELEMETRY_TIMEOUT"] = "5.0"
123
+ except Exception:
124
+ pass
125
+
126
+ # Global connection pool
127
+ _unity_connection_pool: UnityConnectionPool | None = None
128
+ _plugin_registry: PluginRegistry | None = None
129
+
130
+ # Cached server version (set at startup to avoid repeated I/O)
131
+ _server_version: str | None = None
132
+
133
+ # In-memory custom tool service initialized after MCP construction
134
+ custom_tool_service: CustomToolService | None = None
135
+
136
+
137
+ @asynccontextmanager
138
+ async def server_lifespan(server: FastMCP) -> AsyncIterator[dict[str, Any]]:
139
+ """Handle server startup and shutdown."""
140
+ global _unity_connection_pool, _server_version
141
+ _server_version = get_package_version()
142
+ logger.info(f"MCP for Unity Server v{_server_version} starting up")
143
+
144
+ # Register custom tool management endpoints with FastMCP
145
+ # Routes are declared globally below after FastMCP initialization
146
+
147
+ # Note: When using HTTP transport, FastMCP handles the HTTP server
148
+ # Tool registration will be handled through FastMCP endpoints
149
+ enable_http_server = os.environ.get(
150
+ "UNITY_MCP_ENABLE_HTTP_SERVER", "").lower() in ("1", "true", "yes", "on")
151
+ if enable_http_server:
152
+ http_host = os.environ.get("UNITY_MCP_HTTP_HOST", "localhost")
153
+ http_port = int(os.environ.get("UNITY_MCP_HTTP_PORT", "8080"))
154
+ logger.info(
155
+ f"HTTP tool registry will be available on http://{http_host}:{http_port}")
156
+
157
+ global _plugin_registry
158
+ if _plugin_registry is None:
159
+ _plugin_registry = PluginRegistry()
160
+ loop = asyncio.get_running_loop()
161
+ PluginHub.configure(_plugin_registry, loop)
162
+
163
+ # Record server startup telemetry
164
+ start_time = time.time()
165
+ start_clk = time.perf_counter()
166
+ # Defer initial telemetry by 1s to avoid stdio handshake interference
167
+
168
+ def _emit_startup():
169
+ try:
170
+ record_telemetry(RecordType.STARTUP, {
171
+ "server_version": _server_version,
172
+ "startup_time": start_time,
173
+ })
174
+ record_milestone(MilestoneType.FIRST_STARTUP)
175
+ except Exception:
176
+ logger.debug("Deferred startup telemetry failed", exc_info=True)
177
+ threading.Timer(1.0, _emit_startup).start()
178
+
179
+ try:
180
+ skip_connect = os.environ.get(
181
+ "UNITY_MCP_SKIP_STARTUP_CONNECT", "").lower() in ("1", "true", "yes", "on")
182
+ if skip_connect:
183
+ logger.info(
184
+ "Skipping Unity connection on startup (UNITY_MCP_SKIP_STARTUP_CONNECT=1)")
185
+ else:
186
+ # Initialize connection pool and discover instances
187
+ _unity_connection_pool = get_unity_connection_pool()
188
+ instances = _unity_connection_pool.discover_all_instances()
189
+
190
+ if instances:
191
+ logger.info(
192
+ f"Discovered {len(instances)} Unity instance(s): {[i.id for i in instances]}")
193
+
194
+ # Try to connect to default instance
195
+ try:
196
+ _unity_connection_pool.get_connection()
197
+ logger.info(
198
+ "Connected to default Unity instance on startup")
199
+
200
+ # Record successful Unity connection (deferred)
201
+ threading.Timer(1.0, lambda: record_telemetry(
202
+ RecordType.UNITY_CONNECTION,
203
+ {
204
+ "status": "connected",
205
+ "connection_time_ms": (time.perf_counter() - start_clk) * 1000,
206
+ "instance_count": len(instances)
207
+ }
208
+ )).start()
209
+ except Exception as e:
210
+ logger.warning(
211
+ f"Could not connect to default Unity instance: {e}")
212
+ else:
213
+ logger.warning("No Unity instances found on startup")
214
+
215
+ except ConnectionError as e:
216
+ logger.warning(f"Could not connect to Unity on startup: {e}")
217
+
218
+ # Record connection failure (deferred)
219
+ _err_msg = str(e)[:200]
220
+ threading.Timer(1.0, lambda: record_telemetry(
221
+ RecordType.UNITY_CONNECTION,
222
+ {
223
+ "status": "failed",
224
+ "error": _err_msg,
225
+ "connection_time_ms": (time.perf_counter() - start_clk) * 1000,
226
+ }
227
+ )).start()
228
+ except Exception as e:
229
+ logger.warning(f"Unexpected error connecting to Unity on startup: {e}")
230
+ _err_msg = str(e)[:200]
231
+ threading.Timer(1.0, lambda: record_telemetry(
232
+ RecordType.UNITY_CONNECTION,
233
+ {
234
+ "status": "failed",
235
+ "error": _err_msg,
236
+ "connection_time_ms": (time.perf_counter() - start_clk) * 1000,
237
+ }
238
+ )).start()
239
+
240
+ try:
241
+ # Yield shared state for lifespan consumers (e.g., middleware)
242
+ yield {
243
+ "pool": _unity_connection_pool,
244
+ "plugin_registry": _plugin_registry,
245
+ }
246
+ finally:
247
+ if _unity_connection_pool:
248
+ _unity_connection_pool.disconnect_all()
249
+ logger.info("MCP for Unity Server shut down")
250
+
251
+
252
+ def _build_instructions(project_scoped_tools: bool) -> str:
253
+ if project_scoped_tools:
254
+ custom_tools_note = (
255
+ "I have a dynamic tool system. Always check the mcpforunity://custom-tools resource first "
256
+ "to see what special capabilities are available for the current project."
257
+ )
258
+ else:
259
+ custom_tools_note = (
260
+ "Custom tools are registered as standard tools when Unity connects. "
261
+ "No project-scoped custom tools resource is available."
262
+ )
263
+
264
+ return f"""
265
+ This server provides tools to interact with the Unity Game Engine Editor.
266
+
267
+ {custom_tools_note}
268
+
269
+ Targeting Unity instances:
270
+ - Use the resource mcpforunity://instances to list active Unity sessions (Name@hash).
271
+ - When multiple instances are connected, call set_active_instance with the exact Name@hash before using tools/resources. The server will error if multiple are connected and no active instance is set.
272
+
273
+ Important Workflows:
274
+
275
+ Resources vs Tools:
276
+ - Use RESOURCES to read editor state (editor_state, project_info, project_tags, tests, etc)
277
+ - Use TOOLS to perform actions and mutations (manage_editor for play mode control, tag/layer management, etc)
278
+ - Always check related resources before modifying the engine state with tools
279
+
280
+ Script Management:
281
+ - After creating or modifying scripts (by your own tools or the `manage_script` tool) use `read_console` to check for compilation errors before proceeding
282
+ - Only after successful compilation can new components/types be used
283
+ - You can poll the `editor_state` resource's `isCompiling` field to check if the domain reload is complete
284
+
285
+ Scene Setup:
286
+ - Always include a Camera and main Light (Directional Light) in new scenes
287
+ - Create prefabs with `manage_asset` for reusable GameObjects
288
+ - Use `manage_scene` to load, save, and query scene information
289
+
290
+ Path Conventions:
291
+ - Unless specified otherwise, all paths are relative to the project's `Assets/` folder
292
+ - Use forward slashes (/) in paths for cross-platform compatibility
293
+
294
+ Console Monitoring:
295
+ - Check `read_console` regularly to catch errors, warnings, and compilation status
296
+ - Filter by log type (Error, Warning, Log) to focus on specific issues
297
+
298
+ Menu Items:
299
+ - Use `execute_menu_item` when you have read the menu items resource
300
+ - This lets you interact with Unity's menu system and third-party tools
301
+
302
+ Payload sizing & paging (important):
303
+ - Many Unity queries can return very large JSON. Prefer **paged + summary-first** calls.
304
+ - `manage_scene(action="get_hierarchy")`:
305
+ - Use `page_size` + `cursor` and follow `next_cursor` until null.
306
+ - `page_size` is **items per page**; recommended starting point: **50**.
307
+ - `manage_gameobject(action="get_components")`:
308
+ - Start with `include_properties=false` (metadata-only) and small `page_size` (e.g. **10-25**).
309
+ - Only request `include_properties=true` when needed; keep `page_size` small (e.g. **3-10**) to bound payloads.
310
+ - `manage_asset(action="search")`:
311
+ - Use paging (`page_size`, `page_number`) and keep `page_size` modest (e.g. **25-50**) to avoid token-heavy responses.
312
+ - Keep `generate_preview=false` unless you explicitly need thumbnails (previews may include large base64 payloads).
313
+ """
314
+
315
+
316
+ def _normalize_instance_token(instance_token: str | None) -> tuple[str | None, str | None]:
317
+ if not instance_token:
318
+ return None, None
319
+ if "@" in instance_token:
320
+ name_part, _, hash_part = instance_token.partition("@")
321
+ return (name_part or None), (hash_part or None)
322
+ return None, instance_token
323
+
324
+
325
+ def create_mcp_server(project_scoped_tools: bool) -> FastMCP:
326
+ mcp = FastMCP(
327
+ name="mcp-for-unity-server",
328
+ lifespan=server_lifespan,
329
+ instructions=_build_instructions(project_scoped_tools),
330
+ )
331
+
332
+ global custom_tool_service
333
+ custom_tool_service = CustomToolService(
334
+ mcp, project_scoped_tools=project_scoped_tools)
335
+
336
+ @mcp.custom_route("/health", methods=["GET"])
337
+ async def health_http(_: Request) -> JSONResponse:
338
+ return JSONResponse({
339
+ "status": "healthy",
340
+ "timestamp": time.time(),
341
+ "version": _server_version or "unknown",
342
+ "message": "MCP for Unity server is running"
343
+ })
344
+
345
+ @mcp.custom_route("/api/auth/login-url", methods=["GET"])
346
+ async def auth_login_url(_: Request) -> JSONResponse:
347
+ """Return the login URL for users to obtain/manage API keys."""
348
+ if not config.api_key_login_url:
349
+ return JSONResponse(
350
+ {
351
+ "success": False,
352
+ "error": "API key management not configured. Contact your server administrator.",
353
+ },
354
+ status_code=404,
355
+ )
356
+ return JSONResponse({
357
+ "success": True,
358
+ "login_url": config.api_key_login_url,
359
+ })
360
+
361
+ # Only expose CLI routes if running locally (not in remote hosted mode)
362
+ if not config.http_remote_hosted:
363
+ @mcp.custom_route("/api/command", methods=["POST"])
364
+ async def cli_command_route(request: Request) -> JSONResponse:
365
+ """REST endpoint for CLI commands to Unity."""
366
+ try:
367
+ body = await request.json()
368
+
369
+ command_type = body.get("type")
370
+ params = body.get("params", {})
371
+ unity_instance = body.get("unity_instance")
372
+
373
+ if not command_type:
374
+ return JSONResponse({"success": False, "error": "Missing 'type' field"}, status_code=400)
375
+
376
+ # Get available sessions
377
+ sessions = await PluginHub.get_sessions()
378
+ if not sessions.sessions:
379
+ return JSONResponse({
380
+ "success": False,
381
+ "error": "No Unity instances connected. Make sure Unity is running with MCP plugin."
382
+ }, status_code=503)
383
+
384
+ # Find target session
385
+ session_id = None
386
+ session_details = None
387
+ instance_name, instance_hash = _normalize_instance_token(
388
+ unity_instance)
389
+ if unity_instance:
390
+ # Try to match by hash or project name
391
+ for sid, details in sessions.sessions.items():
392
+ if details.hash == instance_hash or details.project in (instance_name, unity_instance):
393
+ session_id = sid
394
+ session_details = details
395
+ break
396
+
397
+ # If a specific unity_instance was requested but not found, return an error
398
+ if not session_id:
399
+ return JSONResponse(
400
+ {
401
+ "success": False,
402
+ "error": f"Unity instance '{unity_instance}' not found",
403
+ },
404
+ status_code=404,
405
+ )
406
+ else:
407
+ # No specific unity_instance requested: use first available session
408
+ session_id = next(iter(sessions.sessions.keys()))
409
+ session_details = sessions.sessions.get(session_id)
410
+
411
+ if command_type == "execute_custom_tool":
412
+ tool_name = None
413
+ tool_params = {}
414
+ if isinstance(params, dict):
415
+ tool_name = params.get(
416
+ "tool_name") or params.get("name")
417
+ tool_params = params.get(
418
+ "parameters") or params.get("params") or {}
419
+
420
+ if not tool_name:
421
+ return JSONResponse(
422
+ {"success": False,
423
+ "error": "Missing 'tool_name' for execute_custom_tool"},
424
+ status_code=400,
425
+ )
426
+ if tool_params is None:
427
+ tool_params = {}
428
+ if not isinstance(tool_params, dict):
429
+ return JSONResponse(
430
+ {"success": False,
431
+ "error": "Tool parameters must be an object/dict"},
432
+ status_code=400,
433
+ )
434
+
435
+ # Prefer a concrete hash for project-scoped tools.
436
+ unity_instance_hint = unity_instance
437
+ if session_details and session_details.hash:
438
+ unity_instance_hint = session_details.hash
439
+
440
+ project_id = resolve_project_id_for_unity_instance(
441
+ unity_instance_hint)
442
+ if not project_id:
443
+ return JSONResponse(
444
+ {"success": False,
445
+ "error": "Could not resolve project id for custom tool"},
446
+ status_code=400,
447
+ )
448
+
449
+ service = CustomToolService.get_instance()
450
+ result = await service.execute_tool(
451
+ project_id, tool_name, unity_instance_hint, tool_params
452
+ )
453
+ return JSONResponse(result.model_dump())
454
+
455
+ # Send command to Unity
456
+ result = await PluginHub.send_command(session_id, command_type, params)
457
+ return JSONResponse(result)
458
+
459
+ except Exception as e:
460
+ logger.exception("CLI command error: %s", e)
461
+ return JSONResponse({"success": False, "error": str(e)}, status_code=500)
462
+
463
+ @mcp.custom_route("/api/instances", methods=["GET"])
464
+ async def cli_instances_route(_: Request) -> JSONResponse:
465
+ """REST endpoint to list connected Unity instances."""
466
+ try:
467
+ sessions = await PluginHub.get_sessions()
468
+ instances = []
469
+ for session_id, details in sessions.sessions.items():
470
+ instances.append({
471
+ "session_id": session_id,
472
+ "project": details.project,
473
+ "hash": details.hash,
474
+ "unity_version": details.unity_version,
475
+ "connected_at": details.connected_at,
476
+ })
477
+ return JSONResponse({"success": True, "instances": instances})
478
+ except Exception as e:
479
+ return JSONResponse({"success": False, "error": str(e)}, status_code=500)
480
+
481
+ @mcp.custom_route("/api/custom-tools", methods=["GET"])
482
+ async def cli_custom_tools_route(request: Request) -> JSONResponse:
483
+ """REST endpoint to list custom tools for the active Unity project."""
484
+ try:
485
+ unity_instance = request.query_params.get("instance")
486
+ instance_name, instance_hash = _normalize_instance_token(
487
+ unity_instance)
488
+
489
+ sessions = await PluginHub.get_sessions()
490
+ if not sessions.sessions:
491
+ return JSONResponse({
492
+ "success": False,
493
+ "error": "No Unity instances connected. Make sure Unity is running with MCP plugin."
494
+ }, status_code=503)
495
+
496
+ session_details = None
497
+ if unity_instance:
498
+ # Try to match by hash or project name
499
+ for _, details in sessions.sessions.items():
500
+ if details.hash == instance_hash or details.project in (instance_name, unity_instance):
501
+ session_details = details
502
+ break
503
+ if not session_details:
504
+ return JSONResponse(
505
+ {
506
+ "success": False,
507
+ "error": f"Unity instance '{unity_instance}' not found",
508
+ },
509
+ status_code=404,
510
+ )
511
+ else:
512
+ # No specific unity_instance requested: use first available session
513
+ session_details = next(iter(sessions.sessions.values()))
514
+
515
+ unity_instance_hint = unity_instance
516
+ if session_details and session_details.hash:
517
+ unity_instance_hint = session_details.hash
518
+
519
+ project_id = resolve_project_id_for_unity_instance(
520
+ unity_instance_hint)
521
+ if not project_id:
522
+ return JSONResponse(
523
+ {"success": False,
524
+ "error": "Could not resolve project id for custom tools"},
525
+ status_code=400,
526
+ )
527
+
528
+ service = CustomToolService.get_instance()
529
+ tools = await service.list_registered_tools(project_id)
530
+ tools_payload = [
531
+ tool.model_dump() if hasattr(tool, "model_dump") else tool for tool in tools
532
+ ]
533
+
534
+ return JSONResponse({
535
+ "success": True,
536
+ "project_id": project_id,
537
+ "tool_count": len(tools_payload),
538
+ "tools": tools_payload,
539
+ })
540
+ except Exception as e:
541
+ logger.exception("CLI custom tools error: %s", e)
542
+ return JSONResponse({"success": False, "error": str(e)}, status_code=500)
543
+
544
+ # Initialize and register middleware for session-based Unity instance routing
545
+ # Using the singleton getter ensures we use the same instance everywhere
546
+ unity_middleware = get_unity_instance_middleware()
547
+ mcp.add_middleware(unity_middleware)
548
+ logger.info("Registered Unity instance middleware for session-based routing")
549
+
550
+ # Initialize API key authentication if in remote-hosted mode
551
+ if config.http_remote_hosted and config.api_key_validation_url:
552
+ ApiKeyService(
553
+ validation_url=config.api_key_validation_url,
554
+ cache_ttl=config.api_key_cache_ttl,
555
+ service_token_header=config.api_key_service_token_header,
556
+ service_token=config.api_key_service_token,
557
+ )
558
+ logger.info(
559
+ "Initialized API key authentication service (validation URL: %s, TTL: %.0fs)",
560
+ config.api_key_validation_url,
561
+ config.api_key_cache_ttl,
562
+ )
563
+
564
+ # Mount plugin websocket hub at /hub/plugin when HTTP transport is active
565
+ existing_routes = [
566
+ route for route in mcp._get_additional_http_routes()
567
+ if isinstance(route, WebSocketRoute) and route.path == "/hub/plugin"
568
+ ]
569
+ if not existing_routes:
570
+ mcp._additional_http_routes.append(
571
+ WebSocketRoute("/hub/plugin", PluginHub))
572
+
573
+ # Register all tools
574
+ register_all_tools(mcp, project_scoped_tools=project_scoped_tools)
575
+
576
+ # Register all resources
577
+ register_all_resources(mcp, project_scoped_tools=project_scoped_tools)
578
+
579
+ return mcp
580
+
581
+
582
+ def main():
583
+ """Entry point for uvx and console scripts."""
584
+ parser = argparse.ArgumentParser(
585
+ description="MCP for Unity Server",
586
+ formatter_class=argparse.RawDescriptionHelpFormatter,
587
+ epilog="""
588
+ Environment Variables:
589
+ UNITY_MCP_DEFAULT_INSTANCE Default Unity instance to target (project name, hash, or 'Name@hash')
590
+ UNITY_MCP_SKIP_STARTUP_CONNECT Skip initial Unity connection attempt (set to 1/true/yes/on)
591
+ UNITY_MCP_TELEMETRY_ENABLED Enable telemetry (set to 1/true/yes/on)
592
+ UNITY_MCP_TRANSPORT Transport protocol: stdio or http (default: stdio)
593
+ UNITY_MCP_HTTP_URL HTTP server URL (default: http://localhost:8080)
594
+ UNITY_MCP_HTTP_HOST HTTP server host (overrides URL host)
595
+ UNITY_MCP_HTTP_PORT HTTP server port (overrides URL port)
596
+
597
+ Examples:
598
+ # Use specific Unity project as default
599
+ python -m src.server --default-instance "MyProject"
600
+
601
+ # Start with HTTP transport
602
+ python -m src.server --transport http --http-url http://localhost:8080
603
+
604
+ # Start with stdio transport (default)
605
+ python -m src.server --transport stdio
606
+
607
+ # Use environment variable for transport
608
+ UNITY_MCP_TRANSPORT=http UNITY_MCP_HTTP_URL=http://localhost:9000 python -m src.server
609
+ """
610
+ )
611
+ parser.add_argument(
612
+ "--default-instance",
613
+ type=str,
614
+ metavar="INSTANCE",
615
+ help="Default Unity instance to target (project name, hash, or 'Name@hash'). "
616
+ "Overrides UNITY_MCP_DEFAULT_INSTANCE environment variable."
617
+ )
618
+ parser.add_argument(
619
+ "--transport",
620
+ type=str,
621
+ choices=["stdio", "http"],
622
+ default="stdio",
623
+ help="Transport protocol to use: stdio or http (default: stdio). "
624
+ "Overrides UNITY_MCP_TRANSPORT environment variable."
625
+ )
626
+ parser.add_argument(
627
+ "--http-url",
628
+ type=str,
629
+ default="http://localhost:8080",
630
+ metavar="URL",
631
+ help="HTTP server URL (default: http://localhost:8080). "
632
+ "Can also set via UNITY_MCP_HTTP_URL environment variable."
633
+ )
634
+ parser.add_argument(
635
+ "--http-host",
636
+ type=str,
637
+ default=None,
638
+ metavar="HOST",
639
+ help="HTTP server host (overrides URL host). "
640
+ "Overrides UNITY_MCP_HTTP_HOST environment variable."
641
+ )
642
+ parser.add_argument(
643
+ "--http-port",
644
+ type=int,
645
+ default=None,
646
+ metavar="PORT",
647
+ help="HTTP server port (overrides URL port). "
648
+ "Overrides UNITY_MCP_HTTP_PORT environment variable."
649
+ )
650
+ parser.add_argument(
651
+ "--http-remote-hosted",
652
+ action="store_true",
653
+ help="Treat HTTP transport as remotely hosted (forces explicit Unity instance selection). "
654
+ "Can also set via UNITY_MCP_HTTP_REMOTE_HOSTED=true."
655
+ )
656
+ parser.add_argument(
657
+ "--api-key-validation-url",
658
+ type=str,
659
+ default=None,
660
+ metavar="URL",
661
+ help="External URL to validate API keys (POST with {'api_key': '...'}). "
662
+ "Required when --http-remote-hosted is set. "
663
+ "Can also set via UNITY_MCP_API_KEY_VALIDATION_URL."
664
+ )
665
+ parser.add_argument(
666
+ "--api-key-login-url",
667
+ type=str,
668
+ default=None,
669
+ metavar="URL",
670
+ help="URL where users can obtain/manage API keys. "
671
+ "Returned by /api/auth/login-url endpoint. "
672
+ "Can also set via UNITY_MCP_API_KEY_LOGIN_URL."
673
+ )
674
+ parser.add_argument(
675
+ "--api-key-cache-ttl",
676
+ type=float,
677
+ default=300.0,
678
+ metavar="SECONDS",
679
+ help="Cache TTL for validated API keys in seconds (default: 300). "
680
+ "Can also set via UNITY_MCP_API_KEY_CACHE_TTL."
681
+ )
682
+ parser.add_argument(
683
+ "--api-key-service-token-header",
684
+ type=str,
685
+ default=None,
686
+ metavar="HEADER",
687
+ help="Header name for service token sent to validation endpoint (e.g. X-Service-Token). "
688
+ "Can also set via UNITY_MCP_API_KEY_SERVICE_TOKEN_HEADER."
689
+ )
690
+ parser.add_argument(
691
+ "--api-key-service-token",
692
+ type=str,
693
+ default=None,
694
+ metavar="TOKEN",
695
+ help="Service token value sent to validation endpoint for server authentication. "
696
+ "WARNING: Prefer UNITY_MCP_API_KEY_SERVICE_TOKEN env var in production to avoid process listing exposure."
697
+ )
698
+ parser.add_argument(
699
+ "--unity-instance-token",
700
+ type=str,
701
+ default=None,
702
+ metavar="TOKEN",
703
+ help="Optional per-launch token set by Unity for deterministic lifecycle management. "
704
+ "Used by Unity to validate it is stopping the correct process."
705
+ )
706
+ parser.add_argument(
707
+ "--pidfile",
708
+ type=str,
709
+ default=None,
710
+ metavar="PATH",
711
+ help="Optional path where the server will write its PID on startup. "
712
+ "Used by Unity to stop the exact process it launched when running in a terminal."
713
+ )
714
+ parser.add_argument(
715
+ "--project-scoped-tools",
716
+ action="store_true",
717
+ help="Keep custom tools scoped to the active Unity project and enable the custom tools resource. "
718
+ "Can also set via UNITY_MCP_PROJECT_SCOPED_TOOLS=true."
719
+ )
720
+
721
+ args = parser.parse_args()
722
+
723
+ # Set environment variables from command line args
724
+ if args.default_instance:
725
+ os.environ["UNITY_MCP_DEFAULT_INSTANCE"] = args.default_instance
726
+ logger.info(
727
+ f"Using default Unity instance from command-line: {args.default_instance}")
728
+
729
+ # Set transport mode
730
+ config.transport_mode = args.transport or os.environ.get(
731
+ "UNITY_MCP_TRANSPORT", "stdio")
732
+ logger.info(f"Transport mode: {config.transport_mode}")
733
+
734
+ config.http_remote_hosted = (
735
+ bool(args.http_remote_hosted)
736
+ or os.environ.get("UNITY_MCP_HTTP_REMOTE_HOSTED", "").lower() in ("true", "1", "yes", "on")
737
+ )
738
+
739
+ # API key authentication configuration
740
+ config.api_key_validation_url = (
741
+ args.api_key_validation_url
742
+ or os.environ.get("UNITY_MCP_API_KEY_VALIDATION_URL")
743
+ )
744
+ config.api_key_login_url = (
745
+ args.api_key_login_url
746
+ or os.environ.get("UNITY_MCP_API_KEY_LOGIN_URL")
747
+ )
748
+ try:
749
+ cache_ttl_env = os.environ.get("UNITY_MCP_API_KEY_CACHE_TTL")
750
+ config.api_key_cache_ttl = (
751
+ float(cache_ttl_env) if cache_ttl_env else args.api_key_cache_ttl
752
+ )
753
+ except ValueError:
754
+ logger.warning(
755
+ "Invalid UNITY_MCP_API_KEY_CACHE_TTL value, using default 300.0"
756
+ )
757
+ config.api_key_cache_ttl = 300.0
758
+
759
+ # Service token for authenticating to validation endpoint
760
+ config.api_key_service_token_header = (
761
+ args.api_key_service_token_header
762
+ or os.environ.get("UNITY_MCP_API_KEY_SERVICE_TOKEN_HEADER")
763
+ )
764
+ config.api_key_service_token = (
765
+ args.api_key_service_token
766
+ or os.environ.get("UNITY_MCP_API_KEY_SERVICE_TOKEN")
767
+ )
768
+
769
+ # Validate: remote-hosted HTTP mode requires API key validation URL
770
+ if config.http_remote_hosted and config.transport_mode == "http" and not config.api_key_validation_url:
771
+ logger.error(
772
+ "--http-remote-hosted requires --api-key-validation-url or "
773
+ "UNITY_MCP_API_KEY_VALIDATION_URL environment variable"
774
+ )
775
+ raise SystemExit(1)
776
+
777
+ http_url = os.environ.get("UNITY_MCP_HTTP_URL", args.http_url)
778
+ parsed_url = urlparse(http_url)
779
+
780
+ # Allow individual host/port to override URL components
781
+ http_host = args.http_host or os.environ.get(
782
+ "UNITY_MCP_HTTP_HOST") or parsed_url.hostname or "localhost"
783
+
784
+ # Safely parse optional environment port (may be None or non-numeric)
785
+ _env_port_str = os.environ.get("UNITY_MCP_HTTP_PORT")
786
+ try:
787
+ _env_port = int(_env_port_str) if _env_port_str is not None else None
788
+ except ValueError:
789
+ logger.warning(
790
+ "Invalid UNITY_MCP_HTTP_PORT value '%s', ignoring", _env_port_str)
791
+ _env_port = None
792
+
793
+ http_port = args.http_port or _env_port or parsed_url.port or 8080
794
+
795
+ os.environ["UNITY_MCP_HTTP_HOST"] = http_host
796
+ os.environ["UNITY_MCP_HTTP_PORT"] = str(http_port)
797
+
798
+ # Optional lifecycle handshake for Unity-managed terminal launches
799
+ if args.unity_instance_token:
800
+ os.environ["UNITY_MCP_INSTANCE_TOKEN"] = args.unity_instance_token
801
+ if args.pidfile:
802
+ try:
803
+ pid_dir = os.path.dirname(args.pidfile)
804
+ if pid_dir:
805
+ os.makedirs(pid_dir, exist_ok=True)
806
+ with open(args.pidfile, "w", encoding="ascii") as f:
807
+ f.write(str(os.getpid()))
808
+ except Exception as exc:
809
+ logger.warning(
810
+ "Failed to write pidfile '%s': %s", args.pidfile, exc)
811
+
812
+ if args.http_url != "http://localhost:8080":
813
+ logger.info(f"HTTP URL set to: {http_url}")
814
+ if args.http_host:
815
+ logger.info(f"HTTP host override: {http_host}")
816
+ if args.http_port:
817
+ logger.info(f"HTTP port override: {http_port}")
818
+
819
+ project_scoped_tools = (
820
+ bool(args.project_scoped_tools)
821
+ or os.environ.get("UNITY_MCP_PROJECT_SCOPED_TOOLS", "").lower() in ("true", "1", "yes", "on")
822
+ )
823
+ mcp = create_mcp_server(project_scoped_tools)
824
+
825
+ # Determine transport mode
826
+ if config.transport_mode == 'http':
827
+ # Use HTTP transport for FastMCP
828
+ transport = 'http'
829
+ # Use the parsed host and port from URL/args
830
+ http_url = os.environ.get("UNITY_MCP_HTTP_URL", args.http_url)
831
+ parsed_url = urlparse(http_url)
832
+ host = args.http_host or os.environ.get(
833
+ "UNITY_MCP_HTTP_HOST") or parsed_url.hostname or "localhost"
834
+ port = args.http_port or _env_port or parsed_url.port or 8080
835
+ logger.info(f"Starting FastMCP with HTTP transport on {host}:{port}")
836
+ mcp.run(transport=transport, host=host, port=port)
837
+ else:
838
+ # Use stdio transport for traditional MCP
839
+ logger.info("Starting FastMCP with stdio transport")
840
+ mcp.run(transport='stdio')
841
+
842
+
843
+ # Run the server
844
+ if __name__ == "__main__":
845
+ main()