-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathexecute_custom_tool.py
More file actions
53 lines (47 loc) · 1.71 KB
/
execute_custom_tool.py
File metadata and controls
53 lines (47 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from fastmcp import Context
from mcp.types import ToolAnnotations
from models.models import MCPResponse
from services.custom_tool_service import (
CustomToolService,
get_user_id_from_context,
resolve_project_id_for_unity_instance,
)
from services.registry import mcp_for_unity_tool
from services.tools import get_unity_instance_from_context
@mcp_for_unity_tool(
name="execute_custom_tool",
unity_target=None,
group=None,
description="Execute a project-scoped custom tool registered by Unity.",
annotations=ToolAnnotations(
title="Execute Custom Tool",
destructiveHint=True,
),
)
async def execute_custom_tool(ctx: Context, tool_name: str, parameters: dict | None = None) -> MCPResponse:
unity_instance = await get_unity_instance_from_context(ctx)
if not unity_instance:
return MCPResponse(
success=False,
message="No active Unity instance. Pass unity_instance explicitly or call set_active_instance with Name@hash from mcpforunity://instances.",
)
project_id = resolve_project_id_for_unity_instance(unity_instance)
if project_id is None:
return MCPResponse(
success=False,
message=f"Could not resolve project id for {unity_instance}. Ensure Unity is running and reachable.",
)
if not isinstance(parameters, dict):
return MCPResponse(
success=False,
message="parameters must be an object/dictionary",
)
service = CustomToolService.get_instance()
user_id = await get_user_id_from_context(ctx)
return await service.execute_tool(
project_id,
tool_name,
unity_instance,
parameters,
user_id=user_id,
)