@@ -4,7 +4,10 @@ import (
44 "context"
55 "encoding/base64"
66 "errors"
7+ "fmt"
8+ "io"
79 "mime"
10+ "net/http"
811 "path/filepath"
912 "strings"
1013
@@ -113,6 +116,7 @@ func repositoryResourceContentsHandler(client *github.Client) func(ctx context.C
113116 for _ , entry := range directoryContent {
114117 mimeType := "text/directory"
115118 if entry .GetType () == "file" {
119+ // this is system dependent, and a best guess
116120 mimeType = mime .TypeByExtension (filepath .Ext (entry .GetName ()))
117121 }
118122 resources = append (resources , mcp.TextResourceContents {
@@ -127,28 +131,59 @@ func repositoryResourceContentsHandler(client *github.Client) func(ctx context.C
127131 }
128132 if fileContent != nil {
129133 if fileContent .Content != nil {
130- decodedContent , err := fileContent .GetContent ()
134+ // download the file content from fileContent.GetDownloadURL() and use the content-type header to determine the MIME type
135+ // and return the content as a blob unless it is a text file, where you can return the content as text
136+ req , err := http .NewRequest ("GET" , fileContent .GetDownloadURL (), nil )
131137 if err != nil {
132- return nil , err
138+ return nil , fmt . Errorf ( "failed to create request: %w" , err )
133139 }
134140
135- mimeType := mime .TypeByExtension (filepath .Ext (fileContent .GetName ()))
141+ resp , err := client .Client ().Do (req )
142+ if err != nil {
143+ return nil , fmt .Errorf ("failed to send request: %w" , err )
144+ }
145+ defer func () { _ = resp .Body .Close () }()
146+
147+ if resp .StatusCode != http .StatusOK {
148+ body , err := io .ReadAll (resp .Body )
149+ if err != nil {
150+ return nil , fmt .Errorf ("failed to read response body: %w" , err )
151+ }
152+ return nil , fmt .Errorf ("failed to get security analysis settings: %s" , string (body ))
153+ }
154+
155+ mimeType := resp .Header .Get ("Content-Type" )
156+ if mimeType == "" {
157+ // backstop to the file extension if the content type is not set
158+ mime .TypeByExtension (filepath .Ext (fileContent .GetName ()))
159+ }
136160
161+ // if the content is a string, return it as text
137162 if strings .HasPrefix (mimeType , "text" ) {
163+ content , err := io .ReadAll (resp .Body )
164+ if err != nil {
165+ return nil , fmt .Errorf ("failed to parse the response body: %w" , err )
166+ }
167+
138168 return []mcp.ResourceContents {
139169 mcp.TextResourceContents {
140170 URI : request .Params .URI ,
141171 MIMEType : mimeType ,
142- Text : decodedContent ,
172+ Text : string ( content ) ,
143173 },
144174 }, nil
145175 }
176+ // otherwise, read the content and encode it as base64
177+ decodedContent , err := io .ReadAll (resp .Body )
178+ if err != nil {
179+ return nil , fmt .Errorf ("failed to parse the response body: %w" , err )
180+ }
146181
147182 return []mcp.ResourceContents {
148183 mcp.BlobResourceContents {
149184 URI : request .Params .URI ,
150185 MIMEType : mimeType ,
151- Blob : base64 .StdEncoding .EncodeToString ([] byte ( decodedContent ) ), // Encode content as Base64
186+ Blob : base64 .StdEncoding .EncodeToString (decodedContent ), // Encode content as Base64
152187 },
153188 }, nil
154189 }
0 commit comments