Skip to content

Commit 3f37682

Browse files
committed
Add a function to reload the domain
Closes #357
1 parent 5e4b554 commit 3f37682

File tree

5 files changed

+91
-4
lines changed

5 files changed

+91
-4
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using MCPForUnity.Editor.Helpers;
3+
using Newtonsoft.Json.Linq;
4+
using UnityEditor;
5+
6+
namespace MCPForUnity.Editor.Tools
7+
{
8+
/// <summary>
9+
/// Handles domain reload operations to refresh Unity's script assemblies.
10+
/// This is essential after creating or modifying scripts to make new types available.
11+
/// </summary>
12+
[McpForUnityTool("reload_domain")]
13+
public static class ReloadDomain
14+
{
15+
/// <summary>
16+
/// Main handler for domain reload command.
17+
/// Triggers Unity to reload all script assemblies, which is necessary after
18+
/// script changes before new components can be used.
19+
/// </summary>
20+
public static object HandleCommand(JObject @params)
21+
{
22+
try
23+
{
24+
McpLog.Info("[ReloadDomain] Requesting domain reload");
25+
EditorUtility.RequestScriptReload();
26+
return Response.Success("Domain reload requested. Unity will recompile scripts and refresh assemblies.");
27+
}
28+
catch (Exception ex)
29+
{
30+
McpLog.Error($"[ReloadDomain] Error requesting domain reload: {ex.Message}");
31+
return Response.Error($"Failed to request domain reload: {ex.Message}");
32+
}
33+
}
34+
}
35+
}

MCPForUnity/UnityMcpServer~/src/server.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,10 @@ def _emit_startup():
168168
- `manage_gameobject`: Manages GameObjects in the scene.\n
169169
- `manage_script`: Manages C# script files.\n
170170
- `manage_asset`: Manages prefabs and assets.\n
171-
- `manage_shader`: Manages shaders.\n\n
172-
- Tips:\n
171+
- `manage_shader`: Manages shaders.\n
172+
- `reload_domain`: Triggers Unity domain reload to recompile scripts and refresh assemblies.\n\n
173+
Tips:\n
174+
- Always reload the domain after creating or modifying scripts before attempting to use them. Use `reload_domain` immediately after script changes, then check `read_console` for compile errors.\n
173175
- Create prefabs for reusable GameObjects.\n
174176
- Always include a camera and main light in your scenes.\n
175177
- Unless specified otherwise, paths are relative to the project's `Assets/` folder.\n
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from fastmcp import Context
2+
from models import MCPResponse
3+
from registry import mcp_for_unity_tool
4+
from unity_connection import async_send_command_with_retry
5+
6+
7+
@mcp_for_unity_tool(
8+
description="Trigger a Unity domain reload to recompile scripts and refresh assemblies. Essential after creating or modifying scripts before new components can be used."
9+
)
10+
async def reload_domain(ctx: Context) -> MCPResponse:
11+
"""
12+
Request Unity to reload its domain (script assemblies).
13+
This is necessary after:
14+
15+
- Creating new C# scripts
16+
- Modifying existing scripts
17+
- Before attempting to add new components to GameObjects
18+
19+
Returns immediately after triggering the reload request.
20+
Unity will handle the actual recompilation asynchronously.
21+
"""
22+
await ctx.info("Requesting Unity domain reload")
23+
result = await async_send_command_with_retry("reload_domain", {})
24+
return MCPResponse(**result) if isinstance(result, dict) else result

Server/server.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,10 @@ def _emit_startup():
168168
- `manage_gameobject`: Manages GameObjects in the scene.\n
169169
- `manage_script`: Manages C# script files.\n
170170
- `manage_asset`: Manages prefabs and assets.\n
171-
- `manage_shader`: Manages shaders.\n\n
172-
- Tips:\n
171+
- `manage_shader`: Manages shaders.\n
172+
- `reload_domain`: Triggers Unity domain reload to recompile scripts and refresh assemblies.\n\n
173+
Tips:\n
174+
- Always reload the domain after creating or modifying scripts before attempting to use them. Use `reload_domain` immediately after script changes, then check `read_console` for compile errors.\n
173175
- Create prefabs for reusable GameObjects.\n
174176
- Always include a camera and main light in your scenes.\n
175177
- Unless specified otherwise, paths are relative to the project's `Assets/` folder.\n

Server/tools/reload_domain.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from fastmcp import Context
2+
from models import MCPResponse
3+
from registry import mcp_for_unity_tool
4+
from unity_connection import async_send_command_with_retry
5+
6+
7+
@mcp_for_unity_tool(
8+
description="Trigger a Unity domain reload to recompile scripts and refresh assemblies. Essential after creating or modifying scripts before new components can be used."
9+
)
10+
async def reload_domain(ctx: Context) -> MCPResponse:
11+
"""
12+
Request Unity to reload its domain (script assemblies).
13+
This is necessary after:
14+
15+
- Creating new C# scripts
16+
- Modifying existing scripts
17+
- Before attempting to add new components to GameObjects
18+
19+
Returns immediately after triggering the reload request.
20+
Unity will handle the actual recompilation asynchronously.
21+
"""
22+
await ctx.info("Requesting Unity domain reload")
23+
result = await async_send_command_with_retry("reload_domain", {})
24+
return MCPResponse(**result) if isinstance(result, dict) else result

0 commit comments

Comments
 (0)