Class: SwaggerMCPTool::ToolRegistry

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/swagger_mcp_tool/tool_registry.rb

Overview

Registry for managing dynamic MCP tools

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = nil, logger = nil) ⇒ ToolRegistry

Returns a new instance of ToolRegistry.



17
18
19
20
21
# File 'lib/swagger_mcp_tool/tool_registry.rb', line 17

def initialize(config = nil, logger = nil)
  @dynamic_tools = []
  @tool_names = Set.new
  @logger = logger || config&.logger || Logger.new($stdout)
end

Instance Attribute Details

#dynamic_toolsObject

Returns the value of attribute dynamic_tools.



15
16
17
# File 'lib/swagger_mcp_tool/tool_registry.rb', line 15

def dynamic_tools
  @dynamic_tools
end

Instance Method Details

#create_mcp_tool(tool_def, base_url) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/swagger_mcp_tool/tool_registry.rb', line 63

def create_mcp_tool(tool_def, base_url)
  schema = build_clean_schema(tool_def['parameters'])

  execution_context = create_execution_context(tool_def, base_url)
  registry_instance = self

  MCP::Tool.define(
    name: tool_def['name'],
    description: tool_def['description'],
    input_schema: schema,
    annotations: build_annotations(tool_def['name'], tool_def['method'])
  ) do |args = {}, server_context = {}|
    registry_instance.execute_tool(execution_context, args, server_context)
  end
end

#ensure_setup!Object

Guard method to ensure setup is called



53
54
55
56
57
# File 'lib/swagger_mcp_tool/tool_registry.rb', line 53

def ensure_setup!
  return if @initialized

  raise 'ToolRegistry must be setup before use. Call ToolRegistry.instance.setup(config, logger)'
end

#execute_tool(context, args, server_context) ⇒ Object



59
60
61
# File 'lib/swagger_mcp_tool/tool_registry.rb', line 59

def execute_tool(context, args, server_context)
  execute_tool_with_context(context, args, server_context)
end

#register_dynamic_tools(tools, base_url, _swagger_spec = nil) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/swagger_mcp_tool/tool_registry.rb', line 23

def register_dynamic_tools(tools, base_url, _swagger_spec = nil)
  ensure_setup!
  @dynamic_tools = @config.tools if @config&.tools
  validate_inputs!(tools, base_url)
  @logger.info "Registering #{tools.size} dynamic tools"
  excluded_tools = Set.new(@config.exclude_tools)
 
  tools.each do |tool_def|
    unless excluded_tools.include?(tool_def['name'])
      register_tool(tool_def, base_url) unless tool_exists?(tool_def['name'])
    end
  end

  @logger.info "Successfully registered #{@dynamic_tools.size} tools"
  @dynamic_tools.size
end

#setup(config = nil, logger = nil) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/swagger_mcp_tool/tool_registry.rb', line 40

def setup(config = nil, logger = nil)
  return if @initialized

  @config = config
  @logger = logger || config&.logger || Logger.new($stdout)
  @initialized = true
  @tool_names = Set.new
  @dynamic_tools = []

  @logger.info 'ToolRegistry singleton initialized'
end