diff --git a/MCPForUnity/Editor/Helpers/HttpEndpointUtility.cs b/MCPForUnity/Editor/Helpers/HttpEndpointUtility.cs index 94e2f3be90..ae5ff43d6d 100644 --- a/MCPForUnity/Editor/Helpers/HttpEndpointUtility.cs +++ b/MCPForUnity/Editor/Helpers/HttpEndpointUtility.cs @@ -14,6 +14,9 @@ namespace MCPForUnity.Editor.Helpers /// /// HTTP Local and HTTP Remote use separate EditorPrefs keys so that switching /// between scopes does not overwrite the other scope's URL. + /// + /// Both URLs are stored per project (see ), so editors + /// for different projects on the same machine can point at different servers. /// public static class HttpEndpointUtility { @@ -51,7 +54,7 @@ public static void SaveBaseUrl(string userValue) /// public static string GetLocalBaseUrl() { - string stored = EditorPrefs.GetString(LocalPrefKey, DefaultLocalBaseUrl); + string stored = ReadProjectUrlPref(LocalPrefKey, DefaultLocalBaseUrl); return NormalizeBaseUrl(stored, DefaultLocalBaseUrl, remoteScope: false); } @@ -61,7 +64,7 @@ public static string GetLocalBaseUrl() public static void SaveLocalBaseUrl(string userValue) { string normalized = NormalizeBaseUrl(userValue, DefaultLocalBaseUrl, remoteScope: false); - EditorPrefs.SetString(LocalPrefKey, normalized); + WriteProjectUrlPref(LocalPrefKey, normalized); } /// @@ -70,7 +73,7 @@ public static void SaveLocalBaseUrl(string userValue) /// public static string GetRemoteBaseUrl() { - string stored = EditorPrefs.GetString(RemotePrefKey, DefaultRemoteBaseUrl); + string stored = ReadProjectUrlPref(RemotePrefKey, DefaultRemoteBaseUrl); if (string.IsNullOrWhiteSpace(stored)) { return DefaultRemoteBaseUrl; @@ -85,11 +88,35 @@ public static void SaveRemoteBaseUrl(string userValue) { if (string.IsNullOrWhiteSpace(userValue)) { - EditorPrefs.SetString(RemotePrefKey, DefaultRemoteBaseUrl); + WriteProjectUrlPref(RemotePrefKey, DefaultRemoteBaseUrl); return; } string normalized = NormalizeBaseUrl(userValue, DefaultRemoteBaseUrl, remoteScope: true); - EditorPrefs.SetString(RemotePrefKey, normalized); + WriteProjectUrlPref(RemotePrefKey, normalized); + } + + /// + /// Reads a URL pref ( or + /// ) for the current project. + /// EditorPrefs are machine-wide, so the value is stored under a project-scoped key. + /// Until this project saves its own value, the machine-wide key written by earlier + /// versions is used, so existing setups keep their URL after upgrading. + /// + internal static string ReadProjectUrlPref(string key, string defaultValue) + { + string projectKey = ProjectIdentityUtility.GetProjectScopedKey(key); + return EditorPrefs.HasKey(projectKey) + ? EditorPrefs.GetString(projectKey, defaultValue) + : EditorPrefs.GetString(key, defaultValue); + } + + /// + /// Writes a URL pref for the current project only. The machine-wide key is left + /// untouched so other projects that never saved their own value keep reading it. + /// + internal static void WriteProjectUrlPref(string key, string value) + { + EditorPrefs.SetString(ProjectIdentityUtility.GetProjectScopedKey(key), value); } /// diff --git a/MCPForUnity/Editor/Helpers/ProjectIdentityUtility.cs b/MCPForUnity/Editor/Helpers/ProjectIdentityUtility.cs index 34a53916fe..134c21eba2 100644 --- a/MCPForUnity/Editor/Helpers/ProjectIdentityUtility.cs +++ b/MCPForUnity/Editor/Helpers/ProjectIdentityUtility.cs @@ -75,6 +75,15 @@ public static string GetProjectHash() return _cachedProjectHash; } + /// + /// Returns suffixed with the project hash, for EditorPrefs values + /// that must not be shared by different projects open on the same machine. + /// + public static string GetProjectScopedKey(string key) + { + return $"{key}_{GetProjectHash()}"; + } + /// /// Returns a human friendly project name derived from the Assets directory path, /// or "Unknown" if the name cannot be determined. diff --git a/MCPForUnity/Editor/Services/EditorConfigurationCache.cs b/MCPForUnity/Editor/Services/EditorConfigurationCache.cs index a33243fd8e..d197a42d82 100644 --- a/MCPForUnity/Editor/Services/EditorConfigurationCache.cs +++ b/MCPForUnity/Editor/Services/EditorConfigurationCache.cs @@ -1,5 +1,6 @@ using System; using MCPForUnity.Editor.Constants; +using MCPForUnity.Editor.Helpers; using UnityEditor; namespace MCPForUnity.Editor.Services @@ -95,13 +96,13 @@ public static EditorConfigurationCache Instance public string GitUrlOverride => _gitUrlOverride; /// - /// HTTP base URL for the local MCP server. + /// HTTP base URL for the local MCP server (stored per project). /// Default: empty string /// public string HttpBaseUrl => _httpBaseUrl; /// - /// HTTP base URL for the remote-hosted MCP server. + /// HTTP base URL for the remote-hosted MCP server (stored per project). /// Default: empty string /// public string HttpRemoteBaseUrl => _httpRemoteBaseUrl; @@ -141,8 +142,8 @@ public void Refresh() _devModeForceServerRefresh = EditorPrefs.GetBool(EditorPrefKeys.DevModeForceServerRefresh, false); _uvxPathOverride = EditorPrefs.GetString(EditorPrefKeys.UvxPathOverride, string.Empty); _gitUrlOverride = EditorPrefs.GetString(EditorPrefKeys.GitUrlOverride, string.Empty); - _httpBaseUrl = EditorPrefs.GetString(EditorPrefKeys.HttpBaseUrl, string.Empty); - _httpRemoteBaseUrl = EditorPrefs.GetString(EditorPrefKeys.HttpRemoteBaseUrl, string.Empty); + _httpBaseUrl = HttpEndpointUtility.ReadProjectUrlPref(EditorPrefKeys.HttpBaseUrl, string.Empty); + _httpRemoteBaseUrl = HttpEndpointUtility.ReadProjectUrlPref(EditorPrefKeys.HttpRemoteBaseUrl, string.Empty); _claudeCliPathOverride = EditorPrefs.GetString(EditorPrefKeys.ClaudeCliPathOverride, string.Empty); _httpTransportScope = EditorPrefs.GetString(EditorPrefKeys.HttpTransportScope, string.Empty); _unitySocketPort = EditorPrefs.GetInt(EditorPrefKeys.UnitySocketPort, 0); @@ -242,28 +243,34 @@ public void SetGitUrlOverride(string value) /// /// Set HttpBaseUrl and update cache + EditorPrefs atomically. + /// Always writes the project value, even when it equals the cached one: the cached + /// value may come from the machine-wide fallback, and the project must keep it + /// if that fallback changes later. /// public void SetHttpBaseUrl(string value) { value = value ?? string.Empty; + HttpEndpointUtility.WriteProjectUrlPref(EditorPrefKeys.HttpBaseUrl, value); if (_httpBaseUrl != value) { _httpBaseUrl = value; - EditorPrefs.SetString(EditorPrefKeys.HttpBaseUrl, value); OnConfigurationChanged?.Invoke(nameof(HttpBaseUrl)); } } /// /// Set HttpRemoteBaseUrl and update cache + EditorPrefs atomically. + /// Always writes the project value, even when it equals the cached one: the cached + /// value may come from the machine-wide fallback, and the project must keep it + /// if that fallback changes later. /// public void SetHttpRemoteBaseUrl(string value) { value = value ?? string.Empty; + HttpEndpointUtility.WriteProjectUrlPref(EditorPrefKeys.HttpRemoteBaseUrl, value); if (_httpRemoteBaseUrl != value) { _httpRemoteBaseUrl = value; - EditorPrefs.SetString(EditorPrefKeys.HttpRemoteBaseUrl, value); OnConfigurationChanged?.Invoke(nameof(HttpRemoteBaseUrl)); } } @@ -333,10 +340,10 @@ public void InvalidateKey(string keyName) _gitUrlOverride = EditorPrefs.GetString(EditorPrefKeys.GitUrlOverride, string.Empty); break; case nameof(HttpBaseUrl): - _httpBaseUrl = EditorPrefs.GetString(EditorPrefKeys.HttpBaseUrl, string.Empty); + _httpBaseUrl = HttpEndpointUtility.ReadProjectUrlPref(EditorPrefKeys.HttpBaseUrl, string.Empty); break; case nameof(HttpRemoteBaseUrl): - _httpRemoteBaseUrl = EditorPrefs.GetString(EditorPrefKeys.HttpRemoteBaseUrl, string.Empty); + _httpRemoteBaseUrl = HttpEndpointUtility.ReadProjectUrlPref(EditorPrefKeys.HttpRemoteBaseUrl, string.Empty); break; case nameof(ClaudeCliPathOverride): _claudeCliPathOverride = EditorPrefs.GetString(EditorPrefKeys.ClaudeCliPathOverride, string.Empty); diff --git a/MCPForUnity/Editor/Windows/EditorPrefs/EditorPrefsWindow.cs b/MCPForUnity/Editor/Windows/EditorPrefs/EditorPrefsWindow.cs index 3d011362af..d95b57bf3b 100644 --- a/MCPForUnity/Editor/Windows/EditorPrefs/EditorPrefsWindow.cs +++ b/MCPForUnity/Editor/Windows/EditorPrefs/EditorPrefsWindow.cs @@ -177,6 +177,14 @@ private void LoadKnownMcpKeys() knownMcpKeys.Add(field.GetValue(null).ToString()); } } + + // HTTP URLs are stored under per-project keys; show this project's entries too. + foreach (string key in new[] { EditorPrefKeys.HttpBaseUrl, EditorPrefKeys.HttpRemoteBaseUrl }) + { + string projectKey = ProjectIdentityUtility.GetProjectScopedKey(key); + knownMcpKeys.Add(projectKey); + knownPrefTypes[projectKey] = EditorPrefType.String; + } } private void RefreshPrefs() diff --git a/TestProjects/UnityMCPTests/Assets/Tests/EditMode/Helpers/HttpEndpointProjectScopeTests.cs b/TestProjects/UnityMCPTests/Assets/Tests/EditMode/Helpers/HttpEndpointProjectScopeTests.cs new file mode 100644 index 0000000000..0d7d454393 --- /dev/null +++ b/TestProjects/UnityMCPTests/Assets/Tests/EditMode/Helpers/HttpEndpointProjectScopeTests.cs @@ -0,0 +1,160 @@ +using System.Collections.Generic; +using MCPForUnity.Editor.Constants; +using MCPForUnity.Editor.Helpers; +using MCPForUnity.Editor.Services; +using NUnit.Framework; +using UnityEditor; + +namespace MCPForUnityTests.Editor.Helpers +{ + /// + /// The HTTP Local / HTTP Remote URLs are stored per project so two editors on the same + /// machine can use different servers without overwriting each other's URL. + /// + public class HttpEndpointProjectScopeTests + { + private const string OtherProjectHash = "0123456789abcdef"; + + private static string LocalKey => ProjectIdentityUtility.GetProjectScopedKey(EditorPrefKeys.HttpBaseUrl); + private static string RemoteKey => ProjectIdentityUtility.GetProjectScopedKey(EditorPrefKeys.HttpRemoteBaseUrl); + private static string OtherProjectLocalKey => $"{EditorPrefKeys.HttpBaseUrl}_{OtherProjectHash}"; + + private readonly Dictionary _savedPrefs = new Dictionary(); + + private static IEnumerable TouchedKeys() + { + yield return EditorPrefKeys.HttpBaseUrl; + yield return EditorPrefKeys.HttpRemoteBaseUrl; + yield return LocalKey; + yield return RemoteKey; + yield return OtherProjectLocalKey; + } + + [SetUp] + public void SetUp() + { + _savedPrefs.Clear(); + foreach (string key in TouchedKeys()) + { + _savedPrefs[key] = EditorPrefs.HasKey(key) ? EditorPrefs.GetString(key) : null; + EditorPrefs.DeleteKey(key); + } + } + + [TearDown] + public void TearDown() + { + foreach (var pair in _savedPrefs) + { + if (pair.Value == null) + EditorPrefs.DeleteKey(pair.Key); + else + EditorPrefs.SetString(pair.Key, pair.Value); + } + EditorConfigurationCache.Instance.Refresh(); + } + + [Test] + public void ProjectScopedKey_IsBaseKeyPlusProjectHash() + { + Assert.AreEqual( + $"{EditorPrefKeys.HttpBaseUrl}_{ProjectIdentityUtility.GetProjectHash()}", + LocalKey); + } + + [Test] + public void GetLocalBaseUrl_NothingStored_ReturnsDefault() + { + Assert.AreEqual("http://127.0.0.1:8080", HttpEndpointUtility.GetLocalBaseUrl()); + } + + [Test] + public void GetLocalBaseUrl_NoProjectValue_FallsBackToMachineWideValue() + { + EditorPrefs.SetString(EditorPrefKeys.HttpBaseUrl, "http://127.0.0.1:9001"); + + Assert.AreEqual("http://127.0.0.1:9001", HttpEndpointUtility.GetLocalBaseUrl()); + } + + [Test] + public void SaveLocalBaseUrl_WritesProjectKeyOnly() + { + EditorPrefs.SetString(EditorPrefKeys.HttpBaseUrl, "http://127.0.0.1:9001"); + + HttpEndpointUtility.SaveLocalBaseUrl("http://127.0.0.1:9002"); + + Assert.AreEqual("http://127.0.0.1:9002", EditorPrefs.GetString(LocalKey)); + Assert.AreEqual("http://127.0.0.1:9001", EditorPrefs.GetString(EditorPrefKeys.HttpBaseUrl), + "Machine-wide value must stay as-is for projects that never saved their own URL"); + Assert.AreEqual("http://127.0.0.1:9002", HttpEndpointUtility.GetLocalBaseUrl()); + } + + [Test] + public void GetLocalBaseUrl_IgnoresOtherProjectsValue() + { + EditorPrefs.SetString(OtherProjectLocalKey, "http://127.0.0.1:9003"); + + Assert.AreEqual("http://127.0.0.1:8080", HttpEndpointUtility.GetLocalBaseUrl()); + + HttpEndpointUtility.SaveLocalBaseUrl("http://127.0.0.1:9002"); + + Assert.AreEqual("http://127.0.0.1:9002", HttpEndpointUtility.GetLocalBaseUrl()); + Assert.AreEqual("http://127.0.0.1:9003", EditorPrefs.GetString(OtherProjectLocalKey), + "Saving in this project must not change another project's URL"); + } + + [Test] + public void SaveRemoteBaseUrl_WritesProjectKeyOnly() + { + EditorPrefs.SetString(EditorPrefKeys.HttpRemoteBaseUrl, "https://a.example.com"); + + Assert.AreEqual("https://a.example.com", HttpEndpointUtility.GetRemoteBaseUrl()); + + HttpEndpointUtility.SaveRemoteBaseUrl("b.example.com"); + + Assert.AreEqual("https://b.example.com", HttpEndpointUtility.GetRemoteBaseUrl()); + Assert.AreEqual("https://a.example.com", EditorPrefs.GetString(EditorPrefKeys.HttpRemoteBaseUrl)); + } + + [Test] + public void SaveRemoteBaseUrl_Empty_ClearsThisProjectWithoutFallingBack() + { + EditorPrefs.SetString(EditorPrefKeys.HttpRemoteBaseUrl, "https://a.example.com"); + + HttpEndpointUtility.SaveRemoteBaseUrl(string.Empty); + + Assert.AreEqual(string.Empty, HttpEndpointUtility.GetRemoteBaseUrl()); + Assert.AreEqual("https://a.example.com", EditorPrefs.GetString(EditorPrefKeys.HttpRemoteBaseUrl)); + } + + [Test] + public void ConfigurationCache_ReadsAndWritesProjectValue() + { + EditorPrefs.SetString(EditorPrefKeys.HttpBaseUrl, "http://127.0.0.1:9001"); + HttpEndpointUtility.SaveLocalBaseUrl("http://127.0.0.1:9002"); + EditorConfigurationCache.Instance.Refresh(); + + Assert.AreEqual("http://127.0.0.1:9002", EditorConfigurationCache.Instance.HttpBaseUrl); + + EditorConfigurationCache.Instance.SetHttpRemoteBaseUrl("https://c.example.com"); + + Assert.AreEqual("https://c.example.com", EditorPrefs.GetString(RemoteKey)); + Assert.IsFalse(EditorPrefs.HasKey(EditorPrefKeys.HttpRemoteBaseUrl)); + } + + [Test] + public void ConfigurationCache_SetSameValueAsFallback_PinsItToProject() + { + EditorPrefs.SetString(EditorPrefKeys.HttpBaseUrl, "http://127.0.0.1:9001"); + EditorConfigurationCache.Instance.Refresh(); + Assert.AreEqual("http://127.0.0.1:9001", EditorConfigurationCache.Instance.HttpBaseUrl); + + EditorConfigurationCache.Instance.SetHttpBaseUrl("http://127.0.0.1:9001"); + EditorPrefs.SetString(EditorPrefKeys.HttpBaseUrl, "http://127.0.0.1:9005"); + + Assert.AreEqual("http://127.0.0.1:9001", EditorPrefs.GetString(LocalKey)); + Assert.AreEqual("http://127.0.0.1:9001", HttpEndpointUtility.GetLocalBaseUrl(), + "A URL set for this project must not follow later machine-wide changes"); + } + } +} diff --git a/TestProjects/UnityMCPTests/Assets/Tests/EditMode/Helpers/HttpEndpointProjectScopeTests.cs.meta b/TestProjects/UnityMCPTests/Assets/Tests/EditMode/Helpers/HttpEndpointProjectScopeTests.cs.meta new file mode 100644 index 0000000000..069cee5f53 --- /dev/null +++ b/TestProjects/UnityMCPTests/Assets/Tests/EditMode/Helpers/HttpEndpointProjectScopeTests.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 8181189556464a9693fab19e7bef04a0 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/TestProjects/UnityMCPTests/Assets/Tests/EditMode/Helpers/WriteToConfigTests.cs b/TestProjects/UnityMCPTests/Assets/Tests/EditMode/Helpers/WriteToConfigTests.cs index 8c89253259..db76f25b92 100644 --- a/TestProjects/UnityMCPTests/Assets/Tests/EditMode/Helpers/WriteToConfigTests.cs +++ b/TestProjects/UnityMCPTests/Assets/Tests/EditMode/Helpers/WriteToConfigTests.cs @@ -16,7 +16,7 @@ namespace MCPForUnityTests.Editor.Helpers public class WriteToConfigTests : TransportPreferenceTestBase { private const string UseHttpTransportPrefKey = EditorPrefKeys.UseHttpTransport; - private const string HttpUrlPrefKey = EditorPrefKeys.HttpBaseUrl; + private static string HttpUrlPrefKey => ProjectIdentityUtility.GetProjectScopedKey(EditorPrefKeys.HttpBaseUrl); private string _tempRoot; private string _fakeUvPath; diff --git a/TestProjects/UnityMCPTests/Assets/Tests/EditMode/Services/Characterization/ServerManagementServiceCharacterizationTests.cs b/TestProjects/UnityMCPTests/Assets/Tests/EditMode/Services/Characterization/ServerManagementServiceCharacterizationTests.cs index 6620ac66ce..28852de64c 100644 --- a/TestProjects/UnityMCPTests/Assets/Tests/EditMode/Services/Characterization/ServerManagementServiceCharacterizationTests.cs +++ b/TestProjects/UnityMCPTests/Assets/Tests/EditMode/Services/Characterization/ServerManagementServiceCharacterizationTests.cs @@ -21,6 +21,9 @@ namespace MCPForUnityTests.Editor.Services.Characterization [TestFixture] public class ServerManagementServiceCharacterizationTests : TransportPreferenceTestBase { + private static string LocalUrlKey => ProjectIdentityUtility.GetProjectScopedKey(EditorPrefKeys.HttpBaseUrl); + private static string RemoteUrlKey => ProjectIdentityUtility.GetProjectScopedKey(EditorPrefKeys.HttpRemoteBaseUrl); + private ServerManagementService _service; private bool _savedUseHttpTransport; private string _savedHttpUrl; @@ -37,8 +40,8 @@ public void SetUp() // Save current settings _savedLaunchConfirmed = EditorPrefs.GetBool(EditorPrefKeys.HttpServerLaunchConfirmed, false); _savedUseHttpTransport = EditorPrefs.GetBool(EditorPrefKeys.UseHttpTransport, true); - _savedHttpUrl = EditorPrefs.GetString(EditorPrefKeys.HttpBaseUrl, string.Empty); - _savedHttpRemoteUrl = EditorPrefs.GetString(EditorPrefKeys.HttpRemoteBaseUrl, string.Empty); + _savedHttpUrl = EditorPrefs.GetString(LocalUrlKey, string.Empty); + _savedHttpRemoteUrl = EditorPrefs.GetString(RemoteUrlKey, string.Empty); _savedHttpTransportScope = EditorPrefs.GetString(EditorPrefKeys.HttpTransportScope, string.Empty); _savedAllowLanHttpBind = EditorPrefs.GetBool(EditorPrefKeys.AllowLanHttpBind, false); _savedAllowInsecureRemoteHttp = EditorPrefs.GetBool(EditorPrefKeys.AllowInsecureRemoteHttp, false); @@ -51,19 +54,19 @@ public void TearDown() EditorPrefs.SetBool(EditorPrefKeys.UseHttpTransport, _savedUseHttpTransport); if (!string.IsNullOrEmpty(_savedHttpUrl)) { - EditorPrefs.SetString(EditorPrefKeys.HttpBaseUrl, _savedHttpUrl); + EditorPrefs.SetString(LocalUrlKey, _savedHttpUrl); } else { - EditorPrefs.DeleteKey(EditorPrefKeys.HttpBaseUrl); + EditorPrefs.DeleteKey(LocalUrlKey); } if (!string.IsNullOrEmpty(_savedHttpRemoteUrl)) { - EditorPrefs.SetString(EditorPrefKeys.HttpRemoteBaseUrl, _savedHttpRemoteUrl); + EditorPrefs.SetString(RemoteUrlKey, _savedHttpRemoteUrl); } else { - EditorPrefs.DeleteKey(EditorPrefKeys.HttpRemoteBaseUrl); + EditorPrefs.DeleteKey(RemoteUrlKey); } if (!string.IsNullOrEmpty(_savedHttpTransportScope)) { @@ -177,7 +180,7 @@ public void StartLocalHttpServer_AlreadyConfirmed_SkipsDialogAndLaunchesHeadless { // Arrange - local URL + HTTP enabled + confirmation already given. EditorPrefs.SetBool(EditorPrefKeys.UseHttpTransport, true); - EditorPrefs.SetString(EditorPrefKeys.HttpBaseUrl, "http://localhost:59998"); + EditorPrefs.SetString(LocalUrlKey, "http://localhost:59998"); EditorPrefs.SetBool(EditorPrefKeys.HttpServerLaunchConfirmed, true); EditorConfigurationCache.Instance.Refresh(); @@ -207,7 +210,7 @@ public void StartLocalHttpServer_Quiet_BypassesDialogAndDoesNotSetConfirmedFlag( { // Arrange - local URL + HTTP enabled + NOT yet confirmed. EditorPrefs.SetBool(EditorPrefKeys.UseHttpTransport, true); - EditorPrefs.SetString(EditorPrefKeys.HttpBaseUrl, "http://localhost:59997"); + EditorPrefs.SetString(LocalUrlKey, "http://localhost:59997"); EditorPrefs.SetBool(EditorPrefKeys.HttpServerLaunchConfirmed, false); EditorConfigurationCache.Instance.Refresh(); @@ -236,7 +239,7 @@ public void StartLocalHttpServer_LaunchesIntoPerPortLaunchLog() { // Arrange EditorPrefs.SetBool(EditorPrefKeys.UseHttpTransport, true); - EditorPrefs.SetString(EditorPrefKeys.HttpBaseUrl, "http://localhost:59996"); + EditorPrefs.SetString(LocalUrlKey, "http://localhost:59996"); EditorPrefs.SetBool(EditorPrefKeys.HttpServerLaunchConfirmed, true); EditorConfigurationCache.Instance.Refresh(); @@ -268,7 +271,7 @@ public void StartLocalHttpServer_LaunchesIntoPerPortLaunchLog() public void IsLocalUrl_Localhost_ReturnsTrue() { // Arrange - EditorPrefs.SetString(EditorPrefKeys.HttpBaseUrl, "http://localhost:8080"); + EditorPrefs.SetString(LocalUrlKey, "http://localhost:8080"); _service = new ServerManagementService(); // Act @@ -282,7 +285,7 @@ public void IsLocalUrl_Localhost_ReturnsTrue() public void IsLocalUrl_127001_ReturnsTrue() { // Arrange - EditorPrefs.SetString(EditorPrefKeys.HttpBaseUrl, "http://127.0.0.1:8080"); + EditorPrefs.SetString(LocalUrlKey, "http://127.0.0.1:8080"); _service = new ServerManagementService(); // Act @@ -296,7 +299,7 @@ public void IsLocalUrl_127001_ReturnsTrue() public void IsLocalUrl_127002_ReturnsTrue() { // Arrange - EditorPrefs.SetString(EditorPrefKeys.HttpBaseUrl, "http://127.0.0.2:8080"); + EditorPrefs.SetString(LocalUrlKey, "http://127.0.0.2:8080"); _service = new ServerManagementService(); // Act @@ -310,7 +313,7 @@ public void IsLocalUrl_127002_ReturnsTrue() public void IsLocalUrl_0000_ReturnsTrue() { // Arrange - EditorPrefs.SetString(EditorPrefKeys.HttpBaseUrl, "http://0.0.0.0:8080"); + EditorPrefs.SetString(LocalUrlKey, "http://0.0.0.0:8080"); _service = new ServerManagementService(); // Act @@ -324,7 +327,7 @@ public void IsLocalUrl_0000_ReturnsTrue() public void IsLocalUrl_IPv6Loopback_ReturnsTrue() { // Arrange - EditorPrefs.SetString(EditorPrefKeys.HttpBaseUrl, "http://[::1]:8080"); + EditorPrefs.SetString(LocalUrlKey, "http://[::1]:8080"); _service = new ServerManagementService(); // Act @@ -338,7 +341,7 @@ public void IsLocalUrl_IPv6Loopback_ReturnsTrue() public void IsLocalUrl_IPv6LoopbackLongForm_ReturnsTrue() { // Arrange - EditorPrefs.SetString(EditorPrefKeys.HttpBaseUrl, "http://[0:0:0:0:0:0:0:1]:8080"); + EditorPrefs.SetString(LocalUrlKey, "http://[0:0:0:0:0:0:0:1]:8080"); _service = new ServerManagementService(); // Act @@ -352,7 +355,7 @@ public void IsLocalUrl_IPv6LoopbackLongForm_ReturnsTrue() public void IsLocalUrl_RemoteUrl_ReturnsFalse() { // Arrange - EditorPrefs.SetString(EditorPrefKeys.HttpBaseUrl, "http://example.com:8080"); + EditorPrefs.SetString(LocalUrlKey, "http://example.com:8080"); _service = new ServerManagementService(); // Act @@ -366,7 +369,7 @@ public void IsLocalUrl_RemoteUrl_ReturnsFalse() public void IsLocalUrl_EmptyString_ReturnsFalse() { // Arrange - EditorPrefs.SetString(EditorPrefKeys.HttpBaseUrl, string.Empty); + EditorPrefs.SetString(LocalUrlKey, string.Empty); _service = new ServerManagementService(); // Act @@ -386,7 +389,7 @@ public void CanStartLocalServer_HttpDisabled_ReturnsFalse() { // Arrange EditorPrefs.SetBool(EditorPrefKeys.UseHttpTransport, false); - EditorPrefs.SetString(EditorPrefKeys.HttpBaseUrl, "http://localhost:8080"); + EditorPrefs.SetString(LocalUrlKey, "http://localhost:8080"); EditorConfigurationCache.Instance.Refresh(); _service = new ServerManagementService(); @@ -402,7 +405,7 @@ public void CanStartLocalServer_HttpEnabledLocalUrl_ReturnsTrue() { // Arrange EditorPrefs.SetBool(EditorPrefKeys.UseHttpTransport, true); - EditorPrefs.SetString(EditorPrefKeys.HttpBaseUrl, "http://localhost:8080"); + EditorPrefs.SetString(LocalUrlKey, "http://localhost:8080"); EditorConfigurationCache.Instance.Refresh(); _service = new ServerManagementService(); @@ -418,7 +421,7 @@ public void CanStartLocalServer_HttpEnabledRemoteUrl_ReturnsFalse() { // Arrange EditorPrefs.SetBool(EditorPrefKeys.UseHttpTransport, true); - EditorPrefs.SetString(EditorPrefKeys.HttpBaseUrl, "http://remote.server.com:8080"); + EditorPrefs.SetString(LocalUrlKey, "http://remote.server.com:8080"); EditorConfigurationCache.Instance.Refresh(); _service = new ServerManagementService(); @@ -435,7 +438,7 @@ public void CanStartLocalServer_HttpEnabledZeroBind_DisallowedByDefault_ReturnsF // Arrange EditorPrefs.SetBool(EditorPrefKeys.UseHttpTransport, true); EditorPrefs.SetBool(EditorPrefKeys.AllowLanHttpBind, false); - EditorPrefs.SetString(EditorPrefKeys.HttpBaseUrl, "http://0.0.0.0:8080"); + EditorPrefs.SetString(LocalUrlKey, "http://0.0.0.0:8080"); EditorConfigurationCache.Instance.Refresh(); _service = new ServerManagementService(); @@ -452,7 +455,7 @@ public void CanStartLocalServer_HttpEnabledZeroBind_WithOptIn_ReturnsTrue() // Arrange EditorPrefs.SetBool(EditorPrefKeys.UseHttpTransport, true); EditorPrefs.SetBool(EditorPrefKeys.AllowLanHttpBind, true); - EditorPrefs.SetString(EditorPrefKeys.HttpBaseUrl, "http://0.0.0.0:8080"); + EditorPrefs.SetString(LocalUrlKey, "http://0.0.0.0:8080"); EditorConfigurationCache.Instance.Refresh(); _service = new ServerManagementService(); @@ -548,7 +551,7 @@ public void TryGetLocalHttpServerCommand_HttpDisabled_ReturnsFalseWithError() { // Arrange EditorPrefs.SetBool(EditorPrefKeys.UseHttpTransport, false); - EditorPrefs.SetString(EditorPrefKeys.HttpBaseUrl, "http://localhost:8080"); + EditorPrefs.SetString(LocalUrlKey, "http://localhost:8080"); EditorConfigurationCache.Instance.Refresh(); _service = new ServerManagementService(); @@ -567,7 +570,7 @@ public void TryGetLocalHttpServerCommand_RemoteUrl_ReturnsFalseWithError() { // Arrange EditorPrefs.SetBool(EditorPrefKeys.UseHttpTransport, true); - EditorPrefs.SetString(EditorPrefKeys.HttpBaseUrl, "http://remote.server.com:8080"); + EditorPrefs.SetString(LocalUrlKey, "http://remote.server.com:8080"); EditorConfigurationCache.Instance.Refresh(); _service = new ServerManagementService(); @@ -586,7 +589,7 @@ public void TryGetLocalHttpServerCommand_LocalUrl_ReturnsCommandOrError() { // Arrange EditorPrefs.SetBool(EditorPrefKeys.UseHttpTransport, true); - EditorPrefs.SetString(EditorPrefKeys.HttpBaseUrl, "http://localhost:8080"); + EditorPrefs.SetString(LocalUrlKey, "http://localhost:8080"); _service = new ServerManagementService(); // Act @@ -615,7 +618,7 @@ public void TryGetLocalHttpServerCommand_LocalUrl_ReturnsCommandOrError() public void IsLocalHttpServerReachable_NoServer_ReturnsFalse() { // Arrange - Use a port that's unlikely to have a server running - EditorPrefs.SetString(EditorPrefKeys.HttpBaseUrl, "http://localhost:59999"); + EditorPrefs.SetString(LocalUrlKey, "http://localhost:59999"); _service = new ServerManagementService(); // Act @@ -629,7 +632,7 @@ public void IsLocalHttpServerReachable_NoServer_ReturnsFalse() public void IsLocalHttpServerReachable_RemoteUrl_ReturnsFalse() { // Arrange - EditorPrefs.SetString(EditorPrefKeys.HttpBaseUrl, "http://remote.server.com:8080"); + EditorPrefs.SetString(LocalUrlKey, "http://remote.server.com:8080"); _service = new ServerManagementService(); // Act @@ -660,7 +663,7 @@ public void IsLocalHttpServerReachable_DoesNotThrow() public void IsLocalHttpServerRunning_RemoteUrl_ReturnsFalse() { // Arrange - EditorPrefs.SetString(EditorPrefKeys.HttpBaseUrl, "http://remote.server.com:8080"); + EditorPrefs.SetString(LocalUrlKey, "http://remote.server.com:8080"); _service = new ServerManagementService(); // Act diff --git a/TestProjects/UnityMCPTests/Assets/Tests/EditMode/Services/Server/ServerCommandBuilderTests.cs b/TestProjects/UnityMCPTests/Assets/Tests/EditMode/Services/Server/ServerCommandBuilderTests.cs index 0fe428a252..0c3713fce6 100644 --- a/TestProjects/UnityMCPTests/Assets/Tests/EditMode/Services/Server/ServerCommandBuilderTests.cs +++ b/TestProjects/UnityMCPTests/Assets/Tests/EditMode/Services/Server/ServerCommandBuilderTests.cs @@ -2,6 +2,7 @@ using MCPForUnity.Editor.Services; using MCPForUnity.Editor.Services.Server; using MCPForUnity.Editor.Constants; +using MCPForUnity.Editor.Helpers; using UnityEditor; namespace MCPForUnityTests.Editor.Services.Server @@ -12,6 +13,8 @@ namespace MCPForUnityTests.Editor.Services.Server [TestFixture] public class ServerCommandBuilderTests : TransportPreferenceTestBase { + private static string LocalUrlKey => ProjectIdentityUtility.GetProjectScopedKey(EditorPrefKeys.HttpBaseUrl); + private ServerCommandBuilder _builder; private bool _savedUseHttpTransport; private string _savedHttpUrl; @@ -22,7 +25,7 @@ public void SetUp() _builder = new ServerCommandBuilder(); // Save current settings _savedUseHttpTransport = EditorPrefs.GetBool(EditorPrefKeys.UseHttpTransport, true); - _savedHttpUrl = EditorPrefs.GetString(EditorPrefKeys.HttpBaseUrl, string.Empty); + _savedHttpUrl = EditorPrefs.GetString(LocalUrlKey, string.Empty); } [TearDown] @@ -32,11 +35,11 @@ public void TearDown() EditorPrefs.SetBool(EditorPrefKeys.UseHttpTransport, _savedUseHttpTransport); if (!string.IsNullOrEmpty(_savedHttpUrl)) { - EditorPrefs.SetString(EditorPrefKeys.HttpBaseUrl, _savedHttpUrl); + EditorPrefs.SetString(LocalUrlKey, _savedHttpUrl); } else { - EditorPrefs.DeleteKey(EditorPrefKeys.HttpBaseUrl); + EditorPrefs.DeleteKey(LocalUrlKey); } // Refresh cache to reflect restored values EditorConfigurationCache.Instance.Refresh(); @@ -223,7 +226,7 @@ public void TryBuildCommand_HttpDisabled_ReturnsFalse() { // Arrange EditorPrefs.SetBool(EditorPrefKeys.UseHttpTransport, false); - EditorPrefs.SetString(EditorPrefKeys.HttpBaseUrl, "http://localhost:8080"); + EditorPrefs.SetString(LocalUrlKey, "http://localhost:8080"); EditorConfigurationCache.Instance.Refresh(); // Act @@ -243,7 +246,7 @@ public void TryBuildCommand_RemoteUrl_ReturnsFalse() { // Arrange EditorPrefs.SetBool(EditorPrefKeys.UseHttpTransport, true); - EditorPrefs.SetString(EditorPrefKeys.HttpBaseUrl, "http://remote.server.com:8080"); + EditorPrefs.SetString(LocalUrlKey, "http://remote.server.com:8080"); EditorConfigurationCache.Instance.Refresh(); // Act @@ -260,7 +263,7 @@ public void TryBuildCommand_LocalUrl_ReturnsCommandOrError() { // Arrange EditorPrefs.SetBool(EditorPrefKeys.UseHttpTransport, true); - EditorPrefs.SetString(EditorPrefKeys.HttpBaseUrl, "http://localhost:8080"); + EditorPrefs.SetString(LocalUrlKey, "http://localhost:8080"); EditorConfigurationCache.Instance.Refresh(); // Act