Class: MCP::Tool

Inherits:
Object
  • Object
show all
Defined in:
lib/mcp/tool.rb,
lib/mcp/tool/schema.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, Schema

Constant Summary collapse

NOT_SET =
Object.new
MAX_LENGTH_OF_NAME =
128

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.annotations_valueObject (readonly)

Returns the value of attribute annotations_value.



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

def annotations_value
  @annotations_value
end

.description_valueObject (readonly)

Returns the value of attribute description_value.



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

def description_value
  @description_value
end

.icons_valueObject (readonly)

Returns the value of attribute icons_value.



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

def icons_value
  @icons_value
end

.meta_valueObject (readonly)

Returns the value of attribute meta_value.



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

def meta_value
  @meta_value
end

.output_schema_valueObject (readonly)

Returns the value of attribute output_schema_value.



62
63
64
# File 'lib/mcp/tool.rb', line 62

def output_schema_value
  @output_schema_value
end

.title_valueObject (readonly)

Returns the value of attribute title_value.



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

def title_value
  @title_value
end

Class Method Details

.annotations(hash = NOT_SET) ⇒ Object



116
117
118
119
120
121
122
# File 'lib/mcp/tool.rb', line 116

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)


15
16
17
# File 'lib/mcp/tool.rb', line 15

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

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



124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/mcp/tool.rb', line 124

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

.description(value = NOT_SET) ⇒ Object



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

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

.icons(value = NOT_SET) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/mcp/tool.rb', line 80

def icons(value = NOT_SET)
  if value == NOT_SET
    @icons_value
  else
    @icons_value = value
  end
end

.inherited(subclass) ⇒ Object



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

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(:@icons_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



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

def input_schema(value = NOT_SET)
  if value == NOT_SET
    input_schema_value
  elsif value.is_a?(Hash)
    @input_schema_value = InputSchema.new(value)
  elsif value.is_a?(InputSchema)
    @input_schema_value = value
  end
end

.input_schema_valueObject



58
59
60
# File 'lib/mcp/tool.rb', line 58

def input_schema_value
  @input_schema_value || InputSchema.new
end

.meta(value = NOT_SET) ⇒ Object



108
109
110
111
112
113
114
# File 'lib/mcp/tool.rb', line 108

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

.name_valueObject



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

def name_value
  @name_value || (name.nil? ? nil : StringUtils.handle_from_class_name(name))
end

.output_schema(value = NOT_SET) ⇒ Object



98
99
100
101
102
103
104
105
106
# File 'lib/mcp/tool.rb', line 98

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



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

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

.to_hObject



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

def to_h
  {
    name: name_value,
    title: title_value,
    description: description_value,
    icons: icons&.map(&:to_h),
    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



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

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

    validate!
  end
end

.validate!Object

It complies with the following tool name specification: modelcontextprotocol.io/specification/latest/server/tools#tool-names



140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/mcp/tool.rb', line 140

def validate!
  return true unless tool_name

  if tool_name.empty? || tool_name.length > MAX_LENGTH_OF_NAME
    raise ArgumentError, "Tool names should be between 1 and 128 characters in length (inclusive)."
  end

  unless tool_name.match?(/\A[A-Za-z\d_\-\.]+\z/)
    raise ArgumentError, <<~MESSAGE
      Tool names only allowed characters: uppercase and lowercase ASCII letters (A-Z, a-z), digits (0-9), underscore (_), hyphen (-), and dot (.).
    MESSAGE
  end
end