@@ -536,31 +536,56 @@ func callParamMethod[P any](c *Connection, rawParams interface{}, fn func(P) (in
536536 return marshalToResponse (result )
537537}
538538
539- func (c * Connection ) listTools () (* Response , error ) {
540- if err := c .requireSession (); err != nil {
541- return nil , err
542- }
543- logConn .Printf ("listTools: requesting tool list from backend serverID=%s" , c .serverID )
544- // Fetch first page to determine initial capacity
545- first , err := c .getSDKSession ().ListTools (c .ctx , & sdk.ListToolsParams {})
539+ // paginatedPage holds a single page of results from a paginated SDK list call.
540+ type paginatedPage [T any ] struct {
541+ Items []T
542+ NextCursor string
543+ }
544+
545+ // paginateAll collects all items across paginated SDK list calls.
546+ func paginateAll [T any ](
547+ serverID string ,
548+ itemKind string ,
549+ fetch func (cursor string ) (paginatedPage [T ], error ),
550+ ) ([]T , error ) {
551+ first , err := fetch ("" )
546552 if err != nil {
547553 return nil , err
548554 }
549- allTools := make ([]* sdk.Tool , len (first .Tools ), max (len (first .Tools ), 1 ))
550- copy (allTools , first .Tools )
551- logConn .Printf ("listTools: received page of %d tools from serverID=%s" , len (first .Tools ), c .serverID )
555+ all := make ([]T , len (first .Items ), max (len (first .Items ), 1 ))
556+ copy (all , first .Items )
557+ logConn .Printf ("list%s: received page of %d %s from serverID=%s" , itemKind , len (first .Items ), itemKind , serverID )
558+
552559 cursor := first .NextCursor
553560 for cursor != "" {
554- result , err := c . getSDKSession (). ListTools ( c . ctx , & sdk. ListToolsParams { Cursor : cursor } )
561+ page , err := fetch ( cursor )
555562 if err != nil {
556563 return nil , err
557564 }
558- allTools = append (allTools , result . Tools ... )
559- logConn .Printf ("listTools : received page of %d tools (total so far: %d) from serverID=%s" , len (result . Tools ), len (allTools ), c . serverID )
560- cursor = result .NextCursor
565+ all = append (all , page . Items ... )
566+ logConn .Printf ("list%s : received page of %d %s (total so far: %d) from serverID=%s" , itemKind , len (page . Items ), itemKind , len (all ), serverID )
567+ cursor = page .NextCursor
561568 }
562- logConn .Printf ("listTools: received %d tools total from serverID=%s" , len (allTools ), c .serverID )
563- return marshalToResponse (& sdk.ListToolsResult {Tools : allTools })
569+ logConn .Printf ("list%s: received %d %s total from serverID=%s" , itemKind , len (all ), itemKind , serverID )
570+ return all , nil
571+ }
572+
573+ func (c * Connection ) listTools () (* Response , error ) {
574+ if err := c .requireSession (); err != nil {
575+ return nil , err
576+ }
577+ logConn .Printf ("listTools: requesting tool list from backend serverID=%s" , c .serverID )
578+ tools , err := paginateAll (c .serverID , "tools" , func (cursor string ) (paginatedPage [* sdk.Tool ], error ) {
579+ result , err := c .getSDKSession ().ListTools (c .ctx , & sdk.ListToolsParams {Cursor : cursor })
580+ if err != nil {
581+ return paginatedPage [* sdk.Tool ]{}, err
582+ }
583+ return paginatedPage [* sdk.Tool ]{Items : result .Tools , NextCursor : result .NextCursor }, nil
584+ })
585+ if err != nil {
586+ return nil , err
587+ }
588+ return marshalToResponse (& sdk.ListToolsResult {Tools : tools })
564589}
565590
566591func (c * Connection ) callTool (params interface {}) (* Response , error ) {
@@ -583,26 +608,17 @@ func (c *Connection) listResources() (*Response, error) {
583608 return nil , err
584609 }
585610 logConn .Printf ("listResources: requesting resource list from backend serverID=%s" , c .serverID )
586- // Fetch first page to determine initial capacity
587- first , err := c .getSDKSession ().ListResources (c .ctx , & sdk.ListResourcesParams {})
588- if err != nil {
589- return nil , err
590- }
591- allResources := make ([]* sdk.Resource , len (first .Resources ), max (len (first .Resources ), 1 ))
592- copy (allResources , first .Resources )
593- logConn .Printf ("listResources: received page of %d resources from serverID=%s" , len (first .Resources ), c .serverID )
594- cursor := first .NextCursor
595- for cursor != "" {
611+ resources , err := paginateAll (c .serverID , "resources" , func (cursor string ) (paginatedPage [* sdk.Resource ], error ) {
596612 result , err := c .getSDKSession ().ListResources (c .ctx , & sdk.ListResourcesParams {Cursor : cursor })
597613 if err != nil {
598- return nil , err
614+ return paginatedPage [ * sdk. Resource ]{} , err
599615 }
600- allResources = append (allResources , result .Resources ... )
601- logConn .Printf ("listResources: received page of %d resources (total so far: %d) from serverID=%s" , len (result .Resources ), len (allResources ), c .serverID )
602- cursor = result .NextCursor
616+ return paginatedPage [* sdk.Resource ]{Items : result .Resources , NextCursor : result .NextCursor }, nil
617+ })
618+ if err != nil {
619+ return nil , err
603620 }
604- logConn .Printf ("listResources: received %d resources total from serverID=%s" , len (allResources ), c .serverID )
605- return marshalToResponse (& sdk.ListResourcesResult {Resources : allResources })
621+ return marshalToResponse (& sdk.ListResourcesResult {Resources : resources })
606622}
607623
608624func (c * Connection ) readResource (params interface {}) (* Response , error ) {
@@ -622,26 +638,17 @@ func (c *Connection) listPrompts() (*Response, error) {
622638 return nil , err
623639 }
624640 logConn .Printf ("listPrompts: requesting prompt list from backend serverID=%s" , c .serverID )
625- // Fetch first page to determine initial capacity
626- first , err := c .getSDKSession ().ListPrompts (c .ctx , & sdk.ListPromptsParams {})
627- if err != nil {
628- return nil , err
629- }
630- allPrompts := make ([]* sdk.Prompt , len (first .Prompts ), max (len (first .Prompts ), 1 ))
631- copy (allPrompts , first .Prompts )
632- logConn .Printf ("listPrompts: received page of %d prompts from serverID=%s" , len (first .Prompts ), c .serverID )
633- cursor := first .NextCursor
634- for cursor != "" {
641+ prompts , err := paginateAll (c .serverID , "prompts" , func (cursor string ) (paginatedPage [* sdk.Prompt ], error ) {
635642 result , err := c .getSDKSession ().ListPrompts (c .ctx , & sdk.ListPromptsParams {Cursor : cursor })
636643 if err != nil {
637- return nil , err
644+ return paginatedPage [ * sdk. Prompt ]{} , err
638645 }
639- allPrompts = append (allPrompts , result .Prompts ... )
640- logConn .Printf ("listPrompts: received page of %d prompts (total so far: %d) from serverID=%s" , len (result .Prompts ), len (allPrompts ), c .serverID )
641- cursor = result .NextCursor
646+ return paginatedPage [* sdk.Prompt ]{Items : result .Prompts , NextCursor : result .NextCursor }, nil
647+ })
648+ if err != nil {
649+ return nil , err
642650 }
643- logConn .Printf ("listPrompts: received %d prompts total from serverID=%s" , len (allPrompts ), c .serverID )
644- return marshalToResponse (& sdk.ListPromptsResult {Prompts : allPrompts })
651+ return marshalToResponse (& sdk.ListPromptsResult {Prompts : prompts })
645652}
646653
647654func (c * Connection ) getPrompt (params interface {}) (* Response , error ) {
0 commit comments