use localhost to ping server if server binds to 0.0.0.0 (#542)

Co-authored-by: vladimir.ivanov <terakriper@gmail.com>
main
Vladimir Ivanov 2026-01-21 09:17:52 -08:00 committed by GitHub
parent d2408f07b4
commit abd596ff4a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 10 additions and 8 deletions

View File

@ -682,16 +682,18 @@ namespace MCPForUnity.Editor.Services.Transport.Transports
throw new InvalidOperationException($"Invalid MCP base URL: {baseUrl}"); throw new InvalidOperationException($"Invalid MCP base URL: {baseUrl}");
} }
string scheme = httpUri.Scheme.Equals("https", StringComparison.OrdinalIgnoreCase) ? "wss" : "ws"; // Replace 0.0.0.0 with localhost for client connections
string builder = $"{scheme}://{httpUri.Authority}"; // 0.0.0.0 is only valid for server binding, not client connections
if (!string.IsNullOrEmpty(httpUri.AbsolutePath) && httpUri.AbsolutePath != "/") string host = httpUri.Host == "0.0.0.0" ? "localhost" : httpUri.Host;
var builder = new UriBuilder(httpUri)
{ {
builder += httpUri.AbsolutePath.TrimEnd('/'); Scheme = httpUri.Scheme.Equals("https", StringComparison.OrdinalIgnoreCase) ? "wss" : "ws",
} Host = host,
Path = httpUri.AbsolutePath.TrimEnd('/') + "/hub/plugin"
};
builder += "/hub/plugin"; return builder.Uri;
return new Uri(builder);
} }
} }
} }