Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit3a88695

Browse files
Implement cursor-based pagination for MCP tools
- Replace page/perPage parameters with single cursor parameter- Fix page size at 10 items (fetch 11 to detect more data)- Add cursor parsing and encoding functions- Update all paginated tools to return items, moreData, and cursor fields- Preserve existing metadata for search results (totalCount, etc.)This addresses issue #1362 by providing deterministic cursor valuesthat models can pass back directly without inferring page numbers.
1 parent05e0f8f commit3a88695

File tree

9 files changed

+256
-170
lines changed

9 files changed

+256
-170
lines changed

‎pkg/github/actions.go‎

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func ListWorkflows(getClient GetClientFn, t translations.TranslationHelperFunc)
6363

6464
// Set up list options
6565
opts:=&github.ListOptions{
66-
PerPage:pagination.PerPage,
66+
PerPage:CursorFetchSize,// Fetch one extra to detect if more data exists
6767
Page:pagination.Page,
6868
}
6969

@@ -73,12 +73,7 @@ func ListWorkflows(getClient GetClientFn, t translations.TranslationHelperFunc)
7373
}
7474
deferfunc() {_=resp.Body.Close() }()
7575

76-
r,err:=json.Marshal(workflows)
77-
iferr!=nil {
78-
returnnil,fmt.Errorf("failed to marshal response: %w",err)
79-
}
80-
81-
returnmcp.NewToolResultText(string(r)),nil
76+
returnCreatePaginatedResponse(workflows,pagination.Page)
8277
}
8378
}
8479

@@ -201,7 +196,7 @@ func ListWorkflowRuns(getClient GetClientFn, t translations.TranslationHelperFun
201196
Event:event,
202197
Status:status,
203198
ListOptions: github.ListOptions{
204-
PerPage:pagination.PerPage,
199+
PerPage:CursorFetchSize,// Fetch one extra to detect if more data exists
205200
Page:pagination.Page,
206201
},
207202
}
@@ -212,12 +207,7 @@ func ListWorkflowRuns(getClient GetClientFn, t translations.TranslationHelperFun
212207
}
213208
deferfunc() {_=resp.Body.Close() }()
214209

215-
r,err:=json.Marshal(workflowRuns)
216-
iferr!=nil {
217-
returnnil,fmt.Errorf("failed to marshal response: %w",err)
218-
}
219-
220-
returnmcp.NewToolResultText(string(r)),nil
210+
returnCreatePaginatedResponse(workflowRuns,pagination.Page)
221211
}
222212
}
223213

@@ -504,7 +494,7 @@ func ListWorkflowJobs(getClient GetClientFn, t translations.TranslationHelperFun
504494
opts:=&github.ListWorkflowJobsOptions{
505495
Filter:filter,
506496
ListOptions: github.ListOptions{
507-
PerPage:pagination.PerPage,
497+
PerPage:CursorFetchSize,// Fetch one extra to detect if more data exists
508498
Page:pagination.Page,
509499
},
510500
}
@@ -515,9 +505,24 @@ func ListWorkflowJobs(getClient GetClientFn, t translations.TranslationHelperFun
515505
}
516506
deferfunc() {_=resp.Body.Close() }()
517507

518-
// Add optimization tip for failed job debugging
508+
// Note: This response includes optimization_tip, so we need to handle it differently
509+
// For now, we'll wrap jobs in pagination and include tip separately
510+
// Get the jobs array from the response
511+
paginatedJobs,err:=CreatePaginatedResponse(jobs,pagination.Page)
512+
iferr!=nil {
513+
returnnil,err
514+
}
515+
516+
// Parse the paginated response to add the tip
517+
varpaginatedDataPaginatedResponse
518+
iferr:=json.Unmarshal([]byte(paginatedJobs.GetText()),&paginatedData);err!=nil {
519+
returnpaginatedJobs,nil// Return as-is if parsing fails
520+
}
521+
519522
response:=map[string]any{
520-
"jobs":jobs,
523+
"items":paginatedData.Items,
524+
"moreData":paginatedData.MoreData,
525+
"cursor":paginatedData.Cursor,
521526
"optimization_tip":"For debugging failed jobs, consider using get_job_logs with failed_only=true and run_id="+fmt.Sprintf("%d",runID)+" to get logs directly without needing to list jobs first",
522527
}
523528

@@ -1019,7 +1024,7 @@ func ListWorkflowRunArtifacts(getClient GetClientFn, t translations.TranslationH
10191024

10201025
// Set up list options
10211026
opts:=&github.ListOptions{
1022-
PerPage:pagination.PerPage,
1027+
PerPage:CursorFetchSize,// Fetch one extra to detect if more data exists
10231028
Page:pagination.Page,
10241029
}
10251030

@@ -1029,12 +1034,7 @@ func ListWorkflowRunArtifacts(getClient GetClientFn, t translations.TranslationH
10291034
}
10301035
deferfunc() {_=resp.Body.Close() }()
10311036

1032-
r,err:=json.Marshal(artifacts)
1033-
iferr!=nil {
1034-
returnnil,fmt.Errorf("failed to marshal response: %w",err)
1035-
}
1036-
1037-
returnmcp.NewToolResultText(string(r)),nil
1037+
returnCreatePaginatedResponse(artifacts,pagination.Page)
10381038
}
10391039
}
10401040

