Class: MCP::Tool
- Inherits:
-
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_value ⇒ Object
Returns the value of attribute annotations_value.
17
18
19
|
# File 'lib/mcp/tool.rb', line 17
def annotations_value
@annotations_value
end
|
.description_value ⇒ Object
Returns the value of attribute description_value.
15
16
17
|
# File 'lib/mcp/tool.rb', line 15
def description_value
@description_value
end
|
.icons_value ⇒ Object
Returns the value of attribute icons_value.
16
17
18
|
# File 'lib/mcp/tool.rb', line 16
def icons_value
@icons_value
end
|
Returns the value of attribute meta_value.
18
19
20
|
# File 'lib/mcp/tool.rb', line 18
def meta_value
@meta_value
end
|
.output_schema_value ⇒ Object
Returns the value of attribute output_schema_value.
67
68
69
|
# File 'lib/mcp/tool.rb', line 67
def output_schema_value
@output_schema_value
end
|
.title_value ⇒ Object
Returns the value of attribute title_value.
14
15
16
|
# File 'lib/mcp/tool.rb', line 14
def title_value
@title_value
end
|
Class Method Details
.annotations(hash = NOT_SET) ⇒ Object
121
122
123
124
125
126
127
|
# File 'lib/mcp/tool.rb', line 121
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
20
21
22
|
# File 'lib/mcp/tool.rb', line 20
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
129
130
131
132
133
134
135
136
137
138
139
140
141
|
# File 'lib/mcp/tool.rb', line 129
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
77
78
79
80
81
82
83
|
# File 'lib/mcp/tool.rb', line 77
def description(value = NOT_SET)
if value == NOT_SET
@description_value
else
@description_value = value
end
end
|
.icons(value = NOT_SET) ⇒ Object
85
86
87
88
89
90
91
|
# File 'lib/mcp/tool.rb', line 85
def icons(value = NOT_SET)
if value == NOT_SET
@icons_value
else
@icons_value = value
end
end
|
.inherited(subclass) ⇒ Object
37
38
39
40
41
42
43
44
45
46
47
|
# File 'lib/mcp/tool.rb', line 37
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
|
93
94
95
96
97
98
99
100
101
|
# File 'lib/mcp/tool.rb', line 93
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
|
63
64
65
|
# File 'lib/mcp/tool.rb', line 63
def input_schema_value
@input_schema_value || InputSchema.new
end
|
113
114
115
116
117
118
119
|
# File 'lib/mcp/tool.rb', line 113
def meta(value = NOT_SET)
if value == NOT_SET
@meta_value
else
@meta_value = value
end
end
|
.name_value ⇒ Object
59
60
61
|
# File 'lib/mcp/tool.rb', line 59
def name_value
@name_value || (name.nil? ? nil : StringUtils.handle_from_class_name(name))
end
|
.output_schema(value = NOT_SET) ⇒ Object
103
104
105
106
107
108
109
110
111
|
# File 'lib/mcp/tool.rb', line 103
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
69
70
71
72
73
74
75
|
# File 'lib/mcp/tool.rb', line 69
def title(value = NOT_SET)
if value == NOT_SET
@title_value
else
@title_value = value
end
end
|
.to_h ⇒ Object
24
25
26
27
28
29
30
31
32
33
34
35
|
# File 'lib/mcp/tool.rb', line 24
def to_h
{
name: name_value,
title: title_value,
description: description_value,
icons: icons_value&.then { |icons| icons.empty? ? nil : 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
|
49
50
51
52
53
54
55
56
57
|
# File 'lib/mcp/tool.rb', line 49
def tool_name(value = NOT_SET)
if value == NOT_SET
name_value
else
@name_value = value
validate!
end
end
|
.validate! ⇒ Object
145
146
147
148
149
150
151
152
153
154
155
156
157
|
# File 'lib/mcp/tool.rb', line 145
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, " Tool names only allowed characters: uppercase and lowercase ASCII letters (A-Z, a-z), digits (0-9), underscore (_), hyphen (-), and dot (.).\n MESSAGE\n end\nend\n"
|