Class: ActionMCP::Client::Catalog

Inherits:
Collection show all
Defined in:
lib/action_mcp/client/catalog.rb

Overview

Catalog

A collection that manages and provides access to resources. This class stores resource definitions and provides methods for retrieving, filtering, and accessing resources by URI or other attributes. It supports lazy loading of resources when initialized with a client.

Example usage:

# Eager loading
resources_data = client.list_resources # Returns array of resource definitions
catalog = Catalog.new(resources_data)

# Lazy loading
catalog = Catalog.new([], client)
resources = catalog.all # Resources are loaded here

# Access a specific resource by URI
main_file = catalog.find_by_uri("file:///project/src/main.rs")

# Get all resources matching a criteria
rust_files = catalog.filter { |r| r.mime_type == "text/x-rust" }

Defined Under Namespace

Classes: Resource

Constant Summary

Constants included from RequestTimeouts

RequestTimeouts::DEFAULT_TIMEOUT

Instance Attribute Summary

Attributes inherited from Collection

#client, #loaded, #next_cursor, #total

Instance Method Summary collapse

Methods inherited from Collection

#all, #all!, #each, #each_page, #filter, #has_more_pages?, #next_page, #page, #size

Methods included from RequestTimeouts

#load_with_timeout

Constructor Details

#initialize(resources, client) ⇒ Catalog

Initialize a new Catalog with resource definitions

Parameters:

  • resources (Array<Hash>)

    Array of resource definition hashes, each containing uri, name, description, and mimeType keys

  • client (Object, nil)

    Optional client for lazy loading of resources



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

def initialize(resources, client)
  super(resources, client)
  self.resources = @collection_data
  @load_method = :list_resources
end

Instance Method Details

#contains_uri?(uri) ⇒ Boolean

Check if the collection contains a resource with the given URI

Parameters:

  • uri (String)

    The resource URI to check for

Returns:

  • (Boolean)

    true if a resource with the URI exists



74
75
76
# File 'lib/action_mcp/client/catalog.rb', line 74

def contains_uri?(uri)
  all.any? { |resource| resource.uri == uri }
end

#find_by_mime_type(mime_type) ⇒ Array<Resource>

Find resources by MIME type

Parameters:

  • mime_type (String)

    MIME type to search for

Returns:

  • (Array<Resource>)

    Resources with the given MIME type



59
60
61
# File 'lib/action_mcp/client/catalog.rb', line 59

def find_by_mime_type(mime_type)
  all.select { |resource| resource.mime_type == mime_type }
end

#find_by_name(name) ⇒ Array<Resource>

Find resources by name

Parameters:

  • name (String)

    Name of the resources to find

Returns:

  • (Array<Resource>)

    Resources with the given name



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

def find_by_name(name)
  all.select { |resource| resource.name == name }
end

#find_by_uri(uri) ⇒ Resource?

Find a resource by URI

Parameters:

  • uri (String)

    URI of the resource to find

Returns:

  • (Resource, nil)

    The resource with the given URI, or nil if not found



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

def find_by_uri(uri)
  all.find { |resource| resource.uri == uri }
end

#group_by_mime_typeHash<String, Array<Resource>>

Group resources by MIME type

Returns:

  • (Hash<String, Array<Resource>>)

    Hash mapping MIME types to arrays of resources



81
82
83
# File 'lib/action_mcp/client/catalog.rb', line 81

def group_by_mime_type
  all.group_by(&:mime_type)
end

#resources=(raw_resources) ⇒ Object

Convert raw resource data into Resource objects

Parameters:

  • raw_resources (Array<Hash>)

    Array of resource definition hashes



100
101
102
# File 'lib/action_mcp/client/catalog.rb', line 100

def resources=(raw_resources)
  @collection_data = raw_resources.map { |resource_data| Resource.new(resource_data) }
end

#search(keyword) ⇒ Array<Resource>

Search resources by keyword in name or description

Parameters:

  • keyword (String)

    Keyword to search for

Returns:

  • (Array<Resource>)

    Resources matching the search term



89
90
91
92
93
94
95
# File 'lib/action_mcp/client/catalog.rb', line 89

def search(keyword)
  keyword = keyword.downcase
  all.select do |resource|
    resource.name.downcase.include?(keyword) ||
      resource.description&.downcase&.include?(keyword)
  end
end

#urisArray<String>

Get a list of all resource URIs

Returns:

  • (Array<String>)

    URIs of all resources in the collection



66
67
68
# File 'lib/action_mcp/client/catalog.rb', line 66

def uris
  all.map(&:uri)
end