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:
Sam Thanawalla 2025-05-28 16:50:51 +00:00
commit d45330dd8f

View file

@ -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
}
}
}