Skip to content

Commit 5389b01

Browse files
author
vladimir.ivanov
committed
use localhost to ping server if server binds to 0.0.0.0
1 parent 4c9beaf commit 5389b01

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

MCPForUnity/Editor/Services/Transport/Transports/WebSocketTransportClient.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -682,16 +682,18 @@ private static Uri BuildWebSocketUri(string baseUrl)
682682
throw new InvalidOperationException($"Invalid MCP base URL: {baseUrl}");
683683
}
684684

685-
string scheme = httpUri.Scheme.Equals("https", StringComparison.OrdinalIgnoreCase) ? "wss" : "ws";
686-
string builder = $"{scheme}://{httpUri.Authority}";
687-
if (!string.IsNullOrEmpty(httpUri.AbsolutePath) && httpUri.AbsolutePath != "/")
688-
{
689-
builder += httpUri.AbsolutePath.TrimEnd('/');
690-
}
685+
// Replace 0.0.0.0 with localhost for client connections
686+
// 0.0.0.0 is only valid for server binding, not client connections
687+
string host = httpUri.Host == "0.0.0.0" ? "localhost" : httpUri.Host;
691688

692-
builder += "/hub/plugin";
689+
var builder = new UriBuilder(httpUri)
690+
{
691+
Scheme = httpUri.Scheme.Equals("https", StringComparison.OrdinalIgnoreCase) ? "wss" : "ws",
692+
Host = host,
693+
Path = (httpUri.AbsolutePath.TrimEnd('/') + "/hub/plugin").TrimStart('/')
694+
};
693695

694-
return new Uri(builder);
696+
return builder.Uri;
695697
}
696698
}
697699
}

0 commit comments

Comments
 (0)