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,
lib/mcp/tool/output_schema.rb

Defined Under Namespace

Classes: Annotations, InputSchema, OutputSchema, 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.



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

def description_value
  @description_value
end

.meta_valueObject (readonly)

Returns the value of attribute meta_value.



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

def meta_value
  @meta_value
end

.output_schema_valueObject (readonly)

Returns the value of attribute output_schema_value.



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

def output_schema_value
  @output_schema_value
end

.title_valueObject (readonly)

Returns the value of attribute title_value.



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

def title_value
  @title_value
end

Class Method Details

.annotations(hash = NOT_SET) ⇒ Object



104
105
106
107
108
109
110
# File 'lib/mcp/tool.rb', line 104

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)


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

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

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



112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/mcp/tool.rb', line 112

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

.description(value = NOT_SET) ⇒ Object



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

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

.inherited(subclass) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/mcp/tool.rb', line 29

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

.input_schema(value = NOT_SET) ⇒ Object



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

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

.input_schema_valueObject



52
53
54
# File 'lib/mcp/tool.rb', line 52

def input_schema_value
  @input_schema_value || InputSchema.new
end

.meta(value = NOT_SET) ⇒ Object



96
97
98
99
100
101
102
# File 'lib/mcp/tool.rb', line 96

def meta(value = NOT_SET)
  if value == NOT_SET
    @meta_value
  else
    @meta_value = value
  end
end

.name_valueObject



48
49
50
# File 'lib/mcp/tool.rb', line 48

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

.output_schema(value = NOT_SET) ⇒ Object



86
87
88
89
90
91
92
93
94
# File 'lib/mcp/tool.rb', line 86

def output_schema(value = NOT_SET)
  if value == NOT_SET
    output_schema_value
  elsif value.is_a?(Hash)
    @output_schema_value = OutputSchema.new(value)
  elsif value.is_a?(OutputSchema)
    @output_schema_value = value
  end
end

.title(value = NOT_SET) ⇒ Object



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

def title(value = NOT_SET)
  if value == NOT_SET
    @title_value
  else
    @title_value = value
  end
end

.to_hObject



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

def to_h
  {
    name: name_value,
    title: title_value,
    description: description_value,
    inputSchema: input_schema_value.to_h,
    outputSchema: @output_schema_value&.to_h,
    annotations: annotations_value&.to_h,
    _meta: meta_value,
  }.compact
end

.tool_name(value = NOT_SET) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/mcp/tool.rb', line 40

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