mcpforunityserver 9.3.0b20260129104751__py3-none-any.whl → 9.3.0b20260131003150__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.
- cli/utils/connection.py +28 -32
- core/config.py +15 -0
- core/constants.py +4 -0
- main.py +306 -174
- {mcpforunityserver-9.3.0b20260129104751.dist-info → mcpforunityserver-9.3.0b20260131003150.dist-info}/METADATA +117 -5
- {mcpforunityserver-9.3.0b20260129104751.dist-info → mcpforunityserver-9.3.0b20260131003150.dist-info}/RECORD +30 -28
- models/__init__.py +2 -2
- models/unity_response.py +24 -1
- services/api_key_service.py +235 -0
- services/resources/active_tool.py +2 -1
- services/resources/editor_state.py +7 -7
- services/resources/layers.py +2 -1
- services/resources/menu_items.py +2 -1
- services/resources/prefab_stage.py +2 -1
- services/resources/project_info.py +2 -1
- services/resources/selection.py +2 -1
- services/resources/tags.py +2 -1
- services/resources/tests.py +3 -2
- services/resources/unity_instances.py +6 -3
- services/resources/windows.py +2 -1
- services/tools/manage_prefabs.py +35 -0
- services/tools/set_active_instance.py +6 -3
- transport/plugin_hub.py +124 -24
- transport/plugin_registry.py +75 -19
- transport/unity_instance_middleware.py +38 -9
- transport/unity_transport.py +41 -10
- {mcpforunityserver-9.3.0b20260129104751.dist-info → mcpforunityserver-9.3.0b20260131003150.dist-info}/WHEEL +0 -0
- {mcpforunityserver-9.3.0b20260129104751.dist-info → mcpforunityserver-9.3.0b20260131003150.dist-info}/entry_points.txt +0 -0
- {mcpforunityserver-9.3.0b20260129104751.dist-info → mcpforunityserver-9.3.0b20260131003150.dist-info}/licenses/LICENSE +0 -0
- {mcpforunityserver-9.3.0b20260129104751.dist-info → mcpforunityserver-9.3.0b20260131003150.dist-info}/top_level.txt +0 -0
cli/utils/connection.py
CHANGED
|
@@ -182,38 +182,34 @@ async def list_unity_instances(config: Optional[CLIConfig] = None) -> Dict[str,
|
|
|
182
182
|
"""
|
|
183
183
|
cfg = config or get_config()
|
|
184
184
|
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
continue
|
|
214
|
-
|
|
215
|
-
raise UnityConnectionError(
|
|
216
|
-
"Failed to list Unity instances: No working endpoint found")
|
|
185
|
+
url = f"http://{cfg.host}:{cfg.port}/api/instances"
|
|
186
|
+
|
|
187
|
+
try:
|
|
188
|
+
async with httpx.AsyncClient() as client:
|
|
189
|
+
response = await client.get(url, timeout=10)
|
|
190
|
+
response.raise_for_status()
|
|
191
|
+
data = response.json()
|
|
192
|
+
if "instances" in data:
|
|
193
|
+
return data
|
|
194
|
+
except httpx.ConnectError as e:
|
|
195
|
+
raise UnityConnectionError(
|
|
196
|
+
f"Cannot connect to Unity MCP server at {cfg.host}:{cfg.port}. "
|
|
197
|
+
f"Make sure the server is running and Unity is connected.\n"
|
|
198
|
+
f"Error: {e}"
|
|
199
|
+
)
|
|
200
|
+
except httpx.TimeoutException:
|
|
201
|
+
raise UnityConnectionError(
|
|
202
|
+
"Connection to Unity timed out while listing instances. "
|
|
203
|
+
"Unity may be busy or unresponsive."
|
|
204
|
+
)
|
|
205
|
+
except httpx.HTTPStatusError as e:
|
|
206
|
+
raise UnityConnectionError(
|
|
207
|
+
f"HTTP error from server: {e.response.status_code} - {e.response.text}"
|
|
208
|
+
)
|
|
209
|
+
except Exception as e:
|
|
210
|
+
raise UnityConnectionError(f"Unexpected error: {e}")
|
|
211
|
+
|
|
212
|
+
raise UnityConnectionError("Failed to list Unity instances")
|
|
217
213
|
|
|
218
214
|
|
|
219
215
|
def run_list_instances(config: Optional[CLIConfig] = None) -> Dict[str, Any]:
|
core/config.py
CHANGED
|
@@ -15,6 +15,21 @@ class ServerConfig:
|
|
|
15
15
|
unity_port: int = 6400
|
|
16
16
|
mcp_port: int = 6500
|
|
17
17
|
|
|
18
|
+
# Transport settings
|
|
19
|
+
transport_mode: str = "stdio"
|
|
20
|
+
|
|
21
|
+
# HTTP transport behaviour
|
|
22
|
+
http_remote_hosted: bool = False
|
|
23
|
+
|
|
24
|
+
# API key authentication (required when http_remote_hosted=True)
|
|
25
|
+
api_key_validation_url: str | None = None # POST endpoint to validate keys
|
|
26
|
+
api_key_login_url: str | None = None # URL for users to get/manage keys
|
|
27
|
+
# Cache TTL in seconds (5 min default)
|
|
28
|
+
api_key_cache_ttl: float = 300.0
|
|
29
|
+
# Optional service token for authenticating to the validation endpoint
|
|
30
|
+
api_key_service_token_header: str | None = None # e.g. "X-Service-Token"
|
|
31
|
+
api_key_service_token: str | None = None # The token value
|
|
32
|
+
|
|
18
33
|
# Connection settings
|
|
19
34
|
connection_timeout: float = 30.0
|
|
20
35
|
buffer_size: int = 16 * 1024 * 1024 # 16MB buffer
|
core/constants.py
ADDED
main.py
CHANGED
|
@@ -3,6 +3,7 @@ from transport.unity_instance_middleware import (
|
|
|
3
3
|
UnityInstanceMiddleware,
|
|
4
4
|
get_unity_instance_middleware
|
|
5
5
|
)
|
|
6
|
+
from services.api_key_service import ApiKeyService
|
|
6
7
|
from transport.legacy.unity_connection import get_unity_connection_pool, UnityConnectionPool
|
|
7
8
|
from services.tools import register_all_tools
|
|
8
9
|
from core.telemetry import record_milestone, record_telemetry, MilestoneType, RecordType, get_package_version
|
|
@@ -312,6 +313,15 @@ Payload sizing & paging (important):
|
|
|
312
313
|
"""
|
|
313
314
|
|
|
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
|
+
|
|
315
325
|
def create_mcp_server(project_scoped_tools: bool) -> FastMCP:
|
|
316
326
|
mcp = FastMCP(
|
|
317
327
|
name="mcp-for-unity-server",
|
|
@@ -332,82 +342,176 @@ def create_mcp_server(project_scoped_tools: bool) -> FastMCP:
|
|
|
332
342
|
"message": "MCP for Unity server is running"
|
|
333
343
|
})
|
|
334
344
|
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
if
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
return None, instance_token
|
|
342
|
-
|
|
343
|
-
@mcp.custom_route("/api/command", methods=["POST"])
|
|
344
|
-
async def cli_command_route(request: Request) -> JSONResponse:
|
|
345
|
-
"""REST endpoint for CLI commands to Unity."""
|
|
346
|
-
try:
|
|
347
|
-
body = await request.json()
|
|
348
|
-
|
|
349
|
-
command_type = body.get("type")
|
|
350
|
-
params = body.get("params", {})
|
|
351
|
-
unity_instance = body.get("unity_instance")
|
|
352
|
-
|
|
353
|
-
if not command_type:
|
|
354
|
-
return JSONResponse({"success": False, "error": "Missing 'type' field"}, status_code=400)
|
|
355
|
-
|
|
356
|
-
# Get available sessions
|
|
357
|
-
sessions = await PluginHub.get_sessions()
|
|
358
|
-
if not sessions.sessions:
|
|
359
|
-
return JSONResponse({
|
|
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
|
+
{
|
|
360
351
|
"success": False,
|
|
361
|
-
"error": "
|
|
362
|
-
},
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
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()))
|
|
409
514
|
|
|
410
|
-
# Prefer a concrete hash for project-scoped tools.
|
|
411
515
|
unity_instance_hint = unity_instance
|
|
412
516
|
if session_details and session_details.hash:
|
|
413
517
|
unity_instance_hint = session_details.hash
|
|
@@ -416,107 +520,26 @@ def create_mcp_server(project_scoped_tools: bool) -> FastMCP:
|
|
|
416
520
|
unity_instance_hint)
|
|
417
521
|
if not project_id:
|
|
418
522
|
return JSONResponse(
|
|
419
|
-
{"success": False,
|
|
523
|
+
{"success": False,
|
|
524
|
+
"error": "Could not resolve project id for custom tools"},
|
|
420
525
|
status_code=400,
|
|
421
526
|
)
|
|
422
527
|
|
|
423
528
|
service = CustomToolService.get_instance()
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
# Send command to Unity
|
|
430
|
-
result = await PluginHub.send_command(session_id, command_type, params)
|
|
431
|
-
return JSONResponse(result)
|
|
432
|
-
|
|
433
|
-
except Exception as e:
|
|
434
|
-
logger.error(f"CLI command error: {e}")
|
|
435
|
-
return JSONResponse({"success": False, "error": str(e)}, status_code=500)
|
|
436
|
-
|
|
437
|
-
@mcp.custom_route("/api/custom-tools", methods=["GET"])
|
|
438
|
-
async def cli_custom_tools_route(request: Request) -> JSONResponse:
|
|
439
|
-
"""REST endpoint to list custom tools for the active Unity project."""
|
|
440
|
-
try:
|
|
441
|
-
unity_instance = request.query_params.get("instance")
|
|
442
|
-
instance_name, instance_hash = _normalize_instance_token(unity_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
|
+
]
|
|
443
533
|
|
|
444
|
-
sessions = await PluginHub.get_sessions()
|
|
445
|
-
if not sessions.sessions:
|
|
446
534
|
return JSONResponse({
|
|
447
|
-
"success":
|
|
448
|
-
"
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
session_details = None
|
|
452
|
-
if unity_instance:
|
|
453
|
-
# Try to match by hash or project name
|
|
454
|
-
for _, details in sessions.sessions.items():
|
|
455
|
-
if details.hash == instance_hash or details.project in (instance_name, unity_instance):
|
|
456
|
-
session_details = details
|
|
457
|
-
break
|
|
458
|
-
if not session_details:
|
|
459
|
-
return JSONResponse(
|
|
460
|
-
{
|
|
461
|
-
"success": False,
|
|
462
|
-
"error": f"Unity instance '{unity_instance}' not found",
|
|
463
|
-
},
|
|
464
|
-
status_code=404,
|
|
465
|
-
)
|
|
466
|
-
else:
|
|
467
|
-
# No specific unity_instance requested: use first available session
|
|
468
|
-
session_details = next(iter(sessions.sessions.values()))
|
|
469
|
-
|
|
470
|
-
unity_instance_hint = unity_instance
|
|
471
|
-
if session_details and session_details.hash:
|
|
472
|
-
unity_instance_hint = session_details.hash
|
|
473
|
-
|
|
474
|
-
project_id = resolve_project_id_for_unity_instance(
|
|
475
|
-
unity_instance_hint)
|
|
476
|
-
if not project_id:
|
|
477
|
-
return JSONResponse(
|
|
478
|
-
{"success": False, "error": "Could not resolve project id for custom tools"},
|
|
479
|
-
status_code=400,
|
|
480
|
-
)
|
|
481
|
-
|
|
482
|
-
service = CustomToolService.get_instance()
|
|
483
|
-
tools = await service.list_registered_tools(project_id)
|
|
484
|
-
tools_payload = [
|
|
485
|
-
tool.model_dump() if hasattr(tool, "model_dump") else tool for tool in tools
|
|
486
|
-
]
|
|
487
|
-
|
|
488
|
-
return JSONResponse({
|
|
489
|
-
"success": True,
|
|
490
|
-
"project_id": project_id,
|
|
491
|
-
"tool_count": len(tools_payload),
|
|
492
|
-
"tools": tools_payload,
|
|
493
|
-
})
|
|
494
|
-
except Exception as e:
|
|
495
|
-
logger.error(f"CLI custom tools error: {e}")
|
|
496
|
-
return JSONResponse({"success": False, "error": str(e)}, status_code=500)
|
|
497
|
-
|
|
498
|
-
@mcp.custom_route("/api/instances", methods=["GET"])
|
|
499
|
-
async def cli_instances_route(_: Request) -> JSONResponse:
|
|
500
|
-
"""REST endpoint to list connected Unity instances."""
|
|
501
|
-
try:
|
|
502
|
-
sessions = await PluginHub.get_sessions()
|
|
503
|
-
instances = []
|
|
504
|
-
for session_id, details in sessions.sessions.items():
|
|
505
|
-
instances.append({
|
|
506
|
-
"session_id": session_id,
|
|
507
|
-
"project": details.project,
|
|
508
|
-
"hash": details.hash,
|
|
509
|
-
"unity_version": details.unity_version,
|
|
510
|
-
"connected_at": details.connected_at,
|
|
535
|
+
"success": True,
|
|
536
|
+
"project_id": project_id,
|
|
537
|
+
"tool_count": len(tools_payload),
|
|
538
|
+
"tools": tools_payload,
|
|
511
539
|
})
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
@mcp.custom_route("/plugin/sessions", methods=["GET"])
|
|
517
|
-
async def plugin_sessions_route(_: Request) -> JSONResponse:
|
|
518
|
-
data = await PluginHub.get_sessions()
|
|
519
|
-
return JSONResponse(data.model_dump())
|
|
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)
|
|
520
543
|
|
|
521
544
|
# Initialize and register middleware for session-based Unity instance routing
|
|
522
545
|
# Using the singleton getter ensures we use the same instance everywhere
|
|
@@ -524,6 +547,20 @@ def create_mcp_server(project_scoped_tools: bool) -> FastMCP:
|
|
|
524
547
|
mcp.add_middleware(unity_middleware)
|
|
525
548
|
logger.info("Registered Unity instance middleware for session-based routing")
|
|
526
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
|
+
|
|
527
564
|
# Mount plugin websocket hub at /hub/plugin when HTTP transport is active
|
|
528
565
|
existing_routes = [
|
|
529
566
|
route for route in mcp._get_additional_http_routes()
|
|
@@ -610,6 +647,54 @@ Examples:
|
|
|
610
647
|
help="HTTP server port (overrides URL port). "
|
|
611
648
|
"Overrides UNITY_MCP_HTTP_PORT environment variable."
|
|
612
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
|
+
)
|
|
613
698
|
parser.add_argument(
|
|
614
699
|
"--unity-instance-token",
|
|
615
700
|
type=str,
|
|
@@ -629,7 +714,8 @@ Examples:
|
|
|
629
714
|
parser.add_argument(
|
|
630
715
|
"--project-scoped-tools",
|
|
631
716
|
action="store_true",
|
|
632
|
-
help="Keep custom tools scoped to the active Unity project and enable the custom tools resource."
|
|
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."
|
|
633
719
|
)
|
|
634
720
|
|
|
635
721
|
args = parser.parse_args()
|
|
@@ -641,10 +727,52 @@ Examples:
|
|
|
641
727
|
f"Using default Unity instance from command-line: {args.default_instance}")
|
|
642
728
|
|
|
643
729
|
# Set transport mode
|
|
644
|
-
transport_mode = args.transport or os.environ.get(
|
|
730
|
+
config.transport_mode = args.transport or os.environ.get(
|
|
645
731
|
"UNITY_MCP_TRANSPORT", "stdio")
|
|
646
|
-
|
|
647
|
-
|
|
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)
|
|
648
776
|
|
|
649
777
|
http_url = os.environ.get("UNITY_MCP_HTTP_URL", args.http_url)
|
|
650
778
|
parsed_url = urlparse(http_url)
|
|
@@ -688,10 +816,14 @@ Examples:
|
|
|
688
816
|
if args.http_port:
|
|
689
817
|
logger.info(f"HTTP port override: {http_port}")
|
|
690
818
|
|
|
691
|
-
|
|
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)
|
|
692
824
|
|
|
693
825
|
# Determine transport mode
|
|
694
|
-
if transport_mode == 'http':
|
|
826
|
+
if config.transport_mode == 'http':
|
|
695
827
|
# Use HTTP transport for FastMCP
|
|
696
828
|
transport = 'http'
|
|
697
829
|
# Use the parsed host and port from URL/args
|