Class: ModelContextProtocol::Server::Registry
- Inherits:
-
Object
- Object
- ModelContextProtocol::Server::Registry
show all
- Defined in:
- lib/model_context_protocol/server/registry.rb
Defined Under Namespace
Classes: PromptsData, ResourceTemplatesData, ResourcesData, ToolsData
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
-
#find_prompt(name) ⇒ Object
-
#find_resource(uri) ⇒ Object
-
#find_resource_template(uri) ⇒ Object
-
#find_tool(name) ⇒ Object
-
#handler_names ⇒ Object
-
#initialize ⇒ Registry
constructor
A new instance of Registry.
-
#prompts(options = {}, &block) ⇒ Object
-
#prompts_data(cursor: nil, page_size: nil, cursor_ttl: nil) ⇒ Object
-
#register(klass) ⇒ Object
-
#resource_templates(&block) ⇒ Object
-
#resource_templates_data(cursor: nil, page_size: nil, cursor_ttl: nil) ⇒ Object
-
#resources(options = {}, &block) ⇒ Object
-
#resources_data(cursor: nil, page_size: nil, cursor_ttl: nil) ⇒ Object
-
#tools(options = {}, &block) ⇒ Object
-
#tools_data(cursor: nil, page_size: nil, cursor_ttl: nil) ⇒ Object
Constructor Details
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_options ⇒ Object
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_options ⇒ Object
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
|
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
|
73
74
75
|
# File 'lib/model_context_protocol/server/registry.rb', line 73
def find_tool(name)
find_by_name(@tools, name)
end
|
#handler_names ⇒ Object
77
78
79
80
81
82
83
|
# File 'lib/model_context_protocol/server/registry.rb', line 77
def handler_names
{
prompts: @prompts.map { |p| p[:name] },
resources: @resources.map { |r| r[:name] || r[:uri] },
tools: @tools.map { |t| t[: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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
# File 'lib/model_context_protocol/server/registry.rb', line 85
def prompts_data(cursor: nil, page_size: nil, cursor_ttl: nil)
items = @prompts.map { |entry| entry.except(:klass) }
if cursor || page_size
paginated = Server::.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
# File 'lib/model_context_protocol/server/registry.rb', line 119
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::.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
# File 'lib/model_context_protocol/server/registry.rb', line 102
def resources_data(cursor: nil, page_size: nil, cursor_ttl: nil)
items = @resources.map { |entry| entry.except(:klass) }
if cursor || page_size
paginated = Server::.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
|
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
|
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
# File 'lib/model_context_protocol/server/registry.rb', line 136
def tools_data(cursor: nil, page_size: nil, cursor_ttl: nil)
items = @tools.map { |entry| entry.except(:klass) }
if cursor || page_size
paginated = Server::.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
|