Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 32 additions & 5 deletions MCPForUnity/Editor/Helpers/HttpEndpointUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 <see cref="ReadProjectUrlPref"/>), so editors
/// for different projects on the same machine can point at different servers.
/// </summary>
public static class HttpEndpointUtility
{
Expand Down Expand Up @@ -51,7 +54,7 @@ public static void SaveBaseUrl(string userValue)
/// </summary>
public static string GetLocalBaseUrl()
{
string stored = EditorPrefs.GetString(LocalPrefKey, DefaultLocalBaseUrl);
string stored = ReadProjectUrlPref(LocalPrefKey, DefaultLocalBaseUrl);
return NormalizeBaseUrl(stored, DefaultLocalBaseUrl, remoteScope: false);
}

Expand All @@ -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);
}

/// <summary>
Expand All @@ -70,7 +73,7 @@ public static void SaveLocalBaseUrl(string userValue)
/// </summary>
public static string GetRemoteBaseUrl()
{
string stored = EditorPrefs.GetString(RemotePrefKey, DefaultRemoteBaseUrl);
string stored = ReadProjectUrlPref(RemotePrefKey, DefaultRemoteBaseUrl);
if (string.IsNullOrWhiteSpace(stored))
{
return DefaultRemoteBaseUrl;
Expand All @@ -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);
}

/// <summary>
/// Reads a URL pref (<see cref="EditorPrefKeys.HttpBaseUrl"/> or
/// <see cref="EditorPrefKeys.HttpRemoteBaseUrl"/>) 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.
/// </summary>
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);
}

/// <summary>
/// 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.
/// </summary>
internal static void WriteProjectUrlPref(string key, string value)
{
EditorPrefs.SetString(ProjectIdentityUtility.GetProjectScopedKey(key), value);
}

/// <summary>
Expand Down
9 changes: 9 additions & 0 deletions MCPForUnity/Editor/Helpers/ProjectIdentityUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@ public static string GetProjectHash()
return _cachedProjectHash;
}

/// <summary>
/// Returns <paramref name="key"/> suffixed with the project hash, for EditorPrefs values
/// that must not be shared by different projects open on the same machine.
/// </summary>
public static string GetProjectScopedKey(string key)
{
return $"{key}_{GetProjectHash()}";
}

/// <summary>
/// Returns a human friendly project name derived from the Assets directory path,
/// or "Unknown" if the name cannot be determined.
Expand Down
23 changes: 15 additions & 8 deletions MCPForUnity/Editor/Services/EditorConfigurationCache.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using MCPForUnity.Editor.Constants;
using MCPForUnity.Editor.Helpers;
using UnityEditor;

namespace MCPForUnity.Editor.Services
Expand Down Expand Up @@ -95,13 +96,13 @@ public static EditorConfigurationCache Instance
public string GitUrlOverride => _gitUrlOverride;

/// <summary>
/// HTTP base URL for the local MCP server.
/// HTTP base URL for the local MCP server (stored per project).
/// Default: empty string
/// </summary>
public string HttpBaseUrl => _httpBaseUrl;

/// <summary>
/// HTTP base URL for the remote-hosted MCP server.
/// HTTP base URL for the remote-hosted MCP server (stored per project).
/// Default: empty string
/// </summary>
public string HttpRemoteBaseUrl => _httpRemoteBaseUrl;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -242,28 +243,34 @@ public void SetGitUrlOverride(string value)

/// <summary>
/// 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.
/// </summary>
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));
}
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

/// <summary>
/// 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.
/// </summary>
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));
}
}
Expand Down Expand Up @@ -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);
Expand Down
8 changes: 8 additions & 0 deletions MCPForUnity/Editor/Windows/EditorPrefs/EditorPrefsWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// 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.
/// </summary>
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<string, string> _savedPrefs = new Dictionary<string, string>();

private static IEnumerable<string> 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");
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading
Loading