Class: ActionMCP::Client::Toolbox
- Inherits:
-
Collection
- Object
- Collection
- ActionMCP::Client::Toolbox
- 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
-
#contains?(name) ⇒ Boolean
Check if the collection contains a tool with the given name.
-
#filter {|tool| ... } ⇒ Array<Tool>
Filter tools based on a given block.
-
#find(name) ⇒ Tool?
Find a tool by name.
-
#initialize(tools, client) ⇒ Toolbox
constructor
Initialize a new Toolbox with tool definitions.
-
#names ⇒ Array<String>
Get a list of all tool names.
-
#search(keyword) ⇒ Array<Tool>
Get tools by category or type.
-
#size ⇒ Integer
Number of tools in the collection.
-
#to_h(provider = :default) ⇒ Hash
Generate a hash representation of all tools in the collection based on provider format.
- #tools=(tools) ⇒ Object
Methods inherited from Collection
#all, #all!, #each, #each_page, #has_more_pages?, #next_page, #page
Methods included from RequestTimeouts
Constructor Details
#initialize(tools, client) ⇒ Toolbox
Initialize a new Toolbox with tool definitions
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
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
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
36 37 38 |
# File 'lib/action_mcp/client/toolbox.rb', line 36 def find(name) all.find { |tool| tool.name == name } end |
#names ⇒ Array<String>
Get a list of all tool names
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
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 |
#size ⇒ Integer
Number of tools in the collection
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
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 |