github/github-mcp-server · error

failed to get GitHub client: %w

Error message

failed to get GitHub client: %w

What it means

The get_commit tool handler calls deps.GetClient(ctx) before fetching the commit. RequestDeps.GetClient extracts the token from the request context, resolves REST/upload URLs from the configured API hosts, and constructs a go-github client; failures at any of those steps ('no token info in context', 'failed to get base REST URL', 'failed to get upload URL', 'failed to create REST client') are wrapped as 'failed to get GitHub client'.

Source

Thrown at pkg/github/repositories.go:96

				return utils.NewToolResultError(err.Error()), nil, nil
			}
			detail, err := parseCommitDetail(detailRaw)
			if err != nil {
				return utils.NewToolResultError(err.Error()), nil, nil
			}
			pagination, err := OptionalPaginationParams(args)
			if err != nil {
				return utils.NewToolResultError(err.Error()), nil, nil
			}

			opts := &github.ListOptions{
				Page:    pagination.Page,
				PerPage: pagination.PerPage,
			}

			client, err := deps.GetClient(ctx)
			if err != nil {
				return nil, nil, fmt.Errorf("failed to get GitHub client: %w", err)
			}
			commit, resp, err := client.Repositories.GetCommit(ctx, owner, repo, sha, opts)
			if err != nil {
				return ghErrors.NewGitHubAPIErrorResponse(ctx,
					fmt.Sprintf("failed to get commit: %s", sha),
					resp,
					err,
				), nil, nil
			}
			defer func() { _ = resp.Body.Close() }()

			if resp.StatusCode != 200 {
				body, err := io.ReadAll(resp.Body)
				if err != nil {
					return nil, nil, fmt.Errorf("failed to read response body: %w", err)
				}
				return ghErrors.NewGitHubAPIStatusErrorResponse(ctx, "failed to get commit", resp, body), nil, nil
			}

View on GitHub (pinned to 0ea1f775a7)

Solutions

  1. Check the wrapped cause first — it names token vs URL vs client construction
  2. Set GITHUB_PERSONAL_ACCESS_TOKEN (stdio) or pass the Authorization: Bearer header (HTTP) for every request
  3. Validate GITHUB_API_URL and GITHUB_UPLOAD_URL as absolute https URLs with a bare host (the API version path is appended by the client)
  4. For GHES, confirm the URLs match the documented API endpoints and contain no redundant /api/v3 suffix

Example fix

# before
export GITHUB_API_URL="github.mycompany.com/api/v3"   # no scheme → URL parse fails

# after
export GITHUB_API_URL="https://github.mycompany.com/api/v3"
export GITHUB_UPLOAD_URL="https://uploads.mycompany.com/"
Defensive patterns

Strategy: validation

Validate before calling

func validateGitHubConfig() error {
	if os.Getenv("GITHUB_PERSONAL_ACCESS_TOKEN") == "" && !headerAuthConfigured() {
		return errors.New("no GitHub token: set GITHUB_PERSONAL_ACCESS_TOKEN or send an Authorization header")
	}
	for _, k := range []string{"GITHUB_API_URL", "GITHUB_UPLOAD_URL"} {
		if v := os.Getenv(k); v != "" {
			u, err := url.Parse(v)
			if err != nil || (u.Scheme != "http" && u.Scheme != "https") || u.Host == "" {
				return fmt.Errorf("%s must be an absolute http(s) URL with host, got %q", k, v)
			}
		}
	}
	return nil
}

Try / catch

client, err := deps.GetClient(ctx)
if err != nil {
	switch {
	case strings.Contains(err.Error(), "no token info in context"):
		return fixAuthPropagation() // env/header wiring
	case strings.Contains(err.Error(), "base REST URL"), strings.Contains(err.Error(), "upload URL"):
		return fixHostConfig() // GITHUB_*_URL values
	default:
		return err
	}
}

Prevention

When it happens

Trigger: No token in the request context (missing GITHUB_PERSONAL_ACCESS_TOKEN for stdio, missing Authorization header for HTTP transports); malformed GITHUB_API_URL/GITHUB_UPLOAD_URL (no scheme, no host, embedded path); go-github WithEnterpriseURLs rejecting non-http(s) or path-bearing URLs.

Common situations: Fresh installs that set the env var name wrong (GITHUB_TOKEN vs GITHUB_PERSONAL_ACCESS_TOKEN); GHES base URLs copied with a trailing slash or /api/v3 path doubled; reverse proxies stripping Authorization; GHES loopback-host URL handling regressions (see the repo's own fix for preserving authority on loopback GHES hosts).

Related errors


AI-assisted analysis of github/github-mcp-server@0ea1f775a7 (2026-08-15). Data as JSON: /api/errors/9dd8312e3b710119. Report an issue: GitHub.