Class: MCP::Tool

Inherits:
Object
  • Object
show all
Defined in:
lib/mcp/tool.rb,
lib/mcp/tool/response.rb,
lib/mcp/tool/annotations.rb,
lib/mcp/tool/input_schema.rb

Defined Under Namespace

Classes: Annotations, InputSchema, Response

Constant Summary collapse

NOT_SET =
Object.new

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.annotations_valueObject (readonly)

Returns the value of attribute annotations_value.



10
11
12
# File 'lib/mcp/tool.rb', line 10

def annotations_value
  @annotations_value
end

.description_valueObject (readonly)

Returns the value of attribute description_value.



8
9
10
# File 'lib/mcp/tool.rb', line 8

def description_value
  @description_value
end

.input_schema_valueObject (readonly)

Returns the value of attribute input_schema_value.



9
10
11
# File 'lib/mcp/tool.rb', line 9

def input_schema_value
  @input_schema_value
end

Class Method Details

.annotations(hash = NOT_SET) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/mcp/tool.rb', line 66

def annotations(hash = NOT_SET)
  if hash == NOT_SET
    @annotations_value
  else
    @annotations_value = Annotations.new(**hash)
  end
end

.call(*args, server_context: nil) ⇒ Object

Raises:

  • (NotImplementedError)


12
13
14
# File 'lib/mcp/tool.rb', line 12

def call(*args, server_context: nil)
  raise NotImplementedError, "Subclasses must implement call"
end

.define(name: nil, description: nil, input_schema: nil, annotations: nil, &block) ⇒ Object



74
75
76
77
78
79
80
81
82
# File 'lib/mcp/tool.rb', line 74

def define(name: nil, description: nil, input_schema: nil, annotations: nil, &block)
  Class.new(self) do
    tool_name name
    description description
    input_schema input_schema
    self.annotations(annotations) if annotations
    define_singleton_method(:call, &block) if block
  end
end

.description(value = NOT_SET) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/mcp/tool.rb', line 46

def description(value = NOT_SET)
  if value == NOT_SET
    @description_value
  else
    @description_value = value
  end
end

.inherited(subclass) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/mcp/tool.rb', line 26

def inherited(subclass)
  super
  subclass.instance_variable_set(:@name_value, nil)
  subclass.instance_variable_set(:@description_value, nil)
  subclass.instance_variable_set(:@input_schema_value, nil)
  subclass.instance_variable_set(:@annotations_value, nil)
end

.input_schema(value = NOT_SET) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/mcp/tool.rb', line 54

def input_schema(value = NOT_SET)
  if value == NOT_SET
    input_schema_value
  elsif value.is_a?(Hash)
    properties = value[:properties] || value["properties"] || {}
    required = value[:required] || value["required"] || []
    @input_schema_value = InputSchema.new(properties:, required:)
  elsif value.is_a?(InputSchema)
    @input_schema_value = value
  end
end

.name_valueObject



42
43
44
# File 'lib/mcp/tool.rb', line 42

def name_value
  @name_value || StringUtils.handle_from_class_name(name)
end

.to_hObject



16
17
18
19
20
21
22
23
24
# File 'lib/mcp/tool.rb', line 16

def to_h
  result = {
    name: name_value,
    description: description_value,
    inputSchema: input_schema_value.to_h,
  }
  result[:annotations] = annotations_value.to_h if annotations_value
  result
end

.tool_name(value = NOT_SET) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/mcp/tool.rb', line 34

def tool_name(value = NOT_SET)
  if value == NOT_SET
    name_value
  else
    @name_value = value
  end
end