Skip to content

Commit a47b0fe

Browse files
Update search tools to use cursor-based pagination
Co-authored-by: SamMorrowDrums <[email protected]>
1 parent 110eba7 commit a47b0fe

File tree

1 file changed

+40
-31
lines changed

1 file changed

+40
-31
lines changed

pkg/github/search.go

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package github
22

33
import (
44
"context"
5-
"encoding/json"
65
"fmt"
76
"io"
87

@@ -53,7 +52,7 @@ func SearchRepositories(getClient GetClientFn, t translations.TranslationHelperF
5352
if err != nil {
5453
return mcp.NewToolResultError(err.Error()), nil
5554
}
56-
pagination, err := OptionalPaginationParams(request)
55+
cursorParams, err := GetCursorBasedParams(request)
5756
if err != nil {
5857
return mcp.NewToolResultError(err.Error()), nil
5958
}
@@ -65,8 +64,8 @@ func SearchRepositories(getClient GetClientFn, t translations.TranslationHelperF
6564
Sort: sort,
6665
Order: order,
6766
ListOptions: github.ListOptions{
68-
Page: pagination.Page,
69-
PerPage: pagination.PerPage,
67+
Page: cursorParams.Page,
68+
PerPage: cursorParams.PerPage + 1, // Request one extra
7069
},
7170
}
7271

@@ -92,8 +91,15 @@ func SearchRepositories(getClient GetClientFn, t translations.TranslationHelperF
9291
return mcp.NewToolResultError(fmt.Sprintf("failed to search repositories: %s", string(body))), nil
9392
}
9493

94+
// Check if there are more results
95+
hasMore := len(result.Repositories) > cursorParams.PerPage
96+
if hasMore {
97+
// Remove the extra item
98+
result.Repositories = result.Repositories[:cursorParams.PerPage]
99+
}
100+
95101
// Return either minimal or full response based on parameter
96-
var r []byte
102+
var responseData interface{}
97103
if minimalOutput {
98104
minimalRepos := make([]MinimalRepository, 0, len(result.Repositories))
99105
for _, repo := range result.Repositories {
@@ -126,24 +132,18 @@ func SearchRepositories(getClient GetClientFn, t translations.TranslationHelperF
126132
minimalRepos = append(minimalRepos, minimalRepo)
127133
}
128134

129-
minimalResult := &MinimalSearchRepositoriesResult{
135+
responseData = &MinimalSearchRepositoriesResult{
130136
TotalCount: result.GetTotal(),
131137
IncompleteResults: result.GetIncompleteResults(),
132138
Items: minimalRepos,
133139
}
134-
135-
r, err = json.Marshal(minimalResult)
136-
if err != nil {
137-
return nil, fmt.Errorf("failed to marshal minimal response: %w", err)
138-
}
139140
} else {
140-
r, err = json.Marshal(result)
141-
if err != nil {
142-
return nil, fmt.Errorf("failed to marshal full response: %w", err)
143-
}
141+
responseData = result
144142
}
145143

146-
return mcp.NewToolResultText(string(r)), nil
144+
// Create paginated response
145+
paginatedResp := NewPaginatedRESTResponse(responseData, cursorParams.Page, cursorParams.PerPage, hasMore)
146+
return MarshalPaginatedResponse(paginatedResp), nil
147147
}
148148
}
149149

@@ -181,7 +181,7 @@ func SearchCode(getClient GetClientFn, t translations.TranslationHelperFunc) (to
181181
if err != nil {
182182
return mcp.NewToolResultError(err.Error()), nil
183183
}
184-
pagination, err := OptionalPaginationParams(request)
184+
cursorParams, err := GetCursorBasedParams(request)
185185
if err != nil {
186186
return mcp.NewToolResultError(err.Error()), nil
187187
}
@@ -190,8 +190,8 @@ func SearchCode(getClient GetClientFn, t translations.TranslationHelperFunc) (to
190190
Sort: sort,
191191
Order: order,
192192
ListOptions: github.ListOptions{
193-
PerPage: pagination.PerPage,
194-
Page: pagination.Page,
193+
PerPage: cursorParams.PerPage + 1, // Request one extra
194+
Page: cursorParams.Page,
195195
},
196196
}
197197

@@ -218,12 +218,16 @@ func SearchCode(getClient GetClientFn, t translations.TranslationHelperFunc) (to
218218
return mcp.NewToolResultError(fmt.Sprintf("failed to search code: %s", string(body))), nil
219219
}
220220

221-
r, err := json.Marshal(result)
222-
if err != nil {
223-
return nil, fmt.Errorf("failed to marshal response: %w", err)
221+
// Check if there are more results
222+
hasMore := len(result.CodeResults) > cursorParams.PerPage
223+
if hasMore {
224+
// Remove the extra item
225+
result.CodeResults = result.CodeResults[:cursorParams.PerPage]
224226
}
225227

226-
return mcp.NewToolResultText(string(r)), nil
228+
// Create paginated response
229+
paginatedResp := NewPaginatedRESTResponse(result, cursorParams.Page, cursorParams.PerPage, hasMore)
230+
return MarshalPaginatedResponse(paginatedResp), nil
227231
}
228232
}
229233

@@ -241,7 +245,7 @@ func userOrOrgHandler(accountType string, getClient GetClientFn) server.ToolHand
241245
if err != nil {
242246
return mcp.NewToolResultError(err.Error()), nil
243247
}
244-
pagination, err := OptionalPaginationParams(request)
248+
cursorParams, err := GetCursorBasedParams(request)
245249
if err != nil {
246250
return mcp.NewToolResultError(err.Error()), nil
247251
}
@@ -250,8 +254,8 @@ func userOrOrgHandler(accountType string, getClient GetClientFn) server.ToolHand
250254
Sort: sort,
251255
Order: order,
252256
ListOptions: github.ListOptions{
253-
PerPage: pagination.PerPage,
254-
Page: pagination.Page,
257+
PerPage: cursorParams.PerPage + 1, // Request one extra
258+
Page: cursorParams.Page,
255259
},
256260
}
257261

@@ -282,6 +286,13 @@ func userOrOrgHandler(accountType string, getClient GetClientFn) server.ToolHand
282286
return mcp.NewToolResultError(fmt.Sprintf("failed to search %ss: %s", accountType, string(body))), nil
283287
}
284288

289+
// Check if there are more results
290+
hasMore := len(result.Users) > cursorParams.PerPage
291+
if hasMore {
292+
// Remove the extra item
293+
result.Users = result.Users[:cursorParams.PerPage]
294+
}
295+
285296
minimalUsers := make([]MinimalUser, 0, len(result.Users))
286297

287298
for _, user := range result.Users {
@@ -307,11 +318,9 @@ func userOrOrgHandler(accountType string, getClient GetClientFn) server.ToolHand
307318
minimalResp.IncompleteResults = *result.IncompleteResults
308319
}
309320

310-
r, err := json.Marshal(minimalResp)
311-
if err != nil {
312-
return nil, fmt.Errorf("failed to marshal response: %w", err)
313-
}
314-
return mcp.NewToolResultText(string(r)), nil
321+
// Create paginated response
322+
paginatedResp := NewPaginatedRESTResponse(minimalResp, cursorParams.Page, cursorParams.PerPage, hasMore)
323+
return MarshalPaginatedResponse(paginatedResp), nil
315324
}
316325
}
317326

0 commit comments

Comments
 (0)