Skip to content

Commit 399d279

Browse files
committed
refactor: add context support to resource handlers
- Updated all resource handlers to accept Context parameter for Unity instance routing - Replaced direct async_send_command_with_retry calls with async_send_with_unity_instance wrapper - Added imports for get_unity_instance_from_context and async_send_with_unity_instance helpers
1 parent d76e2b1 commit 399d279

File tree

8 files changed

+88
-16
lines changed

8 files changed

+88
-16
lines changed

MCPForUnity/UnityMcpServer~/src/resources/active_tool.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
from pydantic import BaseModel
2+
from fastmcp import Context
3+
24
from models import MCPResponse
35
from registry import mcp_for_unity_resource
6+
from tools import get_unity_instance_from_context, async_send_with_unity_instance
47
from unity_connection import async_send_command_with_retry
58

69

@@ -31,7 +34,13 @@ class ActiveToolResponse(MCPResponse):
3134
name="editor_active_tool",
3235
description="Currently active editor tool (Move, Rotate, Scale, etc.) and transform handle settings."
3336
)
34-
async def get_active_tool() -> ActiveToolResponse | MCPResponse:
37+
async def get_active_tool(ctx: Context) -> ActiveToolResponse | MCPResponse:
3538
"""Get active editor tool information."""
36-
response = await async_send_command_with_retry("get_active_tool", {})
39+
unity_instance = get_unity_instance_from_context(ctx)
40+
response = await async_send_with_unity_instance(
41+
async_send_command_with_retry,
42+
unity_instance,
43+
"get_active_tool",
44+
{}
45+
)
3746
return ActiveToolResponse(**response) if isinstance(response, dict) else response

MCPForUnity/UnityMcpServer~/src/resources/editor_state.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
from pydantic import BaseModel
2+
from fastmcp import Context
3+
24
from models import MCPResponse
35
from registry import mcp_for_unity_resource
6+
from tools import get_unity_instance_from_context, async_send_with_unity_instance
47
from unity_connection import async_send_command_with_retry
58

69

@@ -26,7 +29,13 @@ class EditorStateResponse(MCPResponse):
2629
name="editor_state",
2730
description="Current editor runtime state including play mode, compilation status, active scene, and selection summary. Refresh frequently for up-to-date information."
2831
)
29-
async def get_editor_state() -> EditorStateResponse | MCPResponse:
32+
async def get_editor_state(ctx: Context) -> EditorStateResponse | MCPResponse:
3033
"""Get current editor runtime state."""
31-
response = await async_send_command_with_retry("get_editor_state", {})
34+
unity_instance = get_unity_instance_from_context(ctx)
35+
response = await async_send_with_unity_instance(
36+
async_send_command_with_retry,
37+
unity_instance,
38+
"get_editor_state",
39+
{}
40+
)
3241
return EditorStateResponse(**response) if isinstance(response, dict) else response

MCPForUnity/UnityMcpServer~/src/resources/layers.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
from fastmcp import Context
2+
13
from models import MCPResponse
24
from registry import mcp_for_unity_resource
5+
from tools import get_unity_instance_from_context, async_send_with_unity_instance
36
from unity_connection import async_send_command_with_retry
47

58

@@ -13,7 +16,13 @@ class LayersResponse(MCPResponse):
1316
name="project_layers",
1417
description="All layers defined in the project's TagManager with their indices (0-31). Read this before using add_layer or remove_layer tools."
1518
)
16-
async def get_layers() -> LayersResponse | MCPResponse:
19+
async def get_layers(ctx: Context) -> LayersResponse | MCPResponse:
1720
"""Get all project layers with their indices."""
18-
response = await async_send_command_with_retry("get_layers", {})
21+
unity_instance = get_unity_instance_from_context(ctx)
22+
response = await async_send_with_unity_instance(
23+
async_send_command_with_retry,
24+
unity_instance,
25+
"get_layers",
26+
{}
27+
)
1928
return LayersResponse(**response) if isinstance(response, dict) else response

MCPForUnity/UnityMcpServer~/src/resources/prefab_stage.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
from pydantic import BaseModel
2+
from fastmcp import Context
3+
24
from models import MCPResponse
35
from registry import mcp_for_unity_resource
6+
from tools import get_unity_instance_from_context, async_send_with_unity_instance
47
from unity_connection import async_send_command_with_retry
58

69

@@ -23,7 +26,13 @@ class PrefabStageResponse(MCPResponse):
2326
name="editor_prefab_stage",
2427
description="Current prefab editing context if a prefab is open in isolation mode. Returns isOpen=false if no prefab is being edited."
2528
)
26-
async def get_prefab_stage() -> PrefabStageResponse | MCPResponse:
29+
async def get_prefab_stage(ctx: Context) -> PrefabStageResponse | MCPResponse:
2730
"""Get current prefab stage information."""
28-
response = await async_send_command_with_retry("get_prefab_stage", {})
31+
unity_instance = get_unity_instance_from_context(ctx)
32+
response = await async_send_with_unity_instance(
33+
async_send_command_with_retry,
34+
unity_instance,
35+
"get_prefab_stage",
36+
{}
37+
)
2938
return PrefabStageResponse(**response) if isinstance(response, dict) else response

