Class: ActiveMcp::Tool

Inherits:
Object
  • Object
show all
Defined in:
lib/active_mcp/tool.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTool

Returns a new instance of Tool.



47
48
# File 'lib/active_mcp/tool.rb', line 47

def initialize
end

Class Attribute Details

.descObject (readonly)

Returns the value of attribute desc.



6
7
8
# File 'lib/active_mcp/tool.rb', line 6

def desc
  @desc
end

.registered_toolsObject



32
33
34
# File 'lib/active_mcp/tool.rb', line 32

def registered_tools
  @registered_tools ||= []
end

.schemaObject (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

.argumentObject



28
29
30
# File 'lib/active_mcp/tool.rb', line 28

def argument(...)
  property(...)
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_nameObject



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

def tool_name
  name ? name.underscore.sub(/_tool$/, "") : ""
end

.visible?(auth_info) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/active_mcp/tool.rb', line 42

def visible?(auth_info)
  true
end

Instance Method Details

#call(**args) ⇒ Object

Raises:

  • (NotImplementedError)


50
51
52
# File 'lib/active_mcp/tool.rb', line 50

def call(**args)
  raise NotImplementedError, "#{self.class.name}#call must be implemented"
end

#validate_arguments(args) ⇒ Object



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

def validate_arguments(args)
  return true unless self.class.schema

  JSON::Validator.validate!(self.class.schema, args)
rescue JSON::Schema::ValidationError => e
  {error: e.message}
end