Skip to content

Commit 8b0eaaf

Browse files
committed
Use direct calls instead of public_send in fetch_all_pages
`fetch_all_pages` now returns the page results, and each method extracts its items with `flat_map` a direct call, rather than `public_send` on a passed-in method name.
1 parent 26e058b commit 8b0eaaf

1 file changed

Lines changed: 10 additions & 10 deletions

File tree

lib/mcp/client.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ def list_tools(cursor: nil)
146146
# end
147147
def tools
148148
# TODO: consider renaming to `list_all_tools`.
149-
fetch_all_pages(:tools) { |cursor| list_tools(cursor: cursor) }
149+
fetch_all_pages { |cursor| list_tools(cursor: cursor) }.flat_map(&:tools)
150150
end
151151

152152
# Returns a single page of resources from the server.
@@ -175,7 +175,7 @@ def list_resources(cursor: nil)
175175
# @return [Array<Hash>] An array of available resources.
176176
def resources
177177
# TODO: consider renaming to `list_all_resources`.
178-
fetch_all_pages(:resources) { |cursor| list_resources(cursor: cursor) }
178+
fetch_all_pages { |cursor| list_resources(cursor: cursor) }.flat_map(&:resources)
179179
end
180180

181181
# Returns a single page of resource templates from the server.
@@ -204,7 +204,7 @@ def list_resource_templates(cursor: nil)
204204
# @return [Array<Hash>] An array of available resource templates.
205205
def resource_templates
206206
# TODO: consider renaming to `list_all_resource_templates`.
207-
fetch_all_pages(:resource_templates) { |cursor| list_resource_templates(cursor: cursor) }
207+
fetch_all_pages { |cursor| list_resource_templates(cursor: cursor) }.flat_map(&:resource_templates)
208208
end
209209

210210
# Returns a single page of prompts from the server.
@@ -233,7 +233,7 @@ def list_prompts(cursor: nil)
233233
# @return [Array<Hash>] An array of available prompts.
234234
def prompts
235235
# TODO: consider renaming to `list_all_prompts`.
236-
fetch_all_pages(:prompts) { |cursor| list_prompts(cursor: cursor) }
236+
fetch_all_pages { |cursor| list_prompts(cursor: cursor) }.flat_map(&:prompts)
237237
end
238238

239239
# Calls a tool via the transport layer and returns the full response from the server.
@@ -324,25 +324,25 @@ def ping
324324

325325
private
326326

327-
# Follows `next_cursor` across every page of a list endpoint and returns all
328-
# items concatenated. The `seen` set guards against a server that repeats or
327+
# Walks every page of a list endpoint, following `next_cursor`, and returns
328+
# the page results. The `seen` set guards against a server that repeats or
329329
# cycles cursors, so the loop always terminates.
330-
def fetch_all_pages(items_method)
331-
items = []
330+
def fetch_all_pages
331+
pages = []
332332
seen = Set.new
333333
cursor = nil
334334

335335
loop do
336336
page = yield(cursor)
337-
items.concat(page.public_send(items_method))
337+
pages << page
338338
next_cursor = page.next_cursor
339339
break if next_cursor.nil? || seen.include?(next_cursor)
340340

341341
seen << next_cursor
342342
cursor = next_cursor
343343
end
344344

345-
items
345+
pages
346346
end
347347

348348
def request(method:, params: nil)

0 commit comments

Comments
 (0)