github/copilot-sdk · error · ArgumentException

GitHubToken and UseLoggedInUser cannot be combined with…

Error message

GitHubToken and UseLoggedInUser cannot be combined with RuntimeConnection.ForUri (the existing runtime manages its own auth).

What it means

When using RuntimeConnection.ForUri (an external, already-running runtime), the SDK must not supply auth because the existing runtime manages its own authentication. Combining GitHubToken or UseLoggedInUser with a UriRuntimeConnection is rejected with ArgumentException.

Solutions

  1. Remove GitHubToken and UseLoggedInUser from options when using RuntimeConnection.ForUri
  2. Use separate options objects for external-runtime vs SDK-managed modes
  3. Set GitHubToken to null (not just empty-looking values) for the URI mode

Example fix

// before
options.Connection = RuntimeConnection.ForUri(url);
options.GitHubToken = token;
// after
options.Connection = RuntimeConnection.ForUri(url); // auth handled by the external runtime
Defensive patterns

Strategy: validation

Validate before calling

if (options.Connection is UriRuntimeConnection && (!string.IsNullOrEmpty(options.GitHubToken) || options.UseLoggedInUser != null)) throw new InvalidOperationException("GitHubToken/UseLoggedInUser are not allowed with RuntimeConnection.ForUri");

Try / catch

try { client = new CopilotClient(options); } catch (ArgumentException ex) when (ex.Message.Contains("RuntimeConnection.ForUri")) { options.GitHubToken = null; options.UseLoggedInUser = null; client = new CopilotClient(options); }

Prevention

When it happens

Trigger: new CopilotClient(options) where options.Connection is a UriRuntimeConnection AND options.GitHubToken is non-empty OR options.UseLoggedInUser is set.

Common situations: Sharing one options-builder for both in-process and external-runtime modes; leftover GitHubToken from a previous auth setup; enabling UseLoggedInUser for local dev then pointing at an external runtime.

Related errors


AI-assisted analysis of github/copilot-sdk@cd8cf15dc3 (2026-09-09). Data as JSON: /api/errors/481867ec2de2da68. Report an issue: GitHub.

Appendix: source

Thrown at dotnet/src/Client.cs:177

            case TcpRuntimeConnection tcp:
                if (tcp.ConnectionToken is { Length: 0 })
                {
                    throw new ArgumentException("ConnectionToken must be a non-empty string or null.", nameof(options));
                }
                // Auto-generate a connection token when the SDK spawns the runtime over TCP
                // so the loopback listener is safe by default.
                tcp.ConnectionToken ??= Guid.NewGuid().ToString();
                break;

            case UriRuntimeConnection uri:
                if (string.IsNullOrEmpty(uri.Url))
                {
                    throw new ArgumentException("UriRuntimeConnection.Url must be a non-empty string.", nameof(options));
                }
                if (!string.IsNullOrEmpty(_options.GitHubToken) || _options.UseLoggedInUser != null)
                {
                    throw new ArgumentException("GitHubToken and UseLoggedInUser cannot be combined with RuntimeConnection.ForUri (the existing runtime manages its own auth).", nameof(options));
                }
                var parsed = ParseRuntimeUrl(uri.Url);
                _optionsHost = parsed.Host.Trim('[', ']');
                _optionsPort = parsed.Port;
                break;

            default:
                throw new ArgumentException($"Unsupported RuntimeConnection type: {_connection.GetType().Name}", nameof(options));
        }

        ValidateEnvironmentOptions(_options, _connection);

        _logger = _options.Logger ?? NullLogger.Instance;
        _onListModels = _options.OnListModels;

        _clientGlobalApis = BuildClientGlobalApis();

        // Empty mode: validate at construction time that the app supplied a

View on GitHub (pinned to cd8cf15dc3)