{"record":{"id":"e096027beee451a5","repo":"github/copilot-sdk","slug":"cannot-connect-because-tcp-host-or-port-are-not-av","errorCode":null,"errorMessage":"Cannot connect because TCP host or port are not available","messagePattern":"Cannot connect because TCP host or port are not available","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"dotnet/src/Client.cs","lineNumber":2652,"sourceCode":"            {\n                inputStream = ffiHost.ReceiveStream;\n                outputStream = ffiHost.SendStream;\n            }\n            else if (_connection is StdioRuntimeConnection)\n            {\n                if (cliProcess == null)\n                {\n                    throw new InvalidOperationException(\"Runtime process not started\");\n                }\n\n                inputStream = cliProcess.StandardOutput.BaseStream;\n                outputStream = cliProcess.StandardInput.BaseStream;\n            }\n            else\n            {\n                if (tcpHost is null || tcpPort is null)\n                {\n                    throw new InvalidOperationException(\"Cannot connect because TCP host or port are not available\");\n                }\n\n                var socket = new Socket(SocketType.Stream, ProtocolType.Tcp);\n                try\n                {\n                    var tcpConnectTimestamp = Stopwatch.GetTimestamp();\n                    LogConnectingToCliServer(_logger, tcpHost, tcpPort.Value);\n                    await socket.ConnectAsync(tcpHost, tcpPort.Value, cancellationToken);\n                    LoggingHelpers.LogTiming(_logger, LogLevel.Debug, null,\n                        \"CopilotClient.ConnectToServerAsync TCP connect complete. Elapsed={Elapsed}, Host={Host}, Port={Port}\",\n                        tcpConnectTimestamp,\n                        tcpHost,\n                        tcpPort.Value);\n                }\n                catch\n                {\n                    socket.Dispose();\n                    throw;","sourceCodeStart":2634,"sourceCodeEnd":2670,"githubUrl":"https://github.com/github/copilot-sdk/blob/cd8cf15dc3f9e762615790aaed0a771a0f392755/dotnet/src/Client.cs#L2634-L2670","documentation":"When the connection mode is TCP, the client connects a Socket to tcpHost:tcpPort. This InvalidOperationException is thrown before connecting if either host or port is null. It guards against attempting a TCP connect with incomplete connection parameters.","triggerScenarios":"Starting the client in TCP connection mode while tcpHost or tcpPort was not supplied/resolved (e.g. constructor options omitted or the CLI failed to report its listening endpoint).","commonSituations":"Constructing CopilotClient for an externally-managed CLI server but forgetting to pass host/port; environment variable or config that carries the port not set; CLI started without the flag that makes it listen on TCP.","solutions":["Pass both host and port to the client constructor / connection options when using TCP mode.","Verify the CLI was started with the TCP-listening flag and check its logs for the advertised port.","Check environment variables (e.g. the port the CLI prints) are propagated to your process.","Fall back to stdio mode if you do not manage a TCP server yourself."],"exampleFix":"// before: TCP mode without a port\nvar client = new CopilotClient(new CopilotClientOptions { Host = \"127.0.0.1\" });\n\n// after: supply both, or use stdio\nvar client = new CopilotClient(new CopilotClientOptions { Host = \"127.0.0.1\", Port = 4242 });\n// or: new CopilotClient(); // defaults to stdio","handlingStrategy":"validation","validationCode":"if (string.IsNullOrEmpty(options.Host) || options.Port is null or <= 0)\n    throw new ArgumentException(\"TCP host and port are required for TCP mode\");","typeGuard":null,"tryCatchPattern":"try { await client.StartAsync(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"TCP host or port\"))\n{ /* fall back to stdio or reconfigure */ }","preventionTips":["Read host/port from env/config and assert them before constructing the client","Start the CLI with the TCP-listen flag and parse its advertised port","Default to stdio mode unless you explicitly manage a TCP server"],"tags":["tcp","configuration","connection","dotnet"],"backgroundTag":"missing-required-config-field","analyzedSha":"cd8cf15dc3f9e762615790aaed0a771a0f392755","analyzedAt":"2026-09-09T18:32:31.973Z","contentChangedAt":"2026-09-09T18:32:31.973Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}