github/github-mcp-server · warning
failed to get file tree
Error message
failed to get file tree
What it means
Thrown by completePath when client.Git.GetTree(ctx, owner, repo, refVal, true) fails or returns nil. The call fetches the full repository tree recursively; the underlying error is discarded and replaced. The most common real cause is GitHub rejecting a recursive fetch of a very large tree (409 Conflict / 'tree is too large' style errors), but invalid refs and auth failures also land here.
Source
Thrown at pkg/github/repository_resource_completions.go:265
if refVal == "" {
refVal = "HEAD"
}
// Determine the prefix to complete (directory path or file path)
prefix := argValue
if prefix != "" && !strings.HasSuffix(prefix, "/") {
lastSlash := strings.LastIndex(prefix, "/")
if lastSlash >= 0 {
prefix = prefix[:lastSlash+1]
} else {
prefix = ""
}
}
// Get the tree for the ref (recursive)
tree, _, err := client.Git.GetTree(ctx, owner, repo, refVal, true)
if err != nil || tree == nil {
return nil, errors.New("failed to get file tree")
}
// Collect immediate children of the prefix (files and directories, no duplicates)
dirs := map[string]struct{}{}
files := map[string]struct{}{}
prefixLen := len(prefix)
for _, entry := range tree.Entries {
if !strings.HasPrefix(entry.GetPath(), prefix) {
continue
}
rel := entry.GetPath()[prefixLen:]
if rel == "" {
continue
}
// Only immediate children
slashIdx := strings.Index(rel, "/")
if slashIdx >= 0 {
// Directory: only add the directory name (with trailing slash), prefixed with full pathView on GitHub (pinned to 0ea1f775a7)
Solutions
- Verify the ref exists (git ls-remote or GET /repos/{owner}/{repo}/git/ref/{ref}) before completing paths
- For very large repos, avoid recursive tree completion: fetch the root tree non-recursively and walk only the prefix directory
- Check token scopes/permissions on the target repository
- Wrap the underlying error with %w so the true HTTP status is visible instead of the generic message
- Retry once on transient 5xx/network failures
Example fix
// before
tree, _, err := client.Git.GetTree(ctx, owner, repo, refVal, true)
if err != nil || tree == nil {
return nil, errors.New("failed to get file tree")
}
// after
tree, resp, err := client.Git.GetTree(ctx, owner, repo, refVal, true)
if err != nil {
return nil, fmt.Errorf("failed to get file tree (ref %s, HTTP %d): %w", refVal, resp.StatusCode, err)
}
if tree == nil {
return nil, errors.New("failed to get file tree: nil response")
} Defensive patterns
Strategy: validation
Validate before calling
// Validate the ref exists before requesting the recursive tree
_, resp, err := client.Git.GetRef(ctx, owner, repo, "refs/heads/"+refVal)
if err != nil || resp == nil {
// ref invalid; skip path completion
return nil
} Try / catch
// Degrade path completion on tree-fetch failure instead of failing the request
values, err := completePath(ctx, client, resolved, argValue)
if err != nil {
if strings.Contains(err.Error(), "failed to get file tree") {
return &mcp.CompleteResult{Completion: mcp.CompletionResultDetails{Values: []string{}}}, nil
}
return nil, err
} Prevention
- For very large repositories, avoid recursive tree completions; walk directories non-recursively per prefix
- Always complete a concrete ref (branch/sha/tag) rather than defaulting to HEAD so failures are reproducible
- Grant the token Contents:read on target repos
- Wrap underlying GetTree errors with %w when embedding to expose HTTP status (409 too-large, 404 bad ref, 401 auth)
When it happens
Trigger: Path completion against a repository whose tree exceeds GitHub's recursive-fetch limits (very large monorepos); refVal resolves to a nonexistent branch/SHA/tag; token lacks read access to the repo; transient network/API failure; tree response nil.
Common situations: Autocompleting paths on huge repositories (e.g. linux-scale monorepos); ref segment resolved to a value that was deleted or typo'd; fine-grained PAT without 'Contents: read' on the target repo.
Related errors
- failed to get repositories
- owner or repo not specified
- failed to get GitHub client: %w
- failed to read response body: %w
- failed to read file content: %w
AI-assisted analysis of github/github-mcp-server@0ea1f775a7 (2026-08-15).
Data as JSON: /api/errors/dc1c024061c4aafa.
Report an issue: GitHub.