Skip to content

Commit 1bc7484

Browse files
use raw repo URIs for resources
1 parent db7a180 commit 1bc7484

File tree

1 file changed

+40
-5
lines changed

1 file changed

+40
-5
lines changed

pkg/github/repository_resource.go

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ package github
33
import (
44
"context"
55
"encoding/base64"
6+
"fmt"
7+
"io"
68
"mime"
9+
"net/http"
710
"path/filepath"
811
"strings"
912

@@ -97,6 +100,7 @@ func repositoryResourceContentsHandler(client *github.Client) func(ctx context.C
97100
for _, entry := range directoryContent {
98101
mimeType := "text/directory"
99102
if entry.GetType() == "file" {
103+
// this is system dependent, and a best guess
100104
mimeType = mime.TypeByExtension(filepath.Ext(entry.GetName()))
101105
}
102106
resources = append(resources, mcp.TextResourceContents{
@@ -111,28 +115,59 @@ func repositoryResourceContentsHandler(client *github.Client) func(ctx context.C
111115
}
112116
if fileContent != nil {
113117
if fileContent.Content != nil {
114-
decodedContent, err := fileContent.GetContent()
118+
// download the file content from fileContent.GetDownloadURL() and use the content-type header to determine the MIME type
119+
// and return the content as a blob unless it is a text file, where you can return the content as text
120+
req, err := http.NewRequest("GET", fileContent.GetDownloadURL(), nil)
115121
if err != nil {
116-
return nil, err
122+
return nil, fmt.Errorf("failed to create request: %w", err)
117123
}
118124

119-
mimeType := mime.TypeByExtension(filepath.Ext(fileContent.GetName()))
125+
resp, err := client.Client().Do(req)
126+
if err != nil {
127+
return nil, fmt.Errorf("failed to send request: %w", err)
128+
}
129+
defer func() { _ = resp.Body.Close() }()
130+
131+
if resp.StatusCode != http.StatusOK {
132+
body, err := io.ReadAll(resp.Body)
133+
if err != nil {
134+
return nil, fmt.Errorf("failed to read response body: %w", err)
135+
}
136+
return nil, fmt.Errorf("failed to get security analysis settings: %s", string(body))
137+
}
138+
139+
mimeType := resp.Header.Get("Content-Type")
140+
if mimeType == "" {
141+
// backstop to the file extension if the content type is not set
142+
mime.TypeByExtension(filepath.Ext(fileContent.GetName()))
143+
}
120144

145+
// if the content is a string, return it as text
121146
if strings.HasPrefix(mimeType, "text") {
147+
content, err := io.ReadAll(resp.Body)
148+
if err != nil {
149+
return nil, fmt.Errorf("failed to parse the response body: %w", err)
150+
}
151+
122152
return []mcp.ResourceContents{
123153
mcp.TextResourceContents{
124154
URI: request.Params.URI,
125155
MIMEType: mimeType,
126-
Text: decodedContent,
156+
Text: string(content),
127157
},
128158
}, nil
129159
}
160+
// otherwise, read the content and encode it as base64
161+
decodedContent, err := io.ReadAll(resp.Body)
162+
if err != nil {
163+
return nil, fmt.Errorf("failed to parse the response body: %w", err)
164+
}
130165

131166
return []mcp.ResourceContents{
132167
mcp.BlobResourceContents{
133168
URI: request.Params.URI,
134169
MIMEType: mimeType,
135-
Blob: base64.StdEncoding.EncodeToString([]byte(decodedContent)), // Encode content as Base64
170+
Blob: base64.StdEncoding.EncodeToString(decodedContent), // Encode content as Base64
136171
},
137172
}, nil
138173
}

0 commit comments

Comments
 (0)