github/github-mcp-server · warning

failed to marshal response: %w

Error message

failed to marshal response: %w

What it means

The get_tree handler filters entries from a successful git-trees API response and marshals a struct of strings, *tree.SHA, *tree.Truncated (deref'd bools), the entries slice, ints, and the recursive flag. All are JSON-safe primitives/pointers; json.Marshal can only fail if the entries slice or the struct gains unsupported types (func/channel/NaN) or a cycle. In practice this is a defensive branch guarding future edits.

Source

Thrown at pkg/github/git.go:172

				if entry.Size != nil {
					treeEntries[i].Size = entry.Size
				}
			}

			response := TreeResponse{
				SHA:       *tree.SHA,
				Truncated: *tree.Truncated,
				Tree:      treeEntries,
				TreeSHA:   treeSHA,
				Owner:     owner,
				Repo:      repo,
				Recursive: recursive,
				Count:     len(filteredEntries),
			}

			r, err := json.Marshal(response)
			if err != nil {
				return nil, nil, fmt.Errorf("failed to marshal response: %w", err)
			}

			result := utils.NewToolResultText(string(r))
			// The repository tree exposes committed file structure; in public
			// repos anyone can land content via a PR (untrusted), in private
			// repos only collaborators can (trusted). Confidentiality follows
			// repo visibility.
			result = attachRepoVisibilityIFCLabel(ctx, deps, client, owner, repo, result, ifc.LabelCommitContents)
			return result, nil, nil
		},
	)
}

View on GitHub (pinned to 0ea1f775a7)

Solutions

  1. Project each entry into a local struct of string/int fields, as currently done, rather than embedding go-github types
  2. Add a marshal test with a realistic tree fixture to CI
  3. If hit, inspect treeEntries' element type — the bug is local projection, and retrying changes nothing
  4. Keep pointer dereferences guarded (nil *tree.SHA would panic, not marshal-fail; separate concern) so this branch stays the only failure mode
Defensive patterns

Strategy: try-catch

Try / catch

r, err := json.Marshal(response)
if err != nil {
	return nil, nil, fmt.Errorf("failed to marshal response: %w", err)
}

Prevention

When it happens

Trigger: A refactor appending raw go-github objects with func fields into treeEntries; NaN/Inf float additions (e.g. size as float); self-referential structures if entries ever link parents. Not reachable via any repository tree GitHub can return with the current shape.

Common situations: Forks extending tree entries with mode/type objects as non-string types; version upgrades of go-github changing Tree field types from strings to typed structs; copy-paste of the whole *github.TreeEntry into the response.

Related errors


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