using System; using MCPForUnity.Editor.Constants; using MCPForUnity.Editor.Models; using MCPForUnity.Editor.Services; using UnityEditor; namespace MCPForUnity.Editor.Helpers { /// /// Helper methods for managing HTTP endpoint URLs used by the MCP bridge. /// Ensures the stored value is always the base URL (without trailing path), /// and provides convenience accessors for specific endpoints. /// /// HTTP Local and HTTP Remote use separate EditorPrefs keys so that switching /// between scopes does not overwrite the other scope's URL. /// public static class HttpEndpointUtility { private const string LocalPrefKey = EditorPrefKeys.HttpBaseUrl; private const string RemotePrefKey = EditorPrefKeys.HttpRemoteBaseUrl; private const string DefaultLocalBaseUrl = "http://localhost:8080"; private const string DefaultRemoteBaseUrl = ""; /// /// Returns the normalized base URL for the currently active HTTP scope. /// If the scope is "remote", returns the remote URL; otherwise returns the local URL. /// public static string GetBaseUrl() { return IsRemoteScope() ? GetRemoteBaseUrl() : GetLocalBaseUrl(); } /// /// Saves a user-provided URL to the currently active HTTP scope's pref. /// public static void SaveBaseUrl(string userValue) { if (IsRemoteScope()) { SaveRemoteBaseUrl(userValue); } else { SaveLocalBaseUrl(userValue); } } /// /// Returns the normalized local HTTP base URL (always reads local pref). /// public static string GetLocalBaseUrl() { string stored = EditorPrefs.GetString(LocalPrefKey, DefaultLocalBaseUrl); return NormalizeBaseUrl(stored, DefaultLocalBaseUrl); } /// /// Saves a user-provided URL to the local HTTP pref. /// public static void SaveLocalBaseUrl(string userValue) { string normalized = NormalizeBaseUrl(userValue, DefaultLocalBaseUrl); EditorPrefs.SetString(LocalPrefKey, normalized); } /// /// Returns the normalized remote HTTP base URL (always reads remote pref). /// Returns empty string if no remote URL is configured. /// public static string GetRemoteBaseUrl() { string stored = EditorPrefs.GetString(RemotePrefKey, DefaultRemoteBaseUrl); if (string.IsNullOrWhiteSpace(stored)) { return DefaultRemoteBaseUrl; } return NormalizeBaseUrl(stored, DefaultRemoteBaseUrl); } /// /// Saves a user-provided URL to the remote HTTP pref. /// public static void SaveRemoteBaseUrl(string userValue) { if (string.IsNullOrWhiteSpace(userValue)) { EditorPrefs.SetString(RemotePrefKey, DefaultRemoteBaseUrl); return; } string normalized = NormalizeBaseUrl(userValue, DefaultRemoteBaseUrl); EditorPrefs.SetString(RemotePrefKey, normalized); } /// /// Builds the JSON-RPC endpoint for the currently active scope (base + /mcp). /// public static string GetMcpRpcUrl() { return AppendPathSegment(GetBaseUrl(), "mcp"); } /// /// Builds the local JSON-RPC endpoint (local base + /mcp). /// public static string GetLocalMcpRpcUrl() { return AppendPathSegment(GetLocalBaseUrl(), "mcp"); } /// /// Builds the remote JSON-RPC endpoint (remote base + /mcp). /// Returns empty string if no remote URL is configured. /// public static string GetRemoteMcpRpcUrl() { string remoteBase = GetRemoteBaseUrl(); return string.IsNullOrEmpty(remoteBase) ? string.Empty : AppendPathSegment(remoteBase, "mcp"); } /// /// Builds the endpoint used when POSTing custom-tool registration payloads. /// public static string GetRegisterToolsUrl() { return AppendPathSegment(GetBaseUrl(), "register-tools"); } /// /// Returns true if the active HTTP transport scope is "remote". /// public static bool IsRemoteScope() { string scope = EditorConfigurationCache.Instance.HttpTransportScope; return string.Equals(scope, "remote", StringComparison.OrdinalIgnoreCase); } /// /// Returns the that matches the current server-side /// transport selection (Stdio, Http, or HttpRemote). /// Centralises the 3-way determination so callers avoid duplicated logic. /// public static ConfiguredTransport GetCurrentServerTransport() { bool useHttp = EditorConfigurationCache.Instance.UseHttpTransport; if (!useHttp) return ConfiguredTransport.Stdio; return IsRemoteScope() ? ConfiguredTransport.HttpRemote : ConfiguredTransport.Http; } /// /// Normalizes a URL so that we consistently store just the base (no trailing slash/path). /// private static string NormalizeBaseUrl(string value, string defaultUrl) { if (string.IsNullOrWhiteSpace(value)) { return defaultUrl; } string trimmed = value.Trim(); // Ensure scheme exists; default to http:// if user omitted it. if (!trimmed.Contains("://")) { trimmed = $"http://{trimmed}"; } // Remove trailing slash segments. trimmed = trimmed.TrimEnd('/'); // Strip trailing "/mcp" (case-insensitive) if provided. if (trimmed.EndsWith("/mcp", StringComparison.OrdinalIgnoreCase)) { trimmed = trimmed[..^4]; } return trimmed; } private static string AppendPathSegment(string baseUrl, string segment) { return $"{baseUrl.TrimEnd('/')}/{segment}"; } } }