Class: LanguageOperator::Dsl::ToolDefinition
- Inherits:
-
Object
- Object
- LanguageOperator::Dsl::ToolDefinition
- Includes:
- Helpers
- Defined in:
- lib/language_operator/dsl/tool_definition.rb
Overview
Tool definition for MCP tools
Defines an MCP tool with parameters, description, and execution logic. Used within the DSL to create tools that can be registered and served.
Constant Summary collapse
- HTTP =
Provide access to HTTP and Shell helper classes as constants
LanguageOperator::Dsl::HTTP
- Shell =
LanguageOperator::Dsl::Shell
Instance Attribute Summary collapse
-
#execute_block ⇒ Object
readonly
Returns the value of attribute execute_block.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#parameters ⇒ Object
readonly
Returns the value of attribute parameters.
Instance Method Summary collapse
- #call(params) ⇒ Object
- #description(val = nil) ⇒ Object
- #execute(&block) ⇒ Object
-
#initialize(name) ⇒ ToolDefinition
constructor
A new instance of ToolDefinition.
- #parameter(name) ⇒ Object
- #to_schema ⇒ Object
Methods included from Helpers
#env_get, #env_required, #error, #parse_csv, #run_command, #shell_escape, #success, #truncate, #validate_email, #validate_phone, #validate_url
Constructor Details
#initialize(name) ⇒ ToolDefinition
Returns a new instance of ToolDefinition.
38 39 40 41 42 43 |
# File 'lib/language_operator/dsl/tool_definition.rb', line 38 def initialize(name) @name = name @parameters = {} @execute_block = nil @description = nil end |
Instance Attribute Details
#execute_block ⇒ Object (readonly)
Returns the value of attribute execute_block.
36 37 38 |
# File 'lib/language_operator/dsl/tool_definition.rb', line 36 def execute_block @execute_block end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
36 37 38 |
# File 'lib/language_operator/dsl/tool_definition.rb', line 36 def name @name end |
#parameters ⇒ Object (readonly)
Returns the value of attribute parameters.
36 37 38 |
# File 'lib/language_operator/dsl/tool_definition.rb', line 36 def parameters @parameters end |
Instance Method Details
#call(params) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/language_operator/dsl/tool_definition.rb', line 61 def call(params) log_debug "Calling tool '#{@name}' with params: #{params.inspect}" # Apply default values for missing optional parameters @parameters.each do |name, param_def| default_value = param_def.instance_variable_get(:@default) params[name] = default_value if !params.key?(name) && !default_value.nil? # Validate required parameters raise ArgumentError, "Missing required parameter: #{name}" if param_def.required? && !params.key?(name) # Validate parameter format if validator is set and value is present if params.key?(name) error = param_def.validate_value(params[name]) raise ArgumentError, error if error end end # Call the execute block with parameters result = @execute_block.call(params) if @execute_block log_debug "Tool '#{@name}' completed: #{truncate_for_log(result)}" result end |
#description(val = nil) ⇒ Object
45 46 47 48 49 |
# File 'lib/language_operator/dsl/tool_definition.rb', line 45 def description(val = nil) return @description if val.nil? @description = val end |
#execute(&block) ⇒ Object
57 58 59 |
# File 'lib/language_operator/dsl/tool_definition.rb', line 57 def execute(&block) @execute_block = block end |
#parameter(name) ⇒ Object
51 52 53 54 55 |
# File 'lib/language_operator/dsl/tool_definition.rb', line 51 def parameter(name, &) param = ParameterDefinition.new(name) param.instance_eval(&) if block_given? @parameters[name.to_s] = param end |
#to_schema ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/language_operator/dsl/tool_definition.rb', line 86 def to_schema { 'name' => @name, 'description' => @description, 'inputSchema' => { 'type' => 'object', 'properties' => @parameters.transform_values(&:to_schema), 'required' => @parameters.select { |_, p| p.required? }.keys } } end |