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

.output_schema_valueObject (readonly)

Returns the value of attribute output_schema_value.



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

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



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

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, title: nil, description: nil, input_schema: nil, output_schema: nil, annotations: nil, &block) ⇒ Object



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

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

.description(value = NOT_SET) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/mcp/tool.rb', line 63

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

.inherited(subclass) ⇒ Object



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

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)
end

.input_schema(value = NOT_SET) ⇒ Object



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

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



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

def input_schema_value
  @input_schema_value || InputSchema.new
end

.name_valueObject



45
46
47
# File 'lib/mcp/tool.rb', line 45

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

.output_schema(value = NOT_SET) ⇒ Object



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

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

.title(value = NOT_SET) ⇒ Object



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

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

.to_hObject



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

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,
  }.compact
end

.tool_name(value = NOT_SET) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/mcp/tool.rb', line 37

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