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.



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

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

.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 authorized_tools(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_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
45
46
47
48
# File 'lib/active_mcp/tool.rb', line 42

def visible?(auth_info)
  if respond_to?(:authorized?)
    authorized?(auth_info)
  else
    true
  end
end

Instance Method Details

#call(**args) ⇒ Object

Raises:

  • (NotImplementedError)


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.message}
end