Skip to content

Commit b820435

Browse files
authored
Fix: path param should be optional (#1631)
* Fix: path param should be optional * Add test
1 parent ce2e4f9 commit b820435

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

pkg/github/repositories.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -670,10 +670,13 @@ func GetFileContents(t translations.TranslationHelperFunc) inventory.ServerTool
670670
if err != nil {
671671
return utils.NewToolResultError(err.Error()), nil, nil
672672
}
673-
path, err := RequiredParam[string](args, "path")
673+
674+
path, err := OptionalParam[string](args, "path")
674675
if err != nil {
675676
return utils.NewToolResultError(err.Error()), nil, nil
676677
}
678+
path = strings.TrimPrefix(path, "/")
679+
677680
ref, err := OptionalParam[string](args, "ref")
678681
if err != nil {
679682
return utils.NewToolResultError(err.Error()), nil, nil

pkg/github/repositories_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,51 @@ func Test_GetFileContents(t *testing.T) {
245245
expectError: false,
246246
expectedResult: mockDirContent,
247247
},
248+
{
249+
name: "successful text content fetch with leading slash in path",
250+
mockedClient: mock.NewMockedHTTPClient(
251+
mock.WithRequestMatchHandler(
252+
mock.GetReposGitRefByOwnerByRepoByRef,
253+
http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
254+
w.WriteHeader(http.StatusOK)
255+
_, _ = w.Write([]byte(`{"ref": "refs/heads/main", "object": {"sha": ""}}`))
256+
}),
257+
),
258+
mock.WithRequestMatchHandler(
259+
mock.GetReposContentsByOwnerByRepoByPath,
260+
http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
261+
w.WriteHeader(http.StatusOK)
262+
fileContent := &github.RepositoryContent{
263+
Name: github.Ptr("README.md"),
264+
Path: github.Ptr("README.md"),
265+
SHA: github.Ptr("abc123"),
266+
Type: github.Ptr("file"),
267+
}
268+
contentBytes, _ := json.Marshal(fileContent)
269+
_, _ = w.Write(contentBytes)
270+
}),
271+
),
272+
mock.WithRequestMatchHandler(
273+
raw.GetRawReposContentsByOwnerByRepoByBranchByPath,
274+
http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
275+
w.Header().Set("Content-Type", "text/markdown")
276+
_, _ = w.Write(mockRawContent)
277+
}),
278+
),
279+
),
280+
requestArgs: map[string]interface{}{
281+
"owner": "owner",
282+
"repo": "repo",
283+
"path": "/README.md",
284+
"ref": "refs/heads/main",
285+
},
286+
expectError: false,
287+
expectedResult: mcp.ResourceContents{
288+
URI: "repo://owner/repo/refs/heads/main/contents/README.md",
289+
Text: "# Test Repository\n\nThis is a test repository.",
290+
MIMEType: "text/markdown",
291+
},
292+
},
248293
{
249294
name: "content fetch fails",
250295
mockedClient: mock.NewMockedHTTPClient(

0 commit comments

Comments
 (0)