Skip to content

Commit 2e797d8

Browse files
committed
feat: add line range support to get_file_contents tool
1 parent 57c8c75 commit 2e797d8

1 file changed

Lines changed: 50 additions & 1 deletion

File tree

pkg/github/repositories.go

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import (
66
"fmt"
77
"io"
88
"net/http"
9+
"strings"
10+
11+
"encoding/base64"
912

1013
"github.com/github/github-mcp-server/pkg/translations"
1114
"github.com/google/go-github/v69/github"
@@ -407,6 +410,12 @@ func GetFileContents(getClient GetClientFn, t translations.TranslationHelperFunc
407410
mcp.WithString("branch",
408411
mcp.Description("Branch to get contents from"),
409412
),
413+
mcp.WithNumber("begin",
414+
mcp.Description("Begin line number (1-indexed, optional)"),
415+
),
416+
mcp.WithNumber("end",
417+
mcp.Description("End line number (1-indexed, optional)"),
418+
),
410419
),
411420
func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
412421
owner, err := requiredParam[string](request, "owner")
@@ -425,6 +434,14 @@ func GetFileContents(getClient GetClientFn, t translations.TranslationHelperFunc
425434
if err != nil {
426435
return mcp.NewToolResultError(err.Error()), nil
427436
}
437+
begin, err := OptionalIntParam(request, "begin")
438+
if err != nil {
439+
return mcp.NewToolResultError(err.Error()), nil
440+
}
441+
end, err := OptionalIntParam(request, "end")
442+
if err != nil {
443+
return mcp.NewToolResultError(err.Error()), nil
444+
}
428445

429446
client, err := getClient(ctx)
430447
if err != nil {
@@ -447,7 +464,39 @@ func GetFileContents(getClient GetClientFn, t translations.TranslationHelperFunc
447464

448465
var result interface{}
449466
if fileContent != nil {
450-
result = fileContent
467+
if fileContent.Content != nil && (begin > 0 || end > 0) {
468+
decoded, err := fileContent.GetContent()
469+
if err != nil {
470+
return nil, fmt.Errorf("failed to decode file content: %w", err)
471+
}
472+
lines := strings.Split(decoded, "\n")
473+
totalLines := len(lines)
474+
// Adjust indices for 1-based input
475+
startIdx := begin - 1
476+
if startIdx < 0 {
477+
startIdx = 0
478+
}
479+
endIdx := end
480+
if endIdx <= 0 || endIdx > totalLines {
481+
endIdx = totalLines
482+
}
483+
if startIdx >= totalLines {
484+
startIdx = totalLines - 1
485+
}
486+
if startIdx < 0 {
487+
startIdx = 0
488+
}
489+
if endIdx < startIdx {
490+
endIdx = startIdx
491+
}
492+
ranged := lines[startIdx:endIdx]
493+
joined := strings.Join(ranged, "\n")
494+
result = &github.RepositoryContent{
495+
Content: github.Ptr(base64.StdEncoding.EncodeToString([]byte(joined))),
496+
}
497+
} else {
498+
result = fileContent
499+
}
451500
} else {
452501
result = dirContent
453502
}

0 commit comments

Comments
 (0)