‎pkg/github/gists.go‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func ListGists(getClient GetClientFn, t translations.TranslationHelperFunc) (too
4848
opts:=&github.GistListOptions{
4949
ListOptions: github.ListOptions{
5050
Page:pagination.Page,
51-
PerPage:pagination.PerPage,
51+
PerPage:CursorFetchSize,// Fetch one extra to detect if more data exists
5252
},
5353
}
5454

@@ -80,12 +80,7 @@ func ListGists(getClient GetClientFn, t translations.TranslationHelperFunc) (too
8080
returnmcp.NewToolResultError(fmt.Sprintf("failed to list gists: %s",string(body))),nil
8181
}
8282

83-
r,err:=json.Marshal(gists)
84-
iferr!=nil {
85-
returnnil,fmt.Errorf("failed to marshal response: %w",err)
86-
}
87-
88-
returnmcp.NewToolResultText(string(r)),nil
83+
returnCreatePaginatedResponse(gists,pagination.Page)
8984
}
9085
}
9186

‎pkg/github/issues.go‎

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ func GetIssueComments(ctx context.Context, client *github.Client, owner string,
346346
opts:=&github.IssueListCommentsOptions{
347347
ListOptions: github.ListOptions{
348348
Page:pagination.Page,
349-
PerPage:pagination.PerPage,
349+
PerPage:CursorFetchSize,// Fetch one extra to detect if more data exists
350350
},
351351
}
352352

@@ -364,19 +364,14 @@ func GetIssueComments(ctx context.Context, client *github.Client, owner string,
364364
returnmcp.NewToolResultError(fmt.Sprintf("failed to get issue comments: %s",string(body))),nil
365365
}
366366

367-
r,err:=json.Marshal(comments)
368-
iferr!=nil {
369-
returnnil,fmt.Errorf("failed to marshal response: %w",err)
370-
}
371-
372-
returnmcp.NewToolResultText(string(r)),nil
367+
returnCreatePaginatedResponse(comments,pagination.Page)
373368
}
374369

375370
funcGetSubIssues(ctx context.Context,client*github.Client,ownerstring,repostring,issueNumberint,paginationPaginationParams) (*mcp.CallToolResult,error) {
376371
opts:=&github.IssueListOptions{
377372
ListOptions: github.ListOptions{
378373
Page:pagination.Page,
379-
PerPage:pagination.PerPage,
374+
PerPage:CursorFetchSize,// Fetch one extra to detect if more data exists
380375
},
381376
}
382377

@@ -399,12 +394,7 @@ func GetSubIssues(ctx context.Context, client *github.Client, owner string, repo
399394
returnmcp.NewToolResultError(fmt.Sprintf("failed to list sub-issues: %s",string(body))),nil
400395
}
401396

402-
r,err:=json.Marshal(subIssues)
403-
iferr!=nil {
404-
returnnil,fmt.Errorf("failed to marshal response: %w",err)
405-
}
406-
407-
returnmcp.NewToolResultText(string(r)),nil
397+
returnCreatePaginatedResponse(subIssues,pagination.Page)
408398
}
409399

410400
funcGetIssueLabels(ctx context.Context,client*githubv4.Client,ownerstring,repostring,issueNumberint) (*mcp.CallToolResult,error) {

‎pkg/github/notifications.go‎

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func ListNotifications(getClient GetClientFn, t translations.TranslationHelperFu
8989
Participating:filter==FilterOnlyParticipating,
9090
ListOptions: github.ListOptions{
9191
Page:paginationParams.Page,
92-
PerPage:paginationParams.PerPage,
92+
PerPage:CursorFetchSize,// Fetch one extra to detect if more data exists
9393
},
9494
}
9595

@@ -135,13 +135,7 @@ func ListNotifications(getClient GetClientFn, t translations.TranslationHelperFu
135135
returnmcp.NewToolResultError(fmt.Sprintf("failed to get notifications: %s",string(body))),nil
136136
}
137137

138-
// Marshal response to JSON
139-
r,err:=json.Marshal(notifications)
140-
iferr!=nil {
141-
returnnil,fmt.Errorf("failed to marshal response: %w",err)
142-
}
143-
144-
returnmcp.NewToolResultText(string(r)),nil
138+
returnCreatePaginatedResponse(notifications,paginationParams.Page)
145139
}
146140
}
147141

