@@ -4,7 +4,10 @@ import (
4
4
"context"
5
5
"encoding/base64"
6
6
"errors"
7
+ "fmt"
8
+ "io"
7
9
"mime"
10
+ "net/http"
8
11
"path/filepath"
9
12
"strings"
10
13
@@ -113,6 +116,7 @@ func repositoryResourceContentsHandler(client *github.Client) func(ctx context.C
113
116
for _ , entry := range directoryContent {
114
117
mimeType := "text/directory"
115
118
if entry .GetType () == "file" {
119
+ // this is system dependent, and a best guess
116
120
mimeType = mime .TypeByExtension (filepath .Ext (entry .GetName ()))
117
121
}
118
122
resources = append (resources , mcp.TextResourceContents {
@@ -127,28 +131,59 @@ func repositoryResourceContentsHandler(client *github.Client) func(ctx context.C
127
131
}
128
132
if fileContent != nil {
129
133
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 )
131
137
if err != nil {
132
- return nil , err
138
+ return nil , fmt . Errorf ( "failed to create request: %w" , err )
133
139
}
134
140
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
+ }
136
160
161
+ // if the content is a string, return it as text
137
162
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
+
138
168
return []mcp.ResourceContents {
139
169
mcp.TextResourceContents {
140
170
URI : request .Params .URI ,
141
171
MIMEType : mimeType ,
142
- Text : decodedContent ,
172
+ Text : string ( content ) ,
143
173
},
144
174
}, nil
145
175
}
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
+ }
146
181
147
182
return []mcp.ResourceContents {
148
183
mcp.BlobResourceContents {
149
184
URI : request .Params .URI ,
150
185
MIMEType : mimeType ,
151
- Blob : base64 .StdEncoding .EncodeToString ([] byte ( decodedContent ) ), // Encode content as Base64
186
+ Blob : base64 .StdEncoding .EncodeToString (decodedContent ), // Encode content as Base64
152
187
},
153
188
}, nil
154
189
}
0 commit comments