@@ -182,21 +182,15 @@ func (s *Server) Sessions() iter.Seq[*ServerSession] {
182
182
func (s * Server ) listPrompts (_ context.Context , _ * ServerSession , params * ListPromptsParams ) (* ListPromptsResult , error ) {
183
183
s .mu .Lock ()
184
184
defer s .mu .Unlock ()
185
- var cursor string
186
- if params != nil {
187
- cursor = params .Cursor
185
+ if params == nil {
186
+ params = & ListPromptsParams {}
188
187
}
189
- prompts , nextCursor , err := paginateList (s .prompts , cursor , s .opts .PageSize )
190
- if err != nil {
191
- return nil , err
192
- }
193
- res := new (ListPromptsResult )
194
- res .NextCursor = nextCursor
195
- res .Prompts = []* Prompt {} // avoid JSON null
196
- for _ , p := range prompts {
197
- res .Prompts = append (res .Prompts , p .Prompt )
198
- }
199
- return res , nil
188
+ return paginateList (s .prompts , s .opts .PageSize , params , & ListPromptsResult {}, func (res * ListPromptsResult , prompts []* ServerPrompt ) {
189
+ res .Prompts = []* Prompt {} // avoid JSON null
190
+ for _ , p := range prompts {
191
+ res .Prompts = append (res .Prompts , p .Prompt )
192
+ }
193
+ })
200
194
}
201
195
202
196
func (s * Server ) getPrompt (ctx context.Context , cc * ServerSession , params * GetPromptParams ) (* GetPromptResult , error ) {
@@ -213,21 +207,15 @@ func (s *Server) getPrompt(ctx context.Context, cc *ServerSession, params *GetPr
213
207
func (s * Server ) listTools (_ context.Context , _ * ServerSession , params * ListToolsParams ) (* ListToolsResult , error ) {
214
208
s .mu .Lock ()
215
209
defer s .mu .Unlock ()
216
- var cursor string
217
- if params != nil {
218
- cursor = params .Cursor
219
- }
220
- tools , nextCursor , err := paginateList (s .tools , cursor , s .opts .PageSize )
221
- if err != nil {
222
- return nil , err
223
- }
224
- res := new (ListToolsResult )
225
- res .NextCursor = nextCursor
226
- res .Tools = []* Tool {} // avoid JSON null
227
- for _ , t := range tools {
228
- res .Tools = append (res .Tools , t .Tool )
210
+ if params == nil {
211
+ params = & ListToolsParams {}
229
212
}
230
- return res , nil
213
+ return paginateList (s .tools , s .opts .PageSize , params , & ListToolsResult {}, func (res * ListToolsResult , tools []* ServerTool ) {
214
+ res .Tools = []* Tool {} // avoid JSON null
215
+ for _ , t := range tools {
216
+ res .Tools = append (res .Tools , t .Tool )
217
+ }
218
+ })
231
219
}
232
220
233
221
func (s * Server ) callTool (ctx context.Context , cc * ServerSession , params * CallToolParams [json.RawMessage ]) (* CallToolResult , error ) {
@@ -243,21 +231,15 @@ func (s *Server) callTool(ctx context.Context, cc *ServerSession, params *CallTo
243
231
func (s * Server ) listResources (_ context.Context , _ * ServerSession , params * ListResourcesParams ) (* ListResourcesResult , error ) {
244
232
s .mu .Lock ()
245
233
defer s .mu .Unlock ()
246
- var cursor string
247
- if params != nil {
248
- cursor = params .Cursor
249
- }
250
- resources , nextCursor , err := paginateList (s .resources , cursor , s .opts .PageSize )
251
- if err != nil {
252
- return nil , err
234
+ if params == nil {
235
+ params = & ListResourcesParams {}
253
236
}
254
- res := new (ListResourcesResult )
255
- res .NextCursor = nextCursor
256
- res .Resources = []* Resource {} // avoid JSON null
257
- for _ , r := range resources {
258
- res .Resources = append (res .Resources , r .Resource )
259
- }
260
- return res , nil
237
+ return paginateList (s .resources , s .opts .PageSize , params , & ListResourcesResult {}, func (res * ListResourcesResult , resources []* ServerResource ) {
238
+ res .Resources = []* Resource {} // avoid JSON null
239
+ for _ , r := range resources {
240
+ res .Resources = append (res .Resources , r .Resource )
241
+ }
242
+ })
261
243
}
262
244
263
245
func (s * Server ) readResource (ctx context.Context , ss * ServerSession , params * ReadResourceParams ) (* ReadResourceResult , error ) {
@@ -618,22 +600,25 @@ func decodeCursor(cursor string) (*pageToken, error) {
618
600
return & token , nil
619
601
}
620
602
621
- // paginateList returns a slice of features from the given featureSet, based on
622
- // the provided cursor and page size. It also returns a new cursor for the next
623
- // page, or an empty string if there are no more pages.
624
- func paginateList [T any ](fs * featureSet [T ], cursor string , pageSize int ) (features []T , nextCursor string , err error ) {
603
+ // paginateList is a generic helper that returns a paginated slice of items
604
+ // from a featureSet. It populates the provided result res with the items
605
+ // and sets its next cursor for subsequent pages.
606
+ // If there are no more pages, the next cursor within the result will be an empty string.
607
+ func paginateList [P listParams , R listResult [T ], T any ](fs * featureSet [T ], pageSize int , params P , res R , setFunc func (R , []T )) (R , error ) {
625
608
var seq iter.Seq [T ]
626
- if cursor == "" {
609
+ if params . cursorPtr () == nil || * params . cursorPtr () == "" {
627
610
seq = fs .all ()
628
611
} else {
629
- pageToken , err := decodeCursor (cursor )
612
+ pageToken , err := decodeCursor (* params . cursorPtr () )
630
613
// According to the spec, invalid cursors should return Invalid params.
631
614
if err != nil {
632
- return nil , "" , jsonrpc2 .ErrInvalidParams
615
+ var zero R
616
+ return zero , jsonrpc2 .ErrInvalidParams
633
617
}
634
618
seq = fs .above (pageToken .LastUID )
635
619
}
636
620
var count int
621
+ var features []T
637
622
for f := range seq {
638
623
count ++
639
624
// If we've seen pageSize + 1 elements, we've gathered enough info to determine
@@ -643,13 +628,16 @@ func paginateList[T any](fs *featureSet[T], cursor string, pageSize int) (featur
643
628
}
644
629
features = append (features , f )
645
630
}
631
+ setFunc (res , features )
646
632
// No remaining pages.
647
633
if count < pageSize + 1 {
648
- return features , "" , nil
634
+ return res , nil
649
635
}
650
- nextCursor , err = encodeCursor (fs .uniqueID (features [len (features )- 1 ]))
636
+ nextCursor , err : = encodeCursor (fs .uniqueID (features [len (features )- 1 ]))
651
637
if err != nil {
652
- return nil , "" , err
638
+ var zero R
639
+ return zero , err
653
640
}
654
- return features , nextCursor , nil
641
+ * res .nextCursorPtr () = nextCursor
642
+ return res , nil
655
643
}
0 commit comments