Skip to content

Commit c0ee1a9

Browse files
committed
fix: handle empty files in get_file_contents
1. Empty (0-byte) files caused an unhandled error because the GitHub API returns null content with base64 encoding for them; GetContent() fails with "malformed response: base64 encoding of null content". Return empty text/plain content directly, bypassing decoding entirely.
1 parent 851030c commit c0ee1a9

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

pkg/github/repositories.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,20 @@ func GetFileContents(t translations.TranslationHelperFunc) inventory.ServerTool
731731
successNote = fmt.Sprintf(" Note: the provided ref '%s' does not exist, default branch '%s' was used instead.", originalRef, rawOpts.Ref)
732732
}
733733

734+
// Empty files (0 bytes) have no content to decode; return
735+
// them directly as empty text to avoid errors from
736+
// GetContent when the API returns null content with a
737+
// base64 encoding field, and to avoid DetectContentType
738+
// misclassifying them as binary.
739+
if fileSize == 0 {
740+
result := &mcp.ResourceContents{
741+
URI: resourceURI,
742+
Text: "",
743+
MIMEType: "text/plain",
744+
}
745+
return utils.NewToolResultResource(fmt.Sprintf("successfully downloaded empty file (SHA: %s)%s", fileSHA, successNote), result), nil, nil
746+
}
747+
734748
// For files >= 1MB, return a ResourceLink instead of content
735749
const maxContentSize = 1024 * 1024 // 1MB
736750
if fileSize >= maxContentSize {

0 commit comments

Comments
 (0)