Skip to content

Commit dee5927

Browse files
authored
Fix binary files retrieval (#1183)
* tighten content filtering logic * add pdf retrieval test case * add application/xml check
1 parent 94b3d72 commit dee5927

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

pkg/github/repositories.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,14 @@ func GetFileContents(getClient GetClientFn, getRawClient raw.GetRawClientFn, t t
601601
}
602602
}
603603

604-
if strings.HasPrefix(contentType, "application") || strings.HasPrefix(contentType, "text") {
604+
// Determine if content is text or binary
605+
isTextContent := strings.HasPrefix(contentType, "text/") ||
606+
contentType == "application/json" ||
607+
contentType == "application/xml" ||
608+
strings.HasSuffix(contentType, "+json") ||
609+
strings.HasSuffix(contentType, "+xml")
610+
611+
if isTextContent {
605612
result := mcp.TextResourceContents{
606613
URI: resourceURI,
607614
Text: string(body),

pkg/github/repositories_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,51 @@ func Test_GetFileContents(t *testing.T) {
157157
MIMEType: "image/png",
158158
},
159159
},
160+
{
161+
name: "successful PDF file content fetch",
162+
mockedClient: mock.NewMockedHTTPClient(
163+
mock.WithRequestMatchHandler(
164+
mock.GetReposGitRefByOwnerByRepoByRef,
165+
http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
166+
w.WriteHeader(http.StatusOK)
167+
_, _ = w.Write([]byte(`{"ref": "refs/heads/main", "object": {"sha": ""}}`))
168+
}),
169+
),
170+
mock.WithRequestMatchHandler(
171+
mock.GetReposContentsByOwnerByRepoByPath,
172+
http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
173+
w.WriteHeader(http.StatusOK)
174+
fileContent := &github.RepositoryContent{
175+
Name: github.Ptr("document.pdf"),
176+
Path: github.Ptr("document.pdf"),
177+
SHA: github.Ptr("pdf123"),
178+
Type: github.Ptr("file"),
179+
}
180+
contentBytes, _ := json.Marshal(fileContent)
181+
_, _ = w.Write(contentBytes)
182+
}),
183+
),
184+
mock.WithRequestMatchHandler(
185+
raw.GetRawReposContentsByOwnerByRepoByBranchByPath,
186+
http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
187+
w.Header().Set("Content-Type", "application/pdf")
188+
_, _ = w.Write(mockRawContent)
189+
}),
190+
),
191+
),
192+
requestArgs: map[string]interface{}{
193+
"owner": "owner",
194+
"repo": "repo",
195+
"path": "document.pdf",
196+
"ref": "refs/heads/main",
197+
},
198+
expectError: false,
199+
expectedResult: mcp.BlobResourceContents{
200+
URI: "repo://owner/repo/refs/heads/main/contents/document.pdf",
201+
Blob: base64.StdEncoding.EncodeToString(mockRawContent),
202+
MIMEType: "application/pdf",
203+
},
204+
},
160205
{
161206
name: "successful directory content fetch",
162207
mockedClient: mock.NewMockedHTTPClient(

0 commit comments

Comments
 (0)