Class: ModelContextProtocol::Server::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/model_context_protocol/server/registry.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRegistry

Returns a new instance of Registry.



12
13
14
15
16
17
18
19
20
# File 'lib/model_context_protocol/server/registry.rb', line 12

def initialize
  @prompts = []
  @resources = []
  @resource_templates = []
  @tools = []
  @prompts_options = {}
  @resources_options = {}
  @tools_options = {}
end

Instance Attribute Details

#prompts_optionsObject (readonly)

Returns the value of attribute prompts_options.



3
4
5
# File 'lib/model_context_protocol/server/registry.rb', line 3

def prompts_options
  @prompts_options
end

#resources_optionsObject (readonly)

Returns the value of attribute resources_options.



3
4
5
# File 'lib/model_context_protocol/server/registry.rb', line 3

def resources_options
  @resources_options
end

#tools_optionsObject (readonly)

Returns the value of attribute tools_options.



3
4
5
# File 'lib/model_context_protocol/server/registry.rb', line 3

def tools_options
  @tools_options
end

Class Method Details

.new(&block) ⇒ Object



5
6
7
8
9
10
# File 'lib/model_context_protocol/server/registry.rb', line 5

def self.new(&block)
  registry = allocate
  registry.send(:initialize)
  registry.instance_eval(&block) if block
  registry
end

Instance Method Details

#find_prompt(name) ⇒ Object



59
60
61
# File 'lib/model_context_protocol/server/registry.rb', line 59

def find_prompt(name)
  find_by_name(@prompts, name)
end

#find_resource(uri) ⇒ Object



63
64
65
66
# File 'lib/model_context_protocol/server/registry.rb', line 63

def find_resource(uri)
  entry = @resources.find { |r| r[:uri] == uri }
  entry ? entry[:klass] : nil
end

#find_resource_template(uri) ⇒ Object



68
69
70
71
# File 'lib/model_context_protocol/server/registry.rb', line 68

def find_resource_template(uri)
  entry = @resource_templates.find { |r| uri == r[:uriTemplate] }
  entry ? entry[:klass] : nil
end

#find_tool(name) ⇒ Object



73
74
75
# File 'lib/model_context_protocol/server/registry.rb', line 73

def find_tool(name)
  find_by_name(@tools, name)
end

#prompts(options = {}, &block) ⇒ Object



22
23
24
25
# File 'lib/model_context_protocol/server/registry.rb', line 22

def prompts(options = {}, &block)
  @prompts_options = options
  instance_eval(&block) if block
end

#prompts_data(cursor: nil, page_size: nil, cursor_ttl: nil) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/model_context_protocol/server/registry.rb', line 77

def prompts_data(cursor: nil, page_size: nil, cursor_ttl: nil)
  items = @prompts.map { |entry| entry.except(:klass) }

  if cursor || page_size
    paginated = Server::Pagination.paginate(
      items,
      cursor: cursor,
      page_size: page_size || 100,
      cursor_ttl: cursor_ttl
    )

    PromptsData[prompts: paginated.items, next_cursor: paginated.next_cursor]
  else
    PromptsData[prompts: items]
  end
end

#register(klass) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/model_context_protocol/server/registry.rb', line 41

def register(klass)
  definition = klass.definition
  entry = {klass: klass}.merge(definition)

  case klass.ancestors
  when ->(ancestors) { ancestors.include?(ModelContextProtocol::Server::Prompt) }
    @prompts << entry
  when ->(ancestors) { ancestors.include?(ModelContextProtocol::Server::Resource) }
    @resources << entry
  when ->(ancestors) { ancestors.include?(ModelContextProtocol::Server::ResourceTemplate) }
    @resource_templates << entry
  when ->(ancestors) { ancestors.include?(ModelContextProtocol::Server::Tool) }
    @tools << entry
  else
    raise ArgumentError, "Unknown class type: #{klass}"
  end
end

#resource_templates(&block) ⇒ Object



32
33
34
# File 'lib/model_context_protocol/server/registry.rb', line 32

def resource_templates(&block)
  instance_eval(&block) if block
end

#resource_templates_data(cursor: nil, page_size: nil, cursor_ttl: nil) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/model_context_protocol/server/registry.rb', line 111

def resource_templates_data(cursor: nil, page_size: nil, cursor_ttl: nil)
  items = @resource_templates.map { |entry| entry.except(:klass, :completions) }

  if cursor || page_size
    paginated = Server::Pagination.paginate(
      items,
      cursor: cursor,
      page_size: page_size || 100,
      cursor_ttl: cursor_ttl
    )

    ResourceTemplatesData[resource_templates: paginated.items, next_cursor: paginated.next_cursor]
  else
    ResourceTemplatesData[resource_templates: items]
  end
end

#resources(options = {}, &block) ⇒ Object



27
28
29
30
# File 'lib/model_context_protocol/server/registry.rb', line 27

def resources(options = {}, &block)
  @resources_options = options
  instance_eval(&block) if block
end

#resources_data(cursor: nil, page_size: nil, cursor_ttl: nil) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/model_context_protocol/server/registry.rb', line 94

def resources_data(cursor: nil, page_size: nil, cursor_ttl: nil)
  items = @resources.map { |entry| entry.except(:klass) }

  if cursor || page_size
    paginated = Server::Pagination.paginate(
      items,
      cursor: cursor,
      page_size: page_size || 100,
      cursor_ttl: cursor_ttl
    )

    ResourcesData[resources: paginated.items, next_cursor: paginated.next_cursor]
  else
    ResourcesData[resources: items]
  end
end

#tools(options = {}, &block) ⇒ Object



36
37
38
39
# File 'lib/model_context_protocol/server/registry.rb', line 36

def tools(options = {}, &block)
  @tools_options = options
  instance_eval(&block) if block
end

#tools_data(cursor: nil, page_size: nil, cursor_ttl: nil) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/model_context_protocol/server/registry.rb', line 128

def tools_data(cursor: nil, page_size: nil, cursor_ttl: nil)
  items = @tools.map { |entry| entry.except(:klass) }

  if cursor || page_size
    paginated = Server::Pagination.paginate(
      items,
      cursor: cursor,
      page_size: page_size || 100,
      cursor_ttl: cursor_ttl
    )

    ToolsData[tools: paginated.items, next_cursor: paginated.next_cursor]
  else
    ToolsData[tools: items]
  end
end