From abd596ff4a66ebd989562ab82e3e59a87c3c83fe Mon Sep 17 00:00:00 2001 From: Vladimir Ivanov Date: Wed, 21 Jan 2026 09:17:52 -0800 Subject: [PATCH] use localhost to ping server if server binds to 0.0.0.0 (#542) Co-authored-by: vladimir.ivanov --- .../Transports/WebSocketTransportClient.cs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/MCPForUnity/Editor/Services/Transport/Transports/WebSocketTransportClient.cs b/MCPForUnity/Editor/Services/Transport/Transports/WebSocketTransportClient.cs index d9ee1c9..4588e4a 100644 --- a/MCPForUnity/Editor/Services/Transport/Transports/WebSocketTransportClient.cs +++ b/MCPForUnity/Editor/Services/Transport/Transports/WebSocketTransportClient.cs @@ -682,16 +682,18 @@ namespace MCPForUnity.Editor.Services.Transport.Transports throw new InvalidOperationException($"Invalid MCP base URL: {baseUrl}"); } - string scheme = httpUri.Scheme.Equals("https", StringComparison.OrdinalIgnoreCase) ? "wss" : "ws"; - string builder = $"{scheme}://{httpUri.Authority}"; - if (!string.IsNullOrEmpty(httpUri.AbsolutePath) && httpUri.AbsolutePath != "/") + // Replace 0.0.0.0 with localhost for client connections + // 0.0.0.0 is only valid for server binding, not client connections + 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 new Uri(builder); + return builder.Uri; } } }