MCPForUnity/UnityMcpServer~/src/resources/project_info.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
from pydantic import BaseModel
2+
from fastmcp import Context
3+
24
from models import MCPResponse
35
from registry import mcp_for_unity_resource
6+
from tools import get_unity_instance_from_context, async_send_with_unity_instance
47
from unity_connection import async_send_command_with_retry
58

69

@@ -23,7 +26,13 @@ class ProjectInfoResponse(MCPResponse):
2326
name="project_info",
2427
description="Static project information including root path, Unity version, and platform. This data rarely changes."
2528
)
26-
async def get_project_info() -> ProjectInfoResponse | MCPResponse:
29+
async def get_project_info(ctx: Context) -> ProjectInfoResponse | MCPResponse:
2730
"""Get static project configuration information."""
28-
response = await async_send_command_with_retry("get_project_info", {})
31+
unity_instance = get_unity_instance_from_context(ctx)
32+
response = await async_send_with_unity_instance(
33+
async_send_command_with_retry,
34+
unity_instance,
35+
"get_project_info",
36+
{}
37+
)
2938
return ProjectInfoResponse(**response) if isinstance(response, dict) else response

MCPForUnity/UnityMcpServer~/src/resources/selection.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
from pydantic import BaseModel
2+
from fastmcp import Context
3+
24
from models import MCPResponse
35
from registry import mcp_for_unity_resource
6+
from tools import get_unity_instance_from_context, async_send_with_unity_instance
47
from unity_connection import async_send_command_with_retry
58

69

@@ -39,7 +42,13 @@ class SelectionResponse(MCPResponse):
3942
name="editor_selection",
4043
description="Detailed information about currently selected objects in the editor, including GameObjects, assets, and their properties."
4144
)
42-
async def get_selection() -> SelectionResponse | MCPResponse:
45+
async def get_selection(ctx: Context) -> SelectionResponse | MCPResponse:
4346
"""Get detailed editor selection information."""
44-
response = await async_send_command_with_retry("get_selection", {})
47+
unity_instance = get_unity_instance_from_context(ctx)
48+
response = await async_send_with_unity_instance(
49+
async_send_command_with_retry,
50+
unity_instance,
51+
"get_selection",
52+
{}
53+
)
4554
return SelectionResponse(**response) if isinstance(response, dict) else response

MCPForUnity/UnityMcpServer~/src/resources/tags.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
from pydantic import Field
2+
from fastmcp import Context
3+
24
from models import MCPResponse
35
from registry import mcp_for_unity_resource
6+
from tools import get_unity_instance_from_context, async_send_with_unity_instance
47
from unity_connection import async_send_command_with_retry
58

69

@@ -14,7 +17,13 @@ class TagsResponse(MCPResponse):
1417
name="project_tags",
1518
description="All tags defined in the project's TagManager. Read this before using add_tag or remove_tag tools."
1619
)
17-
async def get_tags() -> TagsResponse | MCPResponse:
20+
async def get_tags(ctx: Context) -> TagsResponse | MCPResponse:
1821
"""Get all project tags."""
19-
response = await async_send_command_with_retry("get_tags", {})
22+
unity_instance = get_unity_instance_from_context(ctx)
23+
response = await async_send_with_unity_instance(
24+
async_send_command_with_retry,
25+
unity_instance,
26+
"get_tags",
27+
{}
28+
)
2029
return TagsResponse(**response) if isinstance(response, dict) else response

MCPForUnity/UnityMcpServer~/src/resources/windows.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
from pydantic import BaseModel
2+
from fastmcp import Context
3+
24
from models import MCPResponse
35
from registry import mcp_for_unity_resource
6+
from tools import get_unity_instance_from_context, async_send_with_unity_instance
47
from unity_connection import async_send_command_with_retry
58

69

@@ -31,7 +34,13 @@ class WindowsResponse(MCPResponse):
3134
name="editor_windows",
3235
description="All currently open editor windows with their titles, types, positions, and focus state."
3336
)
34-
async def get_windows() -> WindowsResponse | MCPResponse:
37+
async def get_windows(ctx: Context) -> WindowsResponse | MCPResponse:
3538
"""Get all open editor windows."""
36-
response = await async_send_command_with_retry("get_windows", {})
39+
unity_instance = get_unity_instance_from_context(ctx)
40+
response = await async_send_with_unity_instance(
41+
async_send_command_with_retry,
42+
unity_instance,
43+
"get_windows",
44+
{}
45+
)
3746
return WindowsResponse(**response) if isinstance(response, dict) else response

0 commit comments

Comments
 (0)