Class: ActionMCP::Client::Toolbox

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

Overview

Toolbox

A collection that manages and provides access to tools from the server. This class stores tool definitions along with their input schemas and provides methods for retrieving, filtering, and accessing tools.

Example usage:

tools_data = client.list_tools # Returns array of tool definitions
toolbox = Toolbox.new(tools_data)

# Access a specific tool by name
weather_tool = toolbox.find("weather_forecast")

# Get all tools matching a criteria
calculation_tools = toolbox.filter { |t| t.name.include?("calculate") }

Defined Under Namespace

Classes: Tool

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, #has_more_pages?, #next_page, #page

Methods included from RequestTimeouts

#load_with_timeout

Constructor Details

#initialize(tools, client) ⇒ Toolbox

Initialize a new Toolbox with tool definitions

Parameters:

  • tools (Array<Hash>)

    Array of tool definition hashes, each containing name, description, and inputSchema keys



26
27
28
29
30
# File 'lib/action_mcp/client/toolbox.rb', line 26

def initialize(tools, client)
  super(tools, client)
  self.tools = @collection_data
  @load_method = :list_tools
end

Instance Method Details

#contains?(name) ⇒ Boolean

Check if the collection contains a tool with the given name

Parameters:

  • name (String)

    The tool name to check for

Returns:

  • (Boolean)

    true if a tool with the name exists



68
69
70
# File 'lib/action_mcp/client/toolbox.rb', line 68

def contains?(name)
  all.any? { |tool| tool.name == name }
end

#filter {|tool| ... } ⇒ Array<Tool>

Filter tools based on a given block

Yields:

  • (tool)

    Block that determines whether to include a tool

Yield Parameters:

  • tool (Tool)

    A tool from the collection

Yield Returns:

  • (Boolean)

    true to include the tool, false to exclude it

Returns:

  • (Array<Tool>)

    Tools that match the filter criteria



46
47
48
# File 'lib/action_mcp/client/toolbox.rb', line 46

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

#find(name) ⇒ Tool?

Find a tool by name

Parameters:

  • name (String)

    Name of the tool to find

Returns:

  • (Tool, nil)

    The tool with the given name, or nil if not found



36
37
38
# File 'lib/action_mcp/client/toolbox.rb', line 36

def find(name)
  all.find { |tool| tool.name == name }
end

#namesArray<String>

Get a list of all tool names

Returns:

  • (Array<String>)

    Names of all tools in the collection



53
54
55
# File 'lib/action_mcp/client/toolbox.rb', line 53

def names
  all.map(&:name)
end

#search(keyword) ⇒ Array<Tool>

Get tools by category or type

Parameters:

  • keyword (String)

    Keyword to search for in tool names and descriptions

Returns:

  • (Array<Tool>)

    Tools containing the keyword



76
77
78
79
80
81
# File 'lib/action_mcp/client/toolbox.rb', line 76

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

#sizeInteger

Number of tools in the collection

Returns:

  • (Integer)

    The number of tools



60
61
62
# File 'lib/action_mcp/client/toolbox.rb', line 60

def size
  all.size
end

#to_h(provider = :default) ⇒ Hash

Generate a hash representation of all tools in the collection based on provider format

Parameters:

  • provider (Symbol) (defaults to: :default)

    The provider format to use (:claude, :openai, or :default)

Returns:

  • (Hash)

    Hash containing all tools formatted for the specified provider



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/action_mcp/client/toolbox.rb', line 87

def to_h(provider = :default)
  case provider
  when :claude
    # Claude format
    { "tools" => all.map(&:to_claude_h) }
  when :openai
    # OpenAI format
    { "tools" => all.map(&:to_openai_h) }
  else
    # Default format (same as original)
    { "tools" => all.map(&:to_h) }
  end
end

#tools=(tools) ⇒ Object



101
102
103
# File 'lib/action_mcp/client/toolbox.rb', line 101

def tools=(tools)
  @collection_data = tools.map { |tool_data| Tool.new(tool_data) }
end