2025-12-10 08:00:30 +08:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
2025-12-30 06:39:03 +08:00
|
|
|
using MCPForUnity.Editor.Constants;
|
2025-12-10 08:00:30 +08:00
|
|
|
using MCPForUnity.Editor.Helpers;
|
|
|
|
|
using MCPForUnity.Editor.Services;
|
|
|
|
|
using MCPForUnity.Editor.Windows.Components.ClientConfig;
|
|
|
|
|
using MCPForUnity.Editor.Windows.Components.Connection;
|
|
|
|
|
using MCPForUnity.Editor.Windows.Components.Settings;
|
2025-12-30 06:39:03 +08:00
|
|
|
using MCPForUnity.Editor.Windows.Components.Tools;
|
2025-12-10 08:00:30 +08:00
|
|
|
using UnityEditor;
|
|
|
|
|
using UnityEditor.UIElements;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using UnityEngine.UIElements;
|
|
|
|
|
|
|
|
|
|
namespace MCPForUnity.Editor.Windows
|
|
|
|
|
{
|
|
|
|
|
public class MCPForUnityEditorWindow : EditorWindow
|
|
|
|
|
{
|
|
|
|
|
// Section controllers
|
|
|
|
|
private McpSettingsSection settingsSection;
|
|
|
|
|
private McpConnectionSection connectionSection;
|
|
|
|
|
private McpClientConfigSection clientConfigSection;
|
|
|
|
|
private McpToolsSection toolsSection;
|
|
|
|
|
|
|
|
|
|
private ToolbarToggle settingsTabToggle;
|
|
|
|
|
private ToolbarToggle toolsTabToggle;
|
|
|
|
|
private VisualElement settingsPanel;
|
|
|
|
|
private VisualElement toolsPanel;
|
|
|
|
|
|
|
|
|
|
private static readonly HashSet<MCPForUnityEditorWindow> OpenWindows = new();
|
|
|
|
|
private bool guiCreated = false;
|
2025-12-30 06:39:03 +08:00
|
|
|
private bool toolsLoaded = false;
|
2025-12-10 08:00:30 +08:00
|
|
|
private double lastRefreshTime = 0;
|
|
|
|
|
private const double RefreshDebounceSeconds = 0.5;
|
|
|
|
|
|
|
|
|
|
private enum ActivePanel
|
|
|
|
|
{
|
|
|
|
|
Settings,
|
|
|
|
|
Tools
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal static void CloseAllWindows()
|
|
|
|
|
{
|
|
|
|
|
var windows = OpenWindows.Where(window => window != null).ToArray();
|
|
|
|
|
foreach (var window in windows)
|
|
|
|
|
{
|
|
|
|
|
window.Close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void ShowWindow()
|
|
|
|
|
{
|
|
|
|
|
var window = GetWindow<MCPForUnityEditorWindow>("MCP For Unity");
|
|
|
|
|
window.minSize = new Vector2(500, 600);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Helper to check and manage open windows from other classes
|
|
|
|
|
public static bool HasAnyOpenWindow()
|
|
|
|
|
{
|
|
|
|
|
return OpenWindows.Count > 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void CloseAllOpenWindows()
|
|
|
|
|
{
|
|
|
|
|
if (OpenWindows.Count == 0)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// Copy to array to avoid modifying the collection while iterating
|
|
|
|
|
var arr = new MCPForUnityEditorWindow[OpenWindows.Count];
|
|
|
|
|
OpenWindows.CopyTo(arr);
|
|
|
|
|
foreach (var window in arr)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
window?.Close();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
McpLog.Warn($"Error closing MCP window: {ex.Message}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void CreateGUI()
|
|
|
|
|
{
|
|
|
|
|
// Guard against repeated CreateGUI calls (e.g., domain reloads)
|
|
|
|
|
if (guiCreated)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
string basePath = AssetPathUtility.GetMcpPackageRootPath();
|
|
|
|
|
|
|
|
|
|
// Load main window UXML
|
|
|
|
|
var visualTree = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(
|
|
|
|
|
$"{basePath}/Editor/Windows/MCPForUnityEditorWindow.uxml"
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (visualTree == null)
|
|
|
|
|
{
|
|
|
|
|
McpLog.Error(
|
|
|
|
|
$"Failed to load UXML at: {basePath}/Editor/Windows/MCPForUnityEditorWindow.uxml"
|
|
|
|
|
);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
visualTree.CloneTree(rootVisualElement);
|
|
|
|
|
|
|
|
|
|
// Load main window USS
|
|
|
|
|
var mainStyleSheet = AssetDatabase.LoadAssetAtPath<StyleSheet>(
|
|
|
|
|
$"{basePath}/Editor/Windows/MCPForUnityEditorWindow.uss"
|
|
|
|
|
);
|
|
|
|
|
if (mainStyleSheet != null)
|
|
|
|
|
{
|
|
|
|
|
rootVisualElement.styleSheets.Add(mainStyleSheet);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Load common USS
|
|
|
|
|
var commonStyleSheet = AssetDatabase.LoadAssetAtPath<StyleSheet>(
|
|
|
|
|
$"{basePath}/Editor/Windows/Components/Common.uss"
|
|
|
|
|
);
|
|
|
|
|
if (commonStyleSheet != null)
|
|
|
|
|
{
|
|
|
|
|
rootVisualElement.styleSheets.Add(commonStyleSheet);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
settingsPanel = rootVisualElement.Q<VisualElement>("settings-panel");
|
|
|
|
|
toolsPanel = rootVisualElement.Q<VisualElement>("tools-panel");
|
|
|
|
|
var settingsContainer = rootVisualElement.Q<VisualElement>("settings-container");
|
|
|
|
|
var toolsContainer = rootVisualElement.Q<VisualElement>("tools-container");
|
|
|
|
|
|
|
|
|
|
if (settingsPanel == null || toolsPanel == null)
|
|
|
|
|
{
|
|
|
|
|
McpLog.Error("Failed to find tab panels in UXML");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (settingsContainer == null)
|
|
|
|
|
{
|
|
|
|
|
McpLog.Error("Failed to find settings-container in UXML");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (toolsContainer == null)
|
|
|
|
|
{
|
|
|
|
|
McpLog.Error("Failed to find tools-container in UXML");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SetupTabs();
|
|
|
|
|
|
|
|
|
|
// Load and initialize Settings section
|
|
|
|
|
var settingsTree = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(
|
|
|
|
|
$"{basePath}/Editor/Windows/Components/Settings/McpSettingsSection.uxml"
|
|
|
|
|
);
|
|
|
|
|
if (settingsTree != null)
|
|
|
|
|
{
|
|
|
|
|
var settingsRoot = settingsTree.Instantiate();
|
|
|
|
|
settingsContainer.Add(settingsRoot);
|
|
|
|
|
settingsSection = new McpSettingsSection(settingsRoot);
|
|
|
|
|
settingsSection.OnGitUrlChanged += () =>
|
|
|
|
|
clientConfigSection?.UpdateManualConfiguration();
|
|
|
|
|
settingsSection.OnHttpServerCommandUpdateRequested += () =>
|
|
|
|
|
connectionSection?.UpdateHttpServerCommandDisplay();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Load and initialize Connection section
|
|
|
|
|
var connectionTree = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(
|
|
|
|
|
$"{basePath}/Editor/Windows/Components/Connection/McpConnectionSection.uxml"
|
|
|
|
|
);
|
|
|
|
|
if (connectionTree != null)
|
|
|
|
|
{
|
|
|
|
|
var connectionRoot = connectionTree.Instantiate();
|
|
|
|
|
settingsContainer.Add(connectionRoot);
|
|
|
|
|
connectionSection = new McpConnectionSection(connectionRoot);
|
|
|
|
|
connectionSection.OnManualConfigUpdateRequested += () =>
|
|
|
|
|
clientConfigSection?.UpdateManualConfiguration();
|
HTTP setup overhaul: transport selection (HTTP local/remote vs stdio), safer lifecycle, cleaner UI, better Claude Code integration (#499)
* Avoid blocking Claude CLI status checks on focus
* Fix Claude Code registration to remove existing server before re-registering
When registering with Claude Code, if a UnityMCP server already exists,
remove it first before adding the new registration. This ensures the
transport mode (HTTP vs stdio) is always updated to match the current
UseHttpTransport EditorPref setting.
Previously, if a stdio registration existed and the user tried to register
with HTTP, the command would fail with 'already exists' and the old stdio
configuration would remain unchanged.
* Fix Claude Code transport validation to parse CLI output format correctly
The validation code was incorrectly parsing the output of 'claude mcp get UnityMCP' by looking for JSON format ("transport": "http"), but the CLI actually returns human-readable text format ("Type: http"). This caused the transport mismatch detection to never trigger, allowing stdio to be selected in the UI while HTTP was registered with Claude Code.
Changes:
- Fix parsing logic to check for "Type: http" or "Type: stdio" in CLI output
- Add OnTransportChanged event to refresh client status when transport changes
- Wire up event handler to trigger client status refresh on transport dropdown change
This ensures that when the transport mode in Unity doesn't match what's registered with Claude Code, the UI will correctly show an error status with instructions to re-register.
* Fix Claude Code registration UI blocking and thread safety issues
This commit resolves three issues with Claude Code registration:
1. UI blocking: Removed synchronous CheckStatus() call after registration
that was blocking the editor. Status is now set immediately with async
verification happening in the background.
2. Thread safety: Fixed "can only be called from the main thread" errors
by capturing Application.dataPath and EditorPrefs.GetBool() on the main
thread before spawning async status check tasks.
3. Transport mismatch detection: Transport mode changes now trigger immediate
status checks to detect HTTP/stdio mismatches, instead of waiting for the
45-second refresh interval.
The registration button now turns green immediately after successful
registration without blocking, and properly detects transport mismatches
when switching between HTTP and stdio modes.
* Enforce thread safety for Claude Code status checks at compile time
Address code review feedback by making CheckStatusWithProjectDir thread-safe
by design rather than by convention:
1. Made projectDir and useHttpTransport parameters non-nullable to prevent
accidental background thread calls without captured values
2. Removed nullable fallback to EditorPrefs.GetBool() which would cause
thread safety violations if called from background threads
3. Added ArgumentNullException for null projectDir instead of falling back
to Application.dataPath (which is main-thread only)
4. Added XML documentation clearly stating threading contracts:
- CheckStatus() must be called from main thread
- CheckStatusWithProjectDir() is safe for background threads
5. Removed unreachable else branch in async status check code
These changes make it impossible to misuse the API from background threads,
with compile-time enforcement instead of runtime errors.
* Consolidate local HTTP Start/Stop and auto-start session
* HTTP improvements: Unity-owned server lifecycle + UI polish
* Deterministic HTTP stop via pidfile+token; spawn server in terminal
* Fix review feedback: token validation, host normalization, safer casts
* Fix stop heuristics edge cases; remove dead pid capture
* Fix unity substring guard in stop heuristics
* Fix local server cleanup and connection checks
* Fix read_console default limits; cleanup Unity-managed server vestiges
* Fix unfocused reconnect stalls; fast-fail retryable Unity commands
* Simplify PluginHub reload handling; honor run_tests timeout
* Fix Windows Claude CLI status check threading
2026-01-02 09:08:51 +08:00
|
|
|
connectionSection.OnTransportChanged += () =>
|
|
|
|
|
clientConfigSection?.RefreshSelectedClient(forceImmediate: true);
|
2025-12-10 08:00:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Load and initialize Client Configuration section
|
|
|
|
|
var clientConfigTree = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(
|
|
|
|
|
$"{basePath}/Editor/Windows/Components/ClientConfig/McpClientConfigSection.uxml"
|
|
|
|
|
);
|
|
|
|
|
if (clientConfigTree != null)
|
|
|
|
|
{
|
|
|
|
|
var clientConfigRoot = clientConfigTree.Instantiate();
|
|
|
|
|
settingsContainer.Add(clientConfigRoot);
|
|
|
|
|
clientConfigSection = new McpClientConfigSection(clientConfigRoot);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Load and initialize Tools section
|
|
|
|
|
var toolsTree = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(
|
|
|
|
|
$"{basePath}/Editor/Windows/Components/Tools/McpToolsSection.uxml"
|
|
|
|
|
);
|
|
|
|
|
if (toolsTree != null)
|
|
|
|
|
{
|
|
|
|
|
var toolsRoot = toolsTree.Instantiate();
|
|
|
|
|
toolsContainer.Add(toolsRoot);
|
|
|
|
|
toolsSection = new McpToolsSection(toolsRoot);
|
2025-12-30 06:39:03 +08:00
|
|
|
|
|
|
|
|
if (toolsTabToggle != null && toolsTabToggle.value)
|
|
|
|
|
{
|
|
|
|
|
EnsureToolsLoaded();
|
|
|
|
|
}
|
2025-12-10 08:00:30 +08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
McpLog.Warn("Failed to load tools section UXML. Tool configuration will be unavailable.");
|
|
|
|
|
}
|
2025-12-30 06:39:03 +08:00
|
|
|
|
2025-12-10 08:00:30 +08:00
|
|
|
guiCreated = true;
|
|
|
|
|
|
|
|
|
|
// Initial updates
|
|
|
|
|
RefreshAllData();
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-30 06:39:03 +08:00
|
|
|
private void EnsureToolsLoaded()
|
|
|
|
|
{
|
|
|
|
|
if (toolsLoaded)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (toolsSection == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
toolsLoaded = true;
|
|
|
|
|
toolsSection.Refresh();
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-10 08:00:30 +08:00
|
|
|
private void OnEnable()
|
|
|
|
|
{
|
|
|
|
|
EditorApplication.update += OnEditorUpdate;
|
|
|
|
|
OpenWindows.Add(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnDisable()
|
|
|
|
|
{
|
|
|
|
|
EditorApplication.update -= OnEditorUpdate;
|
|
|
|
|
OpenWindows.Remove(this);
|
|
|
|
|
guiCreated = false;
|
2025-12-30 06:39:03 +08:00
|
|
|
toolsLoaded = false;
|
2025-12-10 08:00:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnFocus()
|
|
|
|
|
{
|
|
|
|
|
// Only refresh data if UI is built
|
|
|
|
|
if (rootVisualElement == null || rootVisualElement.childCount == 0)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
RefreshAllData();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnEditorUpdate()
|
|
|
|
|
{
|
|
|
|
|
if (rootVisualElement == null || rootVisualElement.childCount == 0)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
connectionSection?.UpdateConnectionStatus();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void RefreshAllData()
|
|
|
|
|
{
|
|
|
|
|
// Debounce rapid successive calls (e.g., from OnFocus being called multiple times)
|
|
|
|
|
double currentTime = EditorApplication.timeSinceStartup;
|
|
|
|
|
if (currentTime - lastRefreshTime < RefreshDebounceSeconds)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
lastRefreshTime = currentTime;
|
|
|
|
|
|
|
|
|
|
connectionSection?.UpdateConnectionStatus();
|
|
|
|
|
|
|
|
|
|
if (MCPServiceLocator.Bridge.IsRunning)
|
|
|
|
|
{
|
|
|
|
|
_ = connectionSection?.VerifyBridgeConnectionAsync();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
settingsSection?.UpdatePathOverrides();
|
|
|
|
|
clientConfigSection?.RefreshSelectedClient();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SetupTabs()
|
|
|
|
|
{
|
|
|
|
|
settingsTabToggle = rootVisualElement.Q<ToolbarToggle>("settings-tab");
|
|
|
|
|
toolsTabToggle = rootVisualElement.Q<ToolbarToggle>("tools-tab");
|
|
|
|
|
|
|
|
|
|
settingsPanel?.RemoveFromClassList("hidden");
|
|
|
|
|
toolsPanel?.RemoveFromClassList("hidden");
|
|
|
|
|
|
|
|
|
|
if (settingsTabToggle != null)
|
|
|
|
|
{
|
|
|
|
|
settingsTabToggle.RegisterValueChangedCallback(evt =>
|
|
|
|
|
{
|
|
|
|
|
if (!evt.newValue)
|
|
|
|
|
{
|
|
|
|
|
if (toolsTabToggle != null && !toolsTabToggle.value)
|
|
|
|
|
{
|
|
|
|
|
settingsTabToggle.SetValueWithoutNotify(true);
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SwitchPanel(ActivePanel.Settings);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (toolsTabToggle != null)
|
|
|
|
|
{
|
|
|
|
|
toolsTabToggle.RegisterValueChangedCallback(evt =>
|
|
|
|
|
{
|
|
|
|
|
if (!evt.newValue)
|
|
|
|
|
{
|
|
|
|
|
if (settingsTabToggle != null && !settingsTabToggle.value)
|
|
|
|
|
{
|
|
|
|
|
toolsTabToggle.SetValueWithoutNotify(true);
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SwitchPanel(ActivePanel.Tools);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var savedPanel = EditorPrefs.GetString(EditorPrefKeys.EditorWindowActivePanel, ActivePanel.Settings.ToString());
|
|
|
|
|
if (!Enum.TryParse(savedPanel, out ActivePanel initialPanel))
|
|
|
|
|
{
|
|
|
|
|
initialPanel = ActivePanel.Settings;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SwitchPanel(initialPanel);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SwitchPanel(ActivePanel panel)
|
|
|
|
|
{
|
|
|
|
|
bool showSettings = panel == ActivePanel.Settings;
|
|
|
|
|
|
|
|
|
|
if (settingsPanel != null)
|
|
|
|
|
{
|
|
|
|
|
settingsPanel.style.display = showSettings ? DisplayStyle.Flex : DisplayStyle.None;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (toolsPanel != null)
|
|
|
|
|
{
|
|
|
|
|
toolsPanel.style.display = showSettings ? DisplayStyle.None : DisplayStyle.Flex;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
settingsTabToggle?.SetValueWithoutNotify(showSettings);
|
|
|
|
|
toolsTabToggle?.SetValueWithoutNotify(!showSettings);
|
|
|
|
|
|
2025-12-30 06:39:03 +08:00
|
|
|
if (!showSettings)
|
|
|
|
|
{
|
|
|
|
|
EnsureToolsLoaded();
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-10 08:00:30 +08:00
|
|
|
EditorPrefs.SetString(EditorPrefKeys.EditorWindowActivePanel, panel.ToString());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal static void RequestHealthVerification()
|
|
|
|
|
{
|
|
|
|
|
foreach (var window in OpenWindows)
|
|
|
|
|
{
|
|
|
|
|
window?.ScheduleHealthCheck();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ScheduleHealthCheck()
|
|
|
|
|
{
|
|
|
|
|
EditorApplication.delayCall += async () =>
|
|
|
|
|
{
|
|
|
|
|
// Ensure window and components are still valid before execution
|
|
|
|
|
if (this == null || connectionSection == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await connectionSection.VerifyBridgeConnectionAsync();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
// Log but don't crash if verification fails during cleanup
|
|
|
|
|
McpLog.Warn($"Health check verification failed: {ex.Message}");
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|