github/copilot-sdk · error · InvalidOperationException

Unknown GitHub token provider registration ID

Error message

Unknown GitHub token provider registration ID '{request.RegistrationId}'.

What it means

Thrown as InvalidOperationException when the CLI requests a GitHub token using a registration ID that the client does not recognize — i.e. GetTokenAsync receives request.RegistrationId with no matching entry in the client's token-provider registry. This means the server is asking for credentials from a provider that was never registered (or has been removed) on this client instance.

Solutions

  1. Re-create the session with the GitHubTokenProvider configured so a fresh registration ID is issued to the CLI
  2. Ensure the same CopilotClient instance (and connection) that registered the provider serves the session — don't restart the client without restarting the CLI session
  3. Check that no code calls UnregisterGitHubTokenProvider (or disposes the client) while the session is still active
  4. If the CLI persisted the registration across restarts, clear/refresh CLI state or upgrade CLI

Example fix

// before
// client restarted; CLI still uses old registrationId -> error on token acquire
client = new CopilotClient(options); // old sessions orphaned
// after
client = new CopilotClient(options);
await client.CreateSessionAsync(new SessionConfig { GitHubTokenProvider = myProvider }); // re-register via new session
Defensive patterns

Strategy: try-catch

Validate before calling

// keep provider registered for the whole session lifetime
if (sessionDisposed) throw new InvalidOperationException("provider unregistered with session; recreate session first");

Try / catch

client.TokenProviderError += (_, e) => logger.LogError(e.Exception, "token acquire failed: {Msg}", e.Exception?.Message);
try { await sessionOp(); }
catch (InvalidOperationException ex) when (ex.Message.Contains("Unknown GitHub token provider registration"))
{ await RecreateSessionWithProviderAsync(); }

Prevention

When it happens

Trigger: The CLI sends a GitHubTokenAcquireRequest with a RegistrationId after the client restarted/recreated the session, after UnregisterGitHubTokenProvider ran, or if the session was created without the provider but the server still has a stale registration reference.

Common situations: Client process restarted while the CLI process (with the old registration ID) kept running; session config changed between runs (provider removed) but the CLI cached the old registration; multiple CopilotClient instances where the CLI contacts the wrong one.

Understand the failure class

Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.

Related errors


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

Appendix: source

Thrown at dotnet/src/Client.cs:2097

            throw new InvalidOperationException("Failed to register GitHub token provider.");
        }
        return registrationId;
    }

    internal void UnregisterGitHubTokenProvider(string registrationId)
        => _gitHubTokenProviders.TryRemove(registrationId, out _);

    private void ClearGitHubTokenProviders() => _gitHubTokenProviders.Clear();

    private sealed class GitHubTokenAdapter(CopilotClient client) : IGitHubTokenHandler
    {
        public async Task<GitHubTokenAcquireResult> GetTokenAsync(
            GitHubTokenAcquireRequest request,
            CancellationToken cancellationToken = default)
        {
            if (!client._gitHubTokenProviders.TryGetValue(request.RegistrationId, out var provider))
            {
                throw new InvalidOperationException(
                    $"Unknown GitHub token provider registration ID '{request.RegistrationId}'.");
            }

            var reason = request.Reason == GitHubTokenAcquireReason.Initial
                ? GitHubTokenRequestReason.Initial
                : request.Reason == GitHubTokenAcquireReason.Refresh
                    ? GitHubTokenRequestReason.Refresh
                    : throw new InvalidOperationException($"Unknown GitHub token request reason '{request.Reason}'.");
            var result = await provider(new GitHubTokenProviderArgs
            {
                Host = request.Host,
                SessionId = request.SessionId,
                Reason = reason,
            }).ConfigureAwait(false);

            if (result is { Cancelled: true })
            {
                return new GitHubTokenAcquireResultCancelled();

View on GitHub (pinned to cd8cf15dc3)