Class: SolidAgent::AgentManifest::Tool

Inherits:
Object
  • Object
show all
Defined in:
lib/solid_agent/agent_manifest/tool.rb

Overview

Tool represents a function/tool that an agent can invoke.

Tools follow the MCP (Model Context Protocol) conventions for maximum portability across different LLM providers and frameworks.

Examples:

Inline tool definition

tool = Tool.new(
  name: "search",
  description: "Search the web for information",
  input_schema: {
    "type" => "object",
    "properties" => {
      "query" => { "type" => "string", "description" => "Search query" }
    },
    "required" => ["query"]
  }
)

Tool reference

tool = Tool.new(ref: "@activeagents/web-tools/search")

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Tool

Returns a new instance of Tool.



39
40
41
42
43
44
# File 'lib/solid_agent/agent_manifest/tool.rb', line 39

def initialize(attributes = {})
  attributes.each do |key, value|
    setter = "#{key}="
    send(setter, value) if respond_to?(setter)
  end
end

Instance Attribute Details

#descriptionString?

Returns Human-readable description.

Returns:

  • (String, nil)

    Human-readable description



31
32
33
# File 'lib/solid_agent/agent_manifest/tool.rb', line 31

def description
  @description
end

#input_schemaHash?

Returns JSON Schema for tool input parameters.

Returns:

  • (Hash, nil)

    JSON Schema for tool input parameters



34
35
36
# File 'lib/solid_agent/agent_manifest/tool.rb', line 34

def input_schema
  @input_schema
end

#nameString?

Returns Tool name (required for inline definitions).

Returns:

  • (String, nil)

    Tool name (required for inline definitions)



28
29
30
# File 'lib/solid_agent/agent_manifest/tool.rb', line 28

def name
  @name
end

#refString?

Returns Reference to external tool (e.g., "$ref" or package reference).

Returns:

  • (String, nil)

    Reference to external tool (e.g., "$ref" or package reference)



37
38
39
# File 'lib/solid_agent/agent_manifest/tool.rb', line 37

def ref
  @ref
end

Class Method Details

.from_hash(data) ⇒ Tool

Create a Tool from a hash (flexible key formats)

Parameters:

  • data (Hash)

    Tool data with various key formats

Returns:



117
118
119
120
121
122
123
124
125
# File 'lib/solid_agent/agent_manifest/tool.rb', line 117

def self.from_hash(data)
  return new(ref: data["$ref"]) if data["$ref"]

  new(
    name: data["name"] || data[:name],
    description: data["description"] || data[:description],
    input_schema: data["inputSchema"] || data["input_schema"] || data[:input_schema] || data[:inputSchema]
  )
end

.from_tool_builder(schema) ⇒ Tool

Create a Tool from a HasTools::ToolBuilder schema

Parameters:

  • schema (Hash)

    Schema from ToolBuilder

Returns:



105
106
107
108
109
110
111
# File 'lib/solid_agent/agent_manifest/tool.rb', line 105

def self.from_tool_builder(schema)
  new(
    name: schema[:name] || schema["name"],
    description: schema[:description] || schema["description"],
    input_schema: schema[:parameters] || schema["parameters"]
  )
end

Instance Method Details

#inline?Boolean

Check if this tool has an inline definition

Returns:

  • (Boolean)


56
57
58
# File 'lib/solid_agent/agent_manifest/tool.rb', line 56

def inline?
  !reference? && name.present?
end

#parameter_namesArray<String>

Get all parameter names

Returns:

  • (Array<String>)


137
138
139
140
# File 'lib/solid_agent/agent_manifest/tool.rb', line 137

def parameter_names
  properties = input_schema&.dig("properties") || input_schema&.dig(:properties) || {}
  properties.keys.map(&:to_s)
end

#reference?Boolean

Check if this tool is a reference to an external tool

Returns:

  • (Boolean)


49
50
51
# File 'lib/solid_agent/agent_manifest/tool.rb', line 49

def reference?
  ref.present?
end

#required_parametersArray<String>

Get list of required parameters

Returns:

  • (Array<String>)


130
131
132
# File 'lib/solid_agent/agent_manifest/tool.rb', line 130

def required_parameters
  input_schema&.dig("required") || input_schema&.dig(:required) || []
end

#to_hHash

Convert to hash representation

Returns:

  • (Hash)


63
64
65
66
67
68
69
70
71
72
73
# File 'lib/solid_agent/agent_manifest/tool.rb', line 63

def to_h
  if reference?
    { "$ref" => ref }
  else
    {
      name: name,
      description: description,
      inputSchema: input_schema
    }.compact
  end
end

#to_mcp_jsonString

Convert to MCP-compatible JSON string

Returns:

  • (String)

    JSON representation following MCP tool format



78
79
80
81
82
83
84
# File 'lib/solid_agent/agent_manifest/tool.rb', line 78

def to_mcp_json
  {
    name: name,
    description: description,
    inputSchema: input_schema
  }.compact.to_json
end

#to_tool_builder_schemaHash

Convert to HasTools::ToolBuilder compatible schema

This format is compatible with OpenAI's function calling API and SolidAgent's existing HasTools concern.

Returns:

  • (Hash)


92
93
94
95
96
97
98
99
# File 'lib/solid_agent/agent_manifest/tool.rb', line 92

def to_tool_builder_schema
  {
    type: "function",
    name: name,
    description: description,
    parameters: input_schema || { type: "object", properties: {} }
  }
end

#valid?Boolean

Check if tool is valid (has required fields)

Returns:

  • (Boolean)


145
146
147
# File 'lib/solid_agent/agent_manifest/tool.rb', line 145

def valid?
  reference? || name.present?
end

#validation_errorsArray<String>

Validation errors

Returns:

  • (Array<String>)


152
153
154
155
156
157
# File 'lib/solid_agent/agent_manifest/tool.rb', line 152

def validation_errors
  errors = []
  errors << "Tool must have a name or $ref" unless valid?
  errors << "Tool description is recommended" if inline? && description.blank?
  errors
end