Skip to content

Commit 82e49f9

Browse files
authored
Merge pull request #473 from IvanMurzak/471-mcp-server-does-not-start-automatically-when-unity-project-is-opened-in-background
fix: MCP server does not start automatically when unity project is opened in background
2 parents 8694be6 + 27aa636 commit 82e49f9

2 files changed

Lines changed: 22 additions & 27 deletions

File tree

Unity-MCP-Plugin/Assets/root/Editor/Scripts/McpServerManager.cs

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,22 @@ static McpServerManager()
7070
EditorApplication.quitting += OnEditorQuitting;
7171

7272
// Check if server process is still running (e.g., after domain reload)
73-
CheckExistingProcess();
73+
EditorApplication.update += CheckExistingProcess;
74+
75+
DownloadServerBinaryIfNeeded()
76+
.ContinueWith(task =>
77+
{
78+
if (task.IsFaulted || !task.Result)
79+
return; // Failed to download binaries, skip auto-start
80+
81+
if (!task.Result)
82+
return; // No binaries available (either in CI or failed to download), skip auto-start
83+
84+
if (EnvironmentUtils.IsCi())
85+
return; // Skip auto-start in CI environment
86+
87+
EditorApplication.update += StartServerIfNeeded;
88+
});
7489
}
7590

7691
#region Binary Metadata
@@ -467,6 +482,7 @@ public static string DockerRunCommand()
467482

468483
static void CheckExistingProcess()
469484
{
485+
EditorApplication.update -= CheckExistingProcess;
470486
// Try to find an existing server process by checking if our tracked PID is still running
471487
// This helps maintain state across domain reloads
472488
var savedPid = EditorPrefs.GetInt(ProcessIdKey, -1);
@@ -1055,21 +1071,15 @@ static void CleanupProcess()
10551071
}
10561072
}
10571073

1058-
public static void ToggleServer()
1059-
{
1060-
if (IsRunning)
1061-
StopServer();
1062-
else
1063-
StartServer();
1064-
}
1065-
10661074
/// <summary>
10671075
/// Starts the MCP server if KeepServerRunning is enabled and no external server is detected.
10681076
/// This method is called during Unity Editor startup to auto-start the server based on user preference.
10691077
/// The external server check is performed asynchronously to avoid blocking the main thread.
10701078
/// </summary>
10711079
public static void StartServerIfNeeded()
10721080
{
1081+
EditorApplication.update -= StartServerIfNeeded;
1082+
10731083
// Check if user wants the server to keep running
10741084
if (!UnityMcpPlugin.KeepServerRunning)
10751085
{
@@ -1126,10 +1136,9 @@ static void CheckExternalServerAsync(int port, Action<bool> onResult)
11261136
{
11271137
_logger.LogDebug("CheckExternalServerAsync: No server detected on port {port} ({message})", port, ex.Message);
11281138
}
1129-
1130-
// Marshal callback back to the main thread
1131-
EditorApplication.delayCall += () => onResult(result);
1132-
});
1139+
return result;
1140+
})
1141+
.ContinueWith(task => onResult(task.Result), TaskScheduler.FromCurrentSynchronizationContext());
11331142
}
11341143

11351144
#endregion // Process Lifecycle

Unity-MCP-Plugin/Assets/root/Editor/Scripts/Startup.cs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,6 @@ static Startup()
2828
UnityMcpPlugin.Instance.BuildMcpPluginIfNeeded();
2929
UnityMcpPlugin.Instance.AddUnityLogCollectorIfNeeded(() => new BufferedFileLogStorage());
3030

31-
// Defer connection to avoid blocking during domain reload.
32-
// Starting async SignalR connections during [InitializeOnLoad] can cause
33-
// Unity to freeze because async continuations may run on the main thread
34-
// while it's still processing the domain reload.
35-
if (!EnvironmentUtils.IsCi())
36-
EditorApplication.delayCall += () => UnityMcpPlugin.ConnectIfNeeded();
37-
38-
McpServerManager.DownloadServerBinaryIfNeeded();
39-
40-
// Defer MCP server auto-start to avoid blocking during domain reload
41-
// and to ensure configuration is fully loaded
42-
if (!EnvironmentUtils.IsCi())
43-
EditorApplication.delayCall += () => McpServerManager.StartServerIfNeeded();
44-
4531
if (Application.dataPath.Contains(" "))
4632
Debug.LogError("The project path contains spaces, which may cause issues during usage of AI Game Developer. Please consider the move the project to a folder without spaces.");
4733

0 commit comments

Comments
 (0)