@@ -182,21 +182,15 @@ func (s *Server) Sessions() iter.Seq[*ServerSession] {
182182func (s * Server ) listPrompts (_ context.Context , _ * ServerSession , params * ListPromptsParams ) (* ListPromptsResult , error ) {
183183 s .mu .Lock ()
184184 defer s .mu .Unlock ()
185- var cursor string
186- if params != nil {
187- cursor = params .Cursor
185+ if params == nil {
186+ params = & ListPromptsParams {}
188187 }
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+ })
200194}
201195
202196func (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
213207func (s * Server ) listTools (_ context.Context , _ * ServerSession , params * ListToolsParams ) (* ListToolsResult , error ) {
214208 s .mu .Lock ()
215209 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 {}
229212 }
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+ })
231219}
232220
233221func (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
243231func (s * Server ) listResources (_ context.Context , _ * ServerSession , params * ListResourcesParams ) (* ListResourcesResult , error ) {
244232 s .mu .Lock ()
245233 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 {}
253236 }
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+ })
261243}
262244
263245func (s * Server ) readResource (ctx context.Context , ss * ServerSession , params * ReadResourceParams ) (* ReadResourceResult , error ) {
@@ -618,22 +600,25 @@ func decodeCursor(cursor string) (*pageToken, error) {
618600 return & token , nil
619601}
620602
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 ) {
625608 var seq iter.Seq [T ]
626- if cursor == "" {
609+ if params . cursorPtr () == nil || * params . cursorPtr () == "" {
627610 seq = fs .all ()
628611 } else {
629- pageToken , err := decodeCursor (cursor )
612+ pageToken , err := decodeCursor (* params . cursorPtr () )
630613 // According to the spec, invalid cursors should return Invalid params.
631614 if err != nil {
632- return nil , "" , jsonrpc2 .ErrInvalidParams
615+ var zero R
616+ return zero , jsonrpc2 .ErrInvalidParams
633617 }
634618 seq = fs .above (pageToken .LastUID )
635619 }
636620 var count int
621+ var features []T
637622 for f := range seq {
638623 count ++
639624 // 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
643628 }
644629 features = append (features , f )
645630 }
631+ setFunc (res , features )
646632 // No remaining pages.
647633 if count < pageSize + 1 {
648- return features , "" , nil
634+ return res , nil
649635 }
650- nextCursor , err = encodeCursor (fs .uniqueID (features [len (features )- 1 ]))
636+ nextCursor , err : = encodeCursor (fs .uniqueID (features [len (features )- 1 ]))
651637 if err != nil {
652- return nil , "" , err
638+ var zero R
639+ return zero , err
653640 }
654- return features , nextCursor , nil
641+ * res .nextCursorPtr () = nextCursor
642+ return res , nil
655643}
0 commit comments