‎pkg/github/pullrequests.go‎

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ func GetPullRequestStatus(ctx context.Context, client *github.Client, owner, rep
220220

221221
funcGetPullRequestFiles(ctx context.Context,client*github.Client,owner,repostring,pullNumberint,paginationPaginationParams) (*mcp.CallToolResult,error) {
222222
opts:=&github.ListOptions{
223-
PerPage:pagination.PerPage,
223+
PerPage:CursorFetchSize,// Fetch one extra to detect if more data exists
224224
Page:pagination.Page,
225225
}
226226
files,resp,err:=client.PullRequests.ListFiles(ctx,owner,repo,pullNumber,opts)
@@ -241,18 +241,13 @@ func GetPullRequestFiles(ctx context.Context, client *github.Client, owner, repo
241241
returnmcp.NewToolResultError(fmt.Sprintf("failed to get pull request files: %s",string(body))),nil
242242
}
243243

244-
r,err:=json.Marshal(files)
245-
iferr!=nil {
246-
returnnil,fmt.Errorf("failed to marshal response: %w",err)
247-
}
248-
249-
returnmcp.NewToolResultText(string(r)),nil
244+
returnCreatePaginatedResponse(files,pagination.Page)
250245
}
251246

252247
funcGetPullRequestReviewComments(ctx context.Context,client*github.Client,owner,repostring,pullNumberint,paginationPaginationParams) (*mcp.CallToolResult,error) {
253248
opts:=&github.PullRequestListCommentsOptions{
254249
ListOptions: github.ListOptions{
255-
PerPage:pagination.PerPage,
250+
PerPage:CursorFetchSize,// Fetch one extra to detect if more data exists
256251
Page:pagination.Page,
257252
},
258253
}
@@ -275,12 +270,7 @@ func GetPullRequestReviewComments(ctx context.Context, client *github.Client, ow
275270
returnmcp.NewToolResultError(fmt.Sprintf("failed to get pull request review comments: %s",string(body))),nil
276271
}
277272

278-
r,err:=json.Marshal(comments)
279-
iferr!=nil {
280-
returnnil,fmt.Errorf("failed to marshal response: %w",err)
281-
}
282-
283-
returnmcp.NewToolResultText(string(r)),nil
273+
returnCreatePaginatedResponse(comments,pagination.Page)
284274
}
285275

286276
funcGetPullRequestReviews(ctx context.Context,client*github.Client,owner,repostring,pullNumberint) (*mcp.CallToolResult,error) {
@@ -788,7 +778,7 @@ func ListPullRequests(getClient GetClientFn, t translations.TranslationHelperFun
788778
Sort:sort,
789779
Direction:direction,
790780
ListOptions: github.ListOptions{
791-
PerPage:pagination.PerPage,
781+
PerPage:CursorFetchSize,// Fetch one extra to detect if more data exists
792782
Page:pagination.Page,
793783
},
794784
}
@@ -828,12 +818,7 @@ func ListPullRequests(getClient GetClientFn, t translations.TranslationHelperFun
828818
}
829819
}
830820

831-
r,err:=json.Marshal(prs)
832-
iferr!=nil {
833-
returnnil,fmt.Errorf("failed to marshal response: %w",err)
834-
}
835-
836-
returnmcp.NewToolResultText(string(r)),nil
821+
returnCreatePaginatedResponse(prs,pagination.Page)
837822
}
838823
}
839824

‎pkg/github/repositories.go‎

Lines changed: 11 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func GetCommit(getClient GetClientFn, t translations.TranslationHelperFunc) (too
6767

6868
opts:=&github.ListOptions{
6969
Page:pagination.Page,
70-
PerPage:pagination.PerPage,
70+
PerPage:CursorFetchSize,// Fetch one extra to detect if more data exists
7171
}
7272

7373
client,err:=getClient(ctx)
@@ -149,17 +149,12 @@ func ListCommits(getClient GetClientFn, t translations.TranslationHelperFunc) (t
149149
iferr!=nil {
150150
returnmcp.NewToolResultError(err.Error()),nil
151151
}
152-
// Set default perPage to 30 if not provided
153-
perPage:=pagination.PerPage
154-
ifperPage==0 {
155-
perPage=30
156-
}
157152
opts:=&github.CommitsListOptions{
158153
SHA:sha,
159154
Author:author,
160155
ListOptions: github.ListOptions{
161156
Page:pagination.Page,
162-
PerPage:perPage,
157+
PerPage:CursorFetchSize,// Fetch one extra to detect if more data exists
163158
},
164159
}
165160

@@ -191,12 +186,7 @@ func ListCommits(getClient GetClientFn, t translations.TranslationHelperFunc) (t
191186
minimalCommits[i]=convertToMinimalCommit(commit,false)
192187
}
193188

194-
r,err:=json.Marshal(minimalCommits)
195-
iferr!=nil {
196-
returnnil,fmt.Errorf("failed to marshal response: %w",err)
197-
}
198-
199-
returnmcp.NewToolResultText(string(r)),nil
189+
returnCreatePaginatedResponse(minimalCommits,pagination.Page)
200190
}
201191
}
202192

