Class: SolidAgent::AgentManifest::Tool
- Inherits:
-
Object
- Object
- SolidAgent::AgentManifest::Tool
- 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.
Instance Attribute Summary collapse
-
#description ⇒ String?
Human-readable description.
-
#input_schema ⇒ Hash?
JSON Schema for tool input parameters.
-
#name ⇒ String?
Tool name (required for inline definitions).
-
#ref ⇒ String?
Reference to external tool (e.g., "$ref" or package reference).
Class Method Summary collapse
-
.from_hash(data) ⇒ Tool
Create a Tool from a hash (flexible key formats).
-
.from_tool_builder(schema) ⇒ Tool
Create a Tool from a HasTools::ToolBuilder schema.
Instance Method Summary collapse
-
#initialize(attributes = {}) ⇒ Tool
constructor
A new instance of Tool.
-
#inline? ⇒ Boolean
Check if this tool has an inline definition.
-
#parameter_names ⇒ Array<String>
Get all parameter names.
-
#reference? ⇒ Boolean
Check if this tool is a reference to an external tool.
-
#required_parameters ⇒ Array<String>
Get list of required parameters.
-
#to_h ⇒ Hash
Convert to hash representation.
-
#to_mcp_json ⇒ String
Convert to MCP-compatible JSON string.
-
#to_tool_builder_schema ⇒ Hash
Convert to HasTools::ToolBuilder compatible schema.
-
#valid? ⇒ Boolean
Check if tool is valid (has required fields).
-
#validation_errors ⇒ Array<String>
Validation errors.
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
#description ⇒ String?
Returns Human-readable description.
31 32 33 |
# File 'lib/solid_agent/agent_manifest/tool.rb', line 31 def description @description end |
#input_schema ⇒ Hash?
Returns 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 |
#name ⇒ String?
Returns Tool name (required for inline definitions).
28 29 30 |
# File 'lib/solid_agent/agent_manifest/tool.rb', line 28 def name @name end |
#ref ⇒ String?
Returns 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)
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
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
56 57 58 |
# File 'lib/solid_agent/agent_manifest/tool.rb', line 56 def inline? !reference? && name.present? end |
#parameter_names ⇒ Array<String>
Get all parameter names
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
49 50 51 |
# File 'lib/solid_agent/agent_manifest/tool.rb', line 49 def reference? ref.present? end |
#required_parameters ⇒ Array<String>
Get list of required parameters
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_h ⇒ Hash
Convert to hash representation
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_json ⇒ String
Convert to MCP-compatible JSON string
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_schema ⇒ Hash
Convert to HasTools::ToolBuilder compatible schema
This format is compatible with OpenAI's function calling API and SolidAgent's existing HasTools concern.
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)
145 146 147 |
# File 'lib/solid_agent/agent_manifest/tool.rb', line 145 def valid? reference? || name.present? end |
#validation_errors ⇒ Array<String>
Validation errors
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 |