internal/mcp: add iterator method for tools
Change-Id: Ibe799caf6592b2763868bdb30f474bc224aa8b00 Reviewed-on: https://go-review.googlesource.com/c/tools/+/676915 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Robert Findley <rfindley@google.com>
This commit is contained in:
parent
e2bbd8002d
commit
d45330dd8f
1 changed files with 70 additions and 16 deletions
|
|
@ -8,6 +8,7 @@ import (
|
|||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"iter"
|
||||
"slices"
|
||||
"sync"
|
||||
|
||||
|
|
@ -314,3 +315,31 @@ func (c *Client) callLoggingHandler(ctx context.Context, cs *ClientSession, para
|
|||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// Tools provides an iterator for all tools available on the server,
|
||||
// automatically fetching pages and managing cursors.
|
||||
// The `params` argument can set the initial cursor.
|
||||
func (c *ClientSession) Tools(ctx context.Context, params *ListToolsParams) iter.Seq2[Tool, error] {
|
||||
currentParams := &ListToolsParams{}
|
||||
if params != nil {
|
||||
*currentParams = *params
|
||||
}
|
||||
return func(yield func(Tool, error) bool) {
|
||||
for {
|
||||
res, err := c.ListTools(ctx, currentParams)
|
||||
if err != nil {
|
||||
yield(Tool{}, err)
|
||||
return
|
||||
}
|
||||
for _, t := range res.Tools {
|
||||
if !yield(*t, nil) {
|
||||
return
|
||||
}
|
||||
}
|
||||
if res.NextCursor == "" {
|
||||
return
|
||||
}
|
||||
currentParams.Cursor = res.NextCursor
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue