Class: Toolchest::ToolDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/toolchest/tool_definition.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method_name:, description:, params:, toolbox_class:, custom_name: nil, access_level: nil, scope: nil, annotations: nil) ⇒ ToolDefinition

Returns a new instance of ToolDefinition.



5
6
7
8
9
10
11
12
13
14
# File 'lib/toolchest/tool_definition.rb', line 5

def initialize(method_name:, description:, params:, toolbox_class:, custom_name: nil, access_level: nil, scope: nil, annotations: nil)
  @method_name = method_name.to_sym
  @description = description
  @params = params
  @toolbox_class = toolbox_class
  @custom_name = custom_name
  @access_level = access_level
  @scope = scope ? Array(scope) : nil
  @annotations = annotations
end

Instance Attribute Details

#access_levelObject (readonly)

Returns the value of attribute access_level.



3
4
5
# File 'lib/toolchest/tool_definition.rb', line 3

def access_level
  @access_level
end

#annotationsObject (readonly)

Returns the value of attribute annotations.



3
4
5
# File 'lib/toolchest/tool_definition.rb', line 3

def annotations
  @annotations
end

#custom_nameObject (readonly)

Returns the value of attribute custom_name.



3
4
5
# File 'lib/toolchest/tool_definition.rb', line 3

def custom_name
  @custom_name
end

#descriptionObject (readonly)

Returns the value of attribute description.



3
4
5
# File 'lib/toolchest/tool_definition.rb', line 3

def description
  @description
end

#method_nameObject (readonly)

Returns the value of attribute method_name.



3
4
5
# File 'lib/toolchest/tool_definition.rb', line 3

def method_name
  @method_name
end

#paramsObject (readonly)

Returns the value of attribute params.



3
4
5
# File 'lib/toolchest/tool_definition.rb', line 3

def params
  @params
end

#scopeObject (readonly)

Returns the value of attribute scope.



3
4
5
# File 'lib/toolchest/tool_definition.rb', line 3

def scope
  @scope
end

#toolbox_classObject (readonly)

Returns the value of attribute toolbox_class.



3
4
5
# File 'lib/toolchest/tool_definition.rb', line 3

def toolbox_class
  @toolbox_class
end

Instance Method Details

#input_schemaObject



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/toolchest/tool_definition.rb', line 45

def input_schema
  properties = {}
  required = []

  @params.each do |param|
    properties[param.name] = param.to_json_schema
    required << param.name.to_s if param.required?
  end

  schema = { type: "object", properties: properties }
  schema[:required] = required if required.any?
  schema
end

#resolved_annotationsObject



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/toolchest/tool_definition.rb', line 33

def resolved_annotations
  base = case @access_level
  when :read
    { readOnlyHint: true, destructiveHint: false }
  when :write
    { readOnlyHint: false, destructiveHint: true }
  else
    {}
  end
  base.merge(@annotations || {})
end

#to_mcp_schema(naming_strategy = nil) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/toolchest/tool_definition.rb', line 22

def to_mcp_schema(naming_strategy = nil)
  schema = {
    name: tool_name(naming_strategy),
    description: @description,
    inputSchema: input_schema
  }
  hints = resolved_annotations
  schema[:annotations] = hints if hints.any?
  schema
end

#tool_name(naming_strategy = nil) ⇒ Object



16
17
18
19
20
# File 'lib/toolchest/tool_definition.rb', line 16

def tool_name(naming_strategy = nil)
  return @custom_name if @custom_name
  naming_strategy ||= Toolchest.configuration.tool_naming
  Naming.generate(toolbox_class, method_name, naming_strategy)
end