@@ -235,7 +225,7 @@ func ListBranches(getClient GetClientFn, t translations.TranslationHelperFunc) (
235225
opts:=&github.BranchListOptions{
236226
ListOptions: github.ListOptions{
237227
Page:pagination.Page,
238-
PerPage:pagination.PerPage,
228+
PerPage:CursorFetchSize,// Fetch one extra to detect if more data exists
239229
},
240230
}
241231

@@ -268,12 +258,7 @@ func ListBranches(getClient GetClientFn, t translations.TranslationHelperFunc) (
268258
minimalBranches=append(minimalBranches,convertToMinimalBranch(branch))
269259
}
270260

271-
r,err:=json.Marshal(minimalBranches)
272-
iferr!=nil {
273-
returnnil,fmt.Errorf("failed to marshal response: %w",err)
274-
}
275-
276-
returnmcp.NewToolResultText(string(r)),nil
261+
returnCreatePaginatedResponse(minimalBranches,pagination.Page)
277262
}
278263
}
279264

@@ -1256,7 +1241,7 @@ func ListTags(getClient GetClientFn, t translations.TranslationHelperFunc) (tool
12561241

12571242
opts:=&github.ListOptions{
12581243
Page:pagination.Page,
1259-
PerPage:pagination.PerPage,
1244+
PerPage:CursorFetchSize,// Fetch one extra to detect if more data exists
12601245
}
12611246

12621247
client,err:=getClient(ctx)
@@ -1282,12 +1267,7 @@ func ListTags(getClient GetClientFn, t translations.TranslationHelperFunc) (tool
12821267
returnmcp.NewToolResultError(fmt.Sprintf("failed to list tags: %s",string(body))),nil
12831268
}
12841269

1285-
r,err:=json.Marshal(tags)
1286-
iferr!=nil {
1287-
returnnil,fmt.Errorf("failed to marshal response: %w",err)
1288-
}
1289-
1290-
returnmcp.NewToolResultText(string(r)),nil
1270+
returnCreatePaginatedResponse(tags,pagination.Page)
12911271
}
12921272
}
12931273

@@ -1412,7 +1392,7 @@ func ListReleases(getClient GetClientFn, t translations.TranslationHelperFunc) (
14121392

14131393
opts:=&github.ListOptions{
14141394
Page:pagination.Page,
1415-
PerPage:pagination.PerPage,
1395+
PerPage:CursorFetchSize,// Fetch one extra to detect if more data exists
14161396
}
14171397

14181398
client,err:=getClient(ctx)
@@ -1434,12 +1414,7 @@ func ListReleases(getClient GetClientFn, t translations.TranslationHelperFunc) (
14341414
returnmcp.NewToolResultError(fmt.Sprintf("failed to list releases: %s",string(body))),nil
14351415
}
14361416

1437-
r,err:=json.Marshal(releases)
1438-
iferr!=nil {
1439-
returnnil,fmt.Errorf("failed to marshal response: %w",err)
1440-
}
1441-
1442-
returnmcp.NewToolResultText(string(r)),nil
1417+
returnCreatePaginatedResponse(releases,pagination.Page)
14431418
}
14441419
}
14451420

@@ -1741,7 +1716,7 @@ func ListStarredRepositories(getClient GetClientFn, t translations.TranslationHe
17411716
opts:=&github.ActivityListStarredOptions{
17421717
ListOptions: github.ListOptions{
17431718
Page:pagination.Page,
1744-
PerPage:pagination.PerPage,
1719+
PerPage:CursorFetchSize,// Fetch one extra to detect if more data exists
17451720
},
17461721
}
17471722
ifsort!="" {
@@ -1810,12 +1785,7 @@ func ListStarredRepositories(getClient GetClientFn, t translations.TranslationHe
18101785
minimalRepos=append(minimalRepos,minimalRepo)
18111786
}
18121787

1813-
r,err:=json.Marshal(minimalRepos)
1814-
iferr!=nil {
1815-
returnnil,fmt.Errorf("failed to marshal starred repositories: %w",err)
1816-
}
1817-
1818-
returnmcp.NewToolResultText(string(r)),nil
1788+
returnCreatePaginatedResponse(minimalRepos,pagination.Page)
18191789
}
18201790
}
18211791

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp