Class: ActionMCP::Client::Collection

Inherits:
Object
  • Object
show all
Includes:
RequestTimeouts, Enumerable
Defined in:
lib/action_mcp/client/collection.rb

Overview

Base collection class for MCP client collections

Direct Known Subclasses

Blueprint, Catalog, PromptBook, Toolbox

Constant Summary

Constants included from RequestTimeouts

RequestTimeouts::DEFAULT_TIMEOUT

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from RequestTimeouts

#load_with_timeout

Constructor Details

#initialize(items, client, silence_sql: true) ⇒ Collection

Returns a new instance of Collection.



11
12
13
14
15
16
17
18
# File 'lib/action_mcp/client/collection.rb', line 11

def initialize(items, client, silence_sql: true)
  @collection_data = items || []
  @client = client
  @loaded = !@collection_data.empty?
  @silence_sql = silence_sql
  @next_cursor = nil
  @total = items&.size || 0
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



9
10
11
# File 'lib/action_mcp/client/collection.rb', line 9

def client
  @client
end

#loadedObject (readonly) Also known as: loaded?

Returns the value of attribute loaded.



9
10
11
# File 'lib/action_mcp/client/collection.rb', line 9

def loaded
  @loaded
end

#next_cursorObject (readonly)

Returns the value of attribute next_cursor.



9
10
11
# File 'lib/action_mcp/client/collection.rb', line 9

def next_cursor
  @next_cursor
end

#totalObject (readonly)

Returns the value of attribute total.



9
10
11
# File 'lib/action_mcp/client/collection.rb', line 9

def total
  @total
end

Instance Method Details

#all(limit: nil) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/action_mcp/client/collection.rb', line 20

def all(limit: nil)
  if limit
    # If a limit is provided, use pagination
    result = []
    each_page(limit: limit) { |page| result.concat(page) }
    result
  else
    # Otherwise, maintain the old behavior
    silence_logs { load_items unless @loaded }
    @collection_data
  end
end

#all!(timeout: DEFAULT_TIMEOUT) ⇒ Object



33
34
35
36
# File 'lib/action_mcp/client/collection.rb', line 33

def all!(timeout: DEFAULT_TIMEOUT)
  silence_logs { load_items(force: true, timeout: timeout) }
  @collection_data
end

#each(&block) ⇒ Object



102
103
104
# File 'lib/action_mcp/client/collection.rb', line 102

def each(&block)
  all.each(&block)
end

#each_page(limit: nil) {|page| ... } ⇒ Object

Iterate through all pages of results

Parameters:

  • limit (Integer, nil) (defaults to: nil)

    Optional limit for page size

Yields:

  • (page)

    Block to process each page

Yield Parameters:

  • page (Array<Object>)

    A page of items



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/action_mcp/client/collection.rb', line 70

def each_page(limit: nil)
  return unless block_given?

  current_page = page(limit: limit)
  yield current_page

  while has_more_pages?
    current_page = next_page(limit: limit)
    yield current_page unless current_page.empty?
  end
end

#filter {|item| ... } ⇒ Array<Object>

Filter items based on a given block

Yields:

  • (item)

    Block that determines whether to include an item

Yield Parameters:

  • item (Object)

    An item from the collection

Yield Returns:

  • (Boolean)

    true to include the item, false to exclude it

Returns:

  • (Array<Object>)

    Items that match the filter criteria



88
89
90
# File 'lib/action_mcp/client/collection.rb', line 88

def filter(&block)
  all.select(&block)
end

#has_more_pages?Boolean

Check if there are more pages available

Returns:

  • (Boolean)

    true if there are more pages to fetch



51
52
53
# File 'lib/action_mcp/client/collection.rb', line 51

def has_more_pages?
  !@next_cursor.nil?
end

#next_page(limit: nil) ⇒ Array<Object>

Fetch the next page of results

Parameters:

  • limit (Integer, nil) (defaults to: nil)

    Optional limit for page size

Returns:

  • (Array<Object>)

    The next page of items, or empty array if no more pages



59
60
61
62
63
# File 'lib/action_mcp/client/collection.rb', line 59

def next_page(limit: nil)
  return [] unless has_more_pages?

  page(cursor: @next_cursor, limit: limit)
end

#page(cursor: nil, limit: nil) ⇒ Array<Object>

Fetch a single page of results

Parameters:

  • cursor (String, nil) (defaults to: nil)

    Optional cursor for pagination

  • limit (Integer, nil) (defaults to: nil)

    Optional limit for page size

Returns:

  • (Array<Object>)

    The page of items



43
44
45
46
# File 'lib/action_mcp/client/collection.rb', line 43

def page(cursor: nil, limit: nil)
  silence_logs { load_page(cursor: cursor, limit: limit) }
  @collection_data
end

#sizeInteger

Number of items in the collection

Returns:

  • (Integer)

    The number of items



95
96
97
# File 'lib/action_mcp/client/collection.rb', line 95

def size
  all.size
end