Enhance get_pull_request_diff tool with pagination and file filtering options by kaovilai · Pull Request #627 · github/github-mcp-server · GitHub | Latest TMZ Celebrity News & Gossip | Watch TMZ Live
Skip to content

Enhance get_pull_request_diff tool with pagination and file filtering options #627

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,11 @@ export GITHUB_MCP_TOOL_ADD_ISSUE_COMMENT_DESCRIPTION="an alternative description
- `owner`: Repository owner (string, required)
- `pullNumber`: Pull request number (number, required)
- `repo`: Repository name (string, required)
- `fileList`: If true, only return the list of changed files without diffs (boolean, optional)
- `files`: List of specific files to include in the diff (e.g., ['src/main.go', 'README.md']) (array, optional)
- `pathPrefix`: Only include files with this path prefix (e.g., 'src/' or 'docs/') (string, optional)
- `page`: Page number for file-based pagination (when used with fileList) (number, optional)
- `perPage`: Number of files per page (max 100, default 30) when using fileList (number, optional)

- **get_pull_request_files** - Get pull request files
- `owner`: Repository owner (string, required)
Expand Down
22 changes: 21 additions & 1 deletion pkg/github/__toolsnaps__/get_pull_request_diff.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,33 @@
"title": "Get pull request diff",
"readOnlyHint": true
},
"description": "Get the diff of a pull request.",
"description": "Get the diff of a pull request. Supports pagination through file filtering to handle large PRs.",
"inputSchema": {
"properties": {
"fileList": {
"description": "If true, only return the list of changed files without diffs",
"type": "boolean"
},
"files": {
"description": "List of specific files to include in the diff (e.g., ['src/main.go', 'README.md'])",
"type": "array"
},
"owner": {
"description": "Repository owner",
"type": "string"
},
"page": {
"description": "Page number for file-based pagination (when used with fileList)",
"type": "number"
},
"pathPrefix": {
"description": "Only include files with this path prefix (e.g., 'src/' or 'docs/')",
"type": "string"
},
"perPage": {
"description": "Number of files per page (max 100, default 30) when using fileList",
"type": "number"
},
"pullNumber": {
"description": "Pull request number",
"type": "number"
Expand Down
139 changes: 138 additions & 1 deletion pkg/github/pullrequests.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io"
"net/http"
"strings"

"github.com/go-viper/mapstructure/v2"
"github.com/google/go-github/v72/github"
Expand Down Expand Up @@ -1574,7 +1575,7 @@ func DeletePendingPullRequestReview(getGQLClient GetGQLClientFn, t translations.

func GetPullRequestDiff(getClient GetClientFn, t translations.TranslationHelperFunc) (mcp.Tool, server.ToolHandlerFunc) {
return mcp.NewTool("get_pull_request_diff",
mcp.WithDescription(t("TOOL_GET_PULL_REQUEST_DIFF_DESCRIPTION", "Get the diff of a pull request.")),
mcp.WithDescription(t("TOOL_GET_PULL_REQUEST_DIFF_DESCRIPTION", "Get the diff of a pull request. Supports pagination through file filtering to handle large PRs.")),
mcp.WithToolAnnotation(mcp.ToolAnnotation{
Title: t("TOOL_GET_PULL_REQUEST_DIFF_USER_TITLE", "Get pull request diff"),
ReadOnlyHint: ToBoolPtr(true),
Expand All @@ -1591,12 +1592,32 @@ func GetPullRequestDiff(getClient GetClientFn, t translations.TranslationHelperF
mcp.Required(),
mcp.Description("Pull request number"),
),
mcp.WithBoolean("fileList",
mcp.Description("If true, only return the list of changed files without diffs"),
),
mcp.WithArray("files",
mcp.Description("List of specific files to include in the diff (e.g., ['src/main.go', 'README.md'])"),
),
mcp.WithString("pathPrefix",
mcp.Description("Only include files with this path prefix (e.g., 'src/' or 'docs/')"),
),
mcp.WithNumber("page",
mcp.Description("Page number for file-based pagination (when used with fileList)"),
),
mcp.WithNumber("perPage",
mcp.Description("Number of files per page (max 100, default 30) when using fileList"),
),
),
func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
var params struct {
Owner string
Repo string
PullNumber int32
FileList bool
Files []string
PathPrefix string
Page int
PerPage int
}
if err := mapstructure.Decode(request.Params.Arguments, &params); err != nil {
return mcp.NewToolResultError(err.Error()), nil
Expand All @@ -1607,6 +1628,122 @@ func GetPullRequestDiff(getClient GetClientFn, t translations.TranslationHelperF
return mcp.NewToolResultError(fmt.Sprintf("failed to get GitHub client: %v", err)), nil
}

// If fileList is requested, return paginated file list
if params.FileList {
perPage := params.PerPage
if perPage == 0 {
perPage = 30
}
if perPage > 100 {
perPage = 100
}
page := params.Page
if page == 0 {
page = 1
}

opts := &github.ListOptions{
PerPage: perPage,
Page: page,
}
files, resp, err := client.PullRequests.ListFiles(ctx, params.Owner, params.Repo, int(params.PullNumber), opts)
if err != nil {
return ghErrors.NewGitHubAPIErrorResponse(ctx,
"failed to get pull request files",
resp,
err,
), nil
}
defer func() { _ = resp.Body.Close() }()

// Filter by path prefix if specified
if params.PathPrefix != "" {
Copy link
Preview

Copilot AI Jul 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The prefix-filtering logic is duplicated in two branches (fileList and specific-files). Extract this into a shared helper function to reduce code duplication.

Copilot uses AI. Check for mistakes.

var filtered []*github.CommitFile
for _, file := range files {
if file.Filename != nil && strings.HasPrefix(*file.Filename, params.PathPrefix) {
filtered = append(filtered, file)
}
}
files = filtered
}

result := map[string]interface{}{
"files": files,
"pagination": map[string]interface{}{
"page": page,
"perPage": perPage,
"totalPages": resp.LastPage,
"hasNext": resp.NextPage > 0,
"hasPrev": resp.PrevPage > 0,
},
}

r, err := json.Marshal(result)
if err != nil {
return nil, fmt.Errorf("failed to marshal response: %w", err)
}
return mcp.NewToolResultText(string(r)), nil
}

// If specific files are requested, get individual file diffs
if len(params.Files) > 0 || params.PathPrefix != "" {
// First, get all changed files
var allFiles []*github.CommitFile
opts := &github.ListOptions{PerPage: 100}
for {
files, resp, err := client.PullRequests.ListFiles(ctx, params.Owner, params.Repo, int(params.PullNumber), opts)
if err != nil {
return ghErrors.NewGitHubAPIErrorResponse(ctx,
"failed to get pull request files",
resp,
err,
), nil
}
allFiles = append(allFiles, files...)
if resp.NextPage == 0 {
break
}
opts.Page = resp.NextPage
}

// Filter files based on criteria
var filteredFiles []*github.CommitFile
if len(params.Files) > 0 {
fileMap := make(map[string]bool)
for _, f := range params.Files {
fileMap[f] = true
}
for _, file := range allFiles {
if file.Filename != nil && fileMap[*file.Filename] {
filteredFiles = append(filteredFiles, file)
}
}
} else if params.PathPrefix != "" {
for _, file := range allFiles {
if file.Filename != nil && strings.HasPrefix(*file.Filename, params.PathPrefix) {
filteredFiles = append(filteredFiles, file)
}
}
}

// Build a partial diff from the filtered files
var diffBuilder strings.Builder
for _, file := range filteredFiles {
if file.Patch != nil {
diffBuilder.WriteString(fmt.Sprintf("diff --git a/%s b/%s\n", *file.Filename, *file.Filename))
if file.Status != nil {
diffBuilder.WriteString(fmt.Sprintf("--- a/%s\n", *file.Filename))
diffBuilder.WriteString(fmt.Sprintf("+++ b/%s\n", *file.Filename))
}
diffBuilder.WriteString(*file.Patch)
diffBuilder.WriteString("\n")
}
}

return mcp.NewToolResultText(diffBuilder.String()), nil
}

// Default behavior: get full diff (with warning for large PRs)
raw, resp, err := client.PullRequests.GetRaw(
ctx,
params.Owner,
Expand Down
Loading

TMZ Celebrity News – Breaking Stories, Videos & Gossip

Looking for the latest TMZ celebrity news? You've come to the right place. From shocking Hollywood scandals to exclusive videos, TMZ delivers it all in real time.

Whether it’s a red carpet slip-up, a viral paparazzi moment, or a legal drama involving your favorite stars, TMZ news is always first to break the story. Stay in the loop with daily updates, insider tips, and jaw-dropping photos.

🎥 Watch TMZ Live

TMZ Live brings you daily celebrity news and interviews straight from the TMZ newsroom. Don’t miss a beat—watch now and see what’s trending in Hollywood.