Class: Otto::MCP::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/otto/mcp/registry.rb

Overview

Registry for managing MCP resources and tools

Instance Method Summary collapse

Constructor Details

#initializeRegistry

Returns a new instance of Registry.



9
10
11
12
# File 'lib/otto/mcp/registry.rb', line 9

def initialize
  @resources = {}
  @tools     = {}
end

Instance Method Details

#call_tool(name, arguments, env) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/otto/mcp/registry.rb', line 73

def call_tool(name, arguments, env)
  tool = @tools[name]
  raise "Tool not found: #{name}" unless tool

  handler = tool[:handler]
  if handler.respond_to?(:call)
    result = handler.call(arguments, env)
  elsif handler.is_a?(String) && handler.include?('.')
    klass_method = handler.split('.')
    klass_name   = klass_method[0..-2].join('::')
    method_name  = klass_method.last

    klass  = Object.const_get(klass_name)
    result = klass.public_send(method_name, arguments, env)
  else
    raise "Invalid tool handler: #{handler}"
  end

  {
    content: [{
      type: 'text',
      text: result.to_s,
    }],
  }
end

#list_resourcesObject



33
34
35
36
37
38
39
40
41
42
# File 'lib/otto/mcp/registry.rb', line 33

def list_resources
  @resources.values.map do |resource|
    {
      uri: resource[:uri],
      name: resource[:name],
      description: resource[:description],
      mimeType: resource[:mimeType],
    }
  end
end

#list_toolsObject



44
45
46
47
48
49
50
51
52
# File 'lib/otto/mcp/registry.rb', line 44

def list_tools
  @tools.values.map do |tool|
    {
      name: tool[:name],
      description: tool[:description],
      inputSchema: tool[:inputSchema],
    }
  end
end

#read_resource(uri) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/otto/mcp/registry.rb', line 54

def read_resource(uri)
  resource = @resources[uri]
  return nil unless resource

  begin
    content = resource[:handler].call
    {
      contents: [{
        uri: uri,
        mimeType: resource[:mimeType],
        text: content.to_s,
      }],
    }
  rescue StandardError => e
    Otto.logger.error "[MCP] Resource read error for #{uri}: #{e.message}"
    nil
  end
end

#register_resource(uri, name, description, mime_type, handler) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/otto/mcp/registry.rb', line 14

def register_resource(uri, name, description, mime_type, handler)
  @resources[uri] = {
    uri: uri,
    name: name,
    description: description,
    mimeType: mime_type,
    handler: handler,
  }
end

#register_tool(name, description, input_schema, handler) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/otto/mcp/registry.rb', line 24

def register_tool(name, description, input_schema, handler)
  @tools[name] = {
    name: name,
    description: description,
    inputSchema: input_schema,
    handler: handler,
  }
end