diff --git a/pkg/github/repositories.go b/pkg/github/repositories.go index 0622f3101..dfd718f7e 100644 --- a/pkg/github/repositories.go +++ b/pkg/github/repositories.go @@ -601,7 +601,14 @@ func GetFileContents(getClient GetClientFn, getRawClient raw.GetRawClientFn, t t } } - if strings.HasPrefix(contentType, "application") || strings.HasPrefix(contentType, "text") { + // Determine if content is text or binary + isTextContent := strings.HasPrefix(contentType, "text/") || + contentType == "application/json" || + contentType == "application/xml" || + strings.HasSuffix(contentType, "+json") || + strings.HasSuffix(contentType, "+xml") + + if isTextContent { result := mcp.TextResourceContents{ URI: resourceURI, Text: string(body), diff --git a/pkg/github/repositories_test.go b/pkg/github/repositories_test.go index 11f11493c..22014148d 100644 --- a/pkg/github/repositories_test.go +++ b/pkg/github/repositories_test.go @@ -157,6 +157,51 @@ func Test_GetFileContents(t *testing.T) { MIMEType: "image/png", }, }, + { + name: "successful PDF file content fetch", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.GetReposGitRefByOwnerByRepoByRef, + http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusOK) + _, _ = w.Write([]byte(`{"ref": "refs/heads/main", "object": {"sha": ""}}`)) + }), + ), + mock.WithRequestMatchHandler( + mock.GetReposContentsByOwnerByRepoByPath, + http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusOK) + fileContent := &github.RepositoryContent{ + Name: github.Ptr("document.pdf"), + Path: github.Ptr("document.pdf"), + SHA: github.Ptr("pdf123"), + Type: github.Ptr("file"), + } + contentBytes, _ := json.Marshal(fileContent) + _, _ = w.Write(contentBytes) + }), + ), + mock.WithRequestMatchHandler( + raw.GetRawReposContentsByOwnerByRepoByBranchByPath, + http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.Header().Set("Content-Type", "application/pdf") + _, _ = w.Write(mockRawContent) + }), + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "path": "document.pdf", + "ref": "refs/heads/main", + }, + expectError: false, + expectedResult: mcp.BlobResourceContents{ + URI: "repo://owner/repo/refs/heads/main/contents/document.pdf", + Blob: base64.StdEncoding.EncodeToString(mockRawContent), + MIMEType: "application/pdf", + }, + }, { name: "successful directory content fetch", mockedClient: mock.NewMockedHTTPClient(