Class: ActiveMcp::Tool
- Inherits:
-
Object
- Object
- ActiveMcp::Tool
- Defined in:
- lib/active_mcp/tool.rb
Class Attribute Summary collapse
-
.desc ⇒ Object
readonly
Returns the value of attribute desc.
- .registered_tools ⇒ Object
-
.schema ⇒ Object
readonly
Returns the value of attribute schema.
Class Method Summary collapse
- .argument ⇒ Object
- .authorized_tools(auth_info = nil) ⇒ Object
- .description(value) ⇒ Object
- .inherited(subclass) ⇒ Object
- .property(name, type, required: false, description: nil) ⇒ Object
- .tool_name ⇒ Object
- .visible?(auth_info) ⇒ Boolean
Instance Method Summary collapse
- #call(**args) ⇒ Object
-
#initialize ⇒ Tool
constructor
A new instance of Tool.
- #validate_arguments(args) ⇒ Object
Constructor Details
#initialize ⇒ Tool
Returns a new instance of Tool.
63 64 |
# File 'lib/active_mcp/tool.rb', line 63 def initialize end |
Class Attribute Details
.desc ⇒ Object (readonly)
Returns the value of attribute desc.
6 7 8 |
# File 'lib/active_mcp/tool.rb', line 6 def desc @desc end |
.registered_tools ⇒ Object
32 33 34 |
# File 'lib/active_mcp/tool.rb', line 32 def registered_tools @registered_tools ||= [] end |
.schema ⇒ Object (readonly)
Returns the value of attribute schema.
6 7 8 |
# File 'lib/active_mcp/tool.rb', line 6 def schema @schema end |
Class Method Details
.argument ⇒ Object
28 29 30 |
# File 'lib/active_mcp/tool.rb', line 28 def argument(...) property(...) end |
.authorized_tools(auth_info = nil) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/active_mcp/tool.rb', line 50 def (auth_info = nil) registered_tools.select do |tool_class| tool_class.visible?(auth_info) end.map do |tool_class| { name: tool_class.tool_name, description: tool_class.desc, inputSchema: tool_class.schema } end end |
.description(value) ⇒ Object
12 13 14 |
# File 'lib/active_mcp/tool.rb', line 12 def description(value) @desc = value end |
.inherited(subclass) ⇒ Object
38 39 40 |
# File 'lib/active_mcp/tool.rb', line 38 def inherited(subclass) registered_tools << subclass end |
.property(name, type, required: false, description: nil) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/active_mcp/tool.rb', line 16 def property(name, type, required: false, description: nil) @schema ||= { "type" => "object", "properties" => {}, "required" => [] } @schema["properties"][name.to_s] = {"type" => type.to_s} @schema["properties"][name.to_s]["description"] = description if description @schema["required"] << name.to_s if required end |
.tool_name ⇒ Object
8 9 10 |
# File 'lib/active_mcp/tool.rb', line 8 def tool_name name ? name.underscore.sub(/_tool$/, "") : "" end |
.visible?(auth_info) ⇒ Boolean
42 43 44 45 46 47 48 |
# File 'lib/active_mcp/tool.rb', line 42 def visible?(auth_info) if respond_to?(:authorized?) (auth_info) else true end end |
Instance Method Details
#call(**args) ⇒ Object
66 67 68 |
# File 'lib/active_mcp/tool.rb', line 66 def call(**args) raise NotImplementedError, "#{self.class.name}#call must be implemented" end |
#validate_arguments(args) ⇒ Object
70 71 72 73 74 75 76 |
# File 'lib/active_mcp/tool.rb', line 70 def validate_arguments(args) return true unless self.class.schema JSON::Validator.validate!(self.class.schema, args) rescue JSON::Schema::ValidationError => e {error: e.} end |