actionmcp 0.201.2 → 0.202.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +40 -0
  3. data/app/controllers/action_mcp/application_controller.rb +21 -36
  4. data/app/jobs/action_mcp/tool_execution_job.rb +2 -16
  5. data/app/models/action_mcp/session.rb +1 -233
  6. data/lib/action_mcp/callbacks.rb +22 -115
  7. data/lib/action_mcp/capability.rb +44 -0
  8. data/lib/action_mcp/configuration.rb +26 -14
  9. data/lib/action_mcp/content/audio.rb +3 -21
  10. data/lib/action_mcp/content/base.rb +7 -0
  11. data/lib/action_mcp/content/binary.rb +30 -0
  12. data/lib/action_mcp/content/image.rb +3 -21
  13. data/lib/action_mcp/content/validation.rb +37 -38
  14. data/lib/action_mcp/content.rb +0 -33
  15. data/lib/action_mcp/custom_methods.rb +61 -0
  16. data/lib/action_mcp/dev_reloader.rb +19 -0
  17. data/lib/action_mcp/lifecycle_callbacks.rb +28 -0
  18. data/lib/action_mcp/log_subscriber.rb +15 -21
  19. data/lib/action_mcp/output_schema_builder.rb +18 -31
  20. data/lib/action_mcp/prompt.rb +10 -61
  21. data/lib/action_mcp/request_meta.rb +11 -0
  22. data/lib/action_mcp/resource_callbacks.rb +22 -149
  23. data/lib/action_mcp/resource_response.rb +3 -7
  24. data/lib/action_mcp/resource_template.rb +1 -0
  25. data/lib/action_mcp/server/base_session.rb +7 -173
  26. data/lib/action_mcp/server/handlers/dispatching.rb +38 -0
  27. data/lib/action_mcp/server/handlers/prompt_handler.rb +6 -12
  28. data/lib/action_mcp/server/handlers/resource_handler.rb +6 -23
  29. data/lib/action_mcp/server/handlers/task_handler.rb +14 -31
  30. data/lib/action_mcp/server/handlers/tool_handler.rb +4 -11
  31. data/lib/action_mcp/server/json_rpc_handler.rb +17 -1
  32. data/lib/action_mcp/server/prompts.rb +8 -17
  33. data/lib/action_mcp/server/resources.rb +4 -28
  34. data/lib/action_mcp/server/result_response.rb +19 -0
  35. data/lib/action_mcp/server/sampling_request.rb +26 -43
  36. data/lib/action_mcp/server/session_registries.rb +127 -0
  37. data/lib/action_mcp/server/tools.rb +5 -26
  38. data/lib/action_mcp/tagged_stream_logging.rb +9 -5
  39. data/lib/action_mcp/tool.rb +25 -60
  40. data/lib/action_mcp/version.rb +1 -1
  41. data/lib/tasks/action_mcp_tasks.rake +22 -44
  42. metadata +10 -3
  43. data/lib/action_mcp/server/handlers/router.rb +0 -33
@@ -6,7 +6,6 @@ module ActionMCP
6
6
  include ActionMCP::Callbacks
7
7
  include ActionMCP::CurrentHelpers
8
8
  class_attribute :_argument_definitions, instance_accessor: false, default: []
9
- class_attribute :_meta, instance_accessor: false, default: {}
10
9
 
11
10
  # ---------------------------------------------------
12
11
  # Prompt Name
@@ -16,47 +15,18 @@ module ActionMCP
16
15
  # @param name [String, nil] The prompt name to set.
17
16
  # @return [String] The prompt name.
18
17
  def self.prompt_name(name = nil)
19
- if name
20
- self._capability_name = name
21
- re_register_if_needed
22
- name
23
- else
24
- _capability_name || default_prompt_name
25
- end
26
- end
27
-
28
- # Re-registers the prompt if it was already registered under a different name
29
- # @return [void]
30
- def self.re_register_if_needed
31
- return if abstract?
32
- return unless _registered_name # Not yet registered
33
-
34
- new_name = capability_name
35
- return if _registered_name == new_name # No change
36
-
37
- ActionMCP::PromptsRegistry.re_register(self, _registered_name)
38
- end
39
-
40
- private_class_method :re_register_if_needed
41
-
42
- # Returns the default prompt name based on the class name.
43
- #
44
- # @return [String] The default prompt name.
45
- def self.default_prompt_name
46
- return "" if name.nil?
47
-
48
- name.underscore.gsub("/", "__").sub(/_prompt$/, "")
18
+ name ? assign_capability_name(name) : capability_name
49
19
  end
50
20
 
51
21
  class << self
52
- alias default_capability_name default_prompt_name
22
+ alias default_prompt_name default_capability_name
53
23
 
54
24
  def type
55
25
  :prompt
56
26
  end
57
27
 
58
- def unregister_from_registry
59
- ActionMCP::PromptsRegistry.unregister(self) if ActionMCP::PromptsRegistry.store.values.include?(self)
28
+ def registry
29
+ ActionMCP::PromptsRegistry
60
30
  end
61
31
 
62
32
  # Hook called when a class inherits from Prompt
@@ -67,17 +37,6 @@ module ActionMCP
67
37
  ActiveSupport.run_load_hooks(:action_mcp_prompt, subclass)
68
38
  end
69
39
  end
70
-
71
- # Sets or retrieves the _meta field
72
- def meta(data = nil)
73
- if data
74
- raise ArgumentError, "_meta must be a hash" unless data.is_a?(Hash)
75
-
76
- self._meta = _meta.merge(data)
77
- else
78
- _meta
79
- end
80
- end
81
40
  end
82
41
 
83
42
  # ---------------------------------------------------
@@ -166,8 +125,8 @@ module ActionMCP
166
125
  run_callbacks :perform do
167
126
  perform # Invoke the subclass-specific logic if valid
168
127
  end
169
- rescue StandardError
170
- # Handle exceptions during execution
128
+ rescue StandardError => e
129
+ Rails.error.report(e, handled: true, severity: :error)
171
130
  @response.mark_as_error!(:internal_error, message: "Unhandled Error executing prompt")
172
131
  end
173
132
  else
@@ -178,21 +137,11 @@ module ActionMCP
178
137
  @response # Return the response with collected messages
179
138
  end
180
139
 
181
- def inspect
182
- attributes_hash = attributes.transform_values(&:inspect)
183
-
184
- response_info = if defined?(@response) && @response
185
- "response: #{@response.messages.size} message(s)"
186
- else
187
- "response: nil"
188
- end
189
-
190
- errors_info = errors.any? ? ", errors: #{errors.full_messages}" : ""
191
-
192
- "#<#{self.class.name} #{attributes_hash.map do |k, v|
193
- "#{k}: #{v.inspect}"
194
- end.join(', ')}, #{response_info}#{errors_info}>"
140
+ # Describes the collected response for #inspect.
141
+ def response_summary
142
+ "#{@response.messages.size} message(s)"
195
143
  end
144
+ private :response_summary
196
145
 
197
146
  # Override render to collect messages
198
147
  def render(**args)
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActionMCP
4
+ module RequestMeta
5
+ def request_meta
6
+ params = execution_context.dig(:request, :params)
7
+ meta = params && (params[:_meta] || params["_meta"])
8
+ meta.is_a?(Hash) ? meta.with_indifferent_access : ActiveSupport::HashWithIndifferentAccess.new
9
+ end
10
+ end
11
+ end
@@ -1,156 +1,29 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "active_support/callbacks"
4
- require "active_support/core_ext/module/attribute_accessors"
5
-
6
3
  module ActionMCP
7
4
  # = Action MCP Resource Template \Callbacks
8
5
  #
9
- # Action MCP Resource Template Callbacks provide hooks during the resource resolution lifecycle.
10
- # These callbacks allow you to trigger logic during the resource resolution process.
11
- # Available callbacks are:
6
+ # Hooks around the +resolve+ method of resource templates:
7
+ # <tt>before_resolve</tt>, <tt>around_resolve</tt> and <tt>after_resolve</tt>.
12
8
  #
13
- # * <tt>before_resolve</tt>
14
- # * <tt>around_resolve</tt>
15
- # * <tt>after_resolve</tt>
16
- module ResourceCallbacks
17
- extend ActiveSupport::Concern
18
- include ActiveSupport::Callbacks
19
-
20
- class << self
21
- include ActiveSupport::Callbacks
22
- define_callbacks :execute
23
- end
24
-
25
- included do
26
- define_callbacks :resolve, skip_after_callbacks_if_terminated: true
27
- end
28
-
29
- # These methods will be included into any Action MCP Resource Template object, adding
30
- # callbacks for the +resolve+ method.
31
- class_methods do
32
- # Defines a callback that will get called right before the
33
- # resource template's resolve method is executed.
34
- #
35
- # class OrdersTemplate < ApplicationMCPResTemplate
36
- # description "Access order information"
37
- # uri_template "ecommerce://customers/{customer_id}/orders/{order_id}"
38
- # mime_type "application/json"
39
- #
40
- # parameter :customer_id,
41
- # description: "Customer identifier",
42
- # required: true
43
- # parameter :order_id,
44
- # description: "Order identifier",
45
- # required: true
46
- #
47
- # before_resolve do |template|
48
- # Rails.logger.info("Starting to resolve order: #{template.order_id} for customer: #{template.customer_id}")
49
- # end
50
- #
51
- # def resolve
52
- # order = MockOrder.find_by(id: order_id)
53
- # return unless order
54
- #
55
- # ActionMCP::Resource.new(
56
- # uri: "ecommerce://orders/#{order_id}",
57
- # name: "Order #{order_id}",
58
- # description: "Order information for order #{order_id}",
59
- # mime_type: "application/json",
60
- # size: order.to_json.length
61
- # )
62
- # end
63
- # end
64
- #
65
- def before_resolve(*filters, &blk)
66
- set_callback(:resolve, :before, *filters, &blk)
67
- end
68
-
69
- # Defines a callback that will get called right after the
70
- # resource template's resolve method has finished.
71
- #
72
- # class OrdersTemplate < ApplicationMCPResTemplate
73
- # description "Access order information"
74
- # uri_template "ecommerce://customers/{customer_id}/orders/{order_id}"
75
- # mime_type "application/json"
76
- #
77
- # parameter :customer_id,
78
- # description: "Customer identifier",
79
- # required: true
80
- # parameter :order_id,
81
- # description: "Order identifier",
82
- # required: true
83
- #
84
- # after_resolve do |template|
85
- # Rails.logger.info("Finished resolving order resource for order: #{template.order_id}")
86
- # end
87
- #
88
- # def resolve
89
- # order = MockOrder.find_by(id: order_id)
90
- # return unless order
91
- #
92
- # ActionMCP::Resource.new(
93
- # uri: "ecommerce://orders/#{order_id}",
94
- # name: "Order #{order_id}",
95
- # description: "Order information for order #{order_id}",
96
- # mime_type: "application/json",
97
- # size: order.to_json.length
98
- # )
99
- # end
100
- # end
101
- #
102
- def after_resolve(*filters, &blk)
103
- set_callback(:resolve, :after, *filters, &blk)
104
- end
105
-
106
- # Defines a callback that will get called around the resource template's resolve method.
107
- #
108
- # class OrdersTemplate < ApplicationMCPResTemplate
109
- # description "Access order information"
110
- # uri_template "ecommerce://customers/{customer_id}/orders/{order_id}"
111
- # mime_type "application/json"
112
- #
113
- # parameter :customer_id,
114
- # description: "Customer identifier",
115
- # required: true
116
- # parameter :order_id,
117
- # description: "Order identifier",
118
- # required: true
119
- #
120
- # around_resolve do |template, block|
121
- # start_time = Time.current
122
- # Rails.logger.info("Starting resolution for order: #{template.order_id}")
123
- #
124
- # resource = block.call
125
- #
126
- # if resource
127
- # Rails.logger.info("Order #{template.order_id} resolved successfully in #{Time.current - start_time}s")
128
- # else
129
- # Rails.logger.info("Order #{template.order_id} not found")
130
- # end
131
- #
132
- # resource
133
- # end
134
- #
135
- # def resolve
136
- # order = MockOrder.find_by(id: order_id)
137
- # return unless order
138
- #
139
- # ActionMCP::Resource.new(
140
- # uri: "ecommerce://orders/#{order_id}",
141
- # name: "Order #{order_id}",
142
- # description: "Order information for order #{order_id}",
143
- # mime_type: "application/json",
144
- # size: order.to_json.length
145
- # )
146
- # end
147
- # end
148
- #
149
- # You can access the return value of the resolve method as shown above.
150
- #
151
- def around_resolve(*filters, &blk)
152
- set_callback(:resolve, :around, *filters, &blk)
153
- end
154
- end
155
- end
9
+ # class OrdersTemplate < ApplicationMCPResTemplate
10
+ # uri_template "ecommerce://customers/{customer_id}/orders/{order_id}"
11
+ #
12
+ # before_resolve do |template|
13
+ # Rails.logger.info("Resolving order #{template.order_id}")
14
+ # end
15
+ #
16
+ # around_resolve do |template, block|
17
+ # resource = block.call
18
+ # Rails.logger.info("Order #{template.order_id} not found") unless resource
19
+ # resource
20
+ # end
21
+ #
22
+ # after_resolve do |template|
23
+ # Rails.logger.info("Resolved order #{template.order_id}")
24
+ # end
25
+ # end
26
+ #
27
+ # The around callback receives the value returned by +resolve+.
28
+ ResourceCallbacks = LifecycleCallbacks.for(:resolve)
156
29
  end
@@ -82,13 +82,9 @@ module ActionMCP
82
82
 
83
83
  # Override to_h to use -32002 for resource not found (consistent with send_resource_read)
84
84
  def to_h(_options = nil)
85
- if @is_error && @symbol.nil?
86
- { code: -32_002, message: @error_message, data: @error_data }
87
- elsif @is_error
88
- JSON_RPC::JsonRpcError.new(@symbol, message: @error_message, data: @error_data).to_h
89
- else
90
- build_success_hash
91
- end
85
+ return { code: -32_002, message: @error_message, data: @error_data } if @is_error && @symbol.nil?
86
+
87
+ super
92
88
  end
93
89
 
94
90
  # Implementation of build_success_hash for ResourceResponse
@@ -12,6 +12,7 @@ module ActionMCP
12
12
  include ResourceCallbacks
13
13
  include UriAmbiguityChecker
14
14
  include CurrentHelpers
15
+ include RequestMeta
15
16
 
16
17
  # Track all registered templates
17
18
  @registered_templates = []
@@ -4,6 +4,8 @@ module ActionMCP
4
4
  module Server
5
5
  # Base session object that mimics ActiveRecord Session with common functionality
6
6
  class BaseSession
7
+ include SessionRegistries
8
+
7
9
  attr_accessor :id, :status, :initialized, :role, :messages_count,
8
10
  :protocol_version, :client_info,
9
11
  :client_capabilities, :server_info, :server_capabilities,
@@ -22,9 +24,7 @@ module ActionMCP
22
24
  @consents = {}
23
25
  @session_data = {}
24
26
 
25
- attributes.each do |key, value|
26
- send("#{key}=", value) if respond_to?("#{key}=")
27
- end
27
+ assign_known_attributes(attributes)
28
28
  end
29
29
 
30
30
  # ActiveRecord-like interface
@@ -48,9 +48,7 @@ module ActionMCP
48
48
  end
49
49
 
50
50
  def update(attributes)
51
- attributes.each do |key, value|
52
- send("#{key}=", value) if respond_to?("#{key}=")
53
- end
51
+ assign_known_attributes(attributes)
54
52
  save
55
53
  end
56
54
 
@@ -77,14 +75,6 @@ module ActionMCP
77
75
  save
78
76
  end
79
77
 
80
- def initialize!
81
- return false unless status == "initializing" && !initialized?
82
-
83
- self.initialized = true
84
- self.status = "initialized"
85
- save
86
- end
87
-
88
78
  def close!
89
79
  self.status = "closed"
90
80
  self.ended_at = Time.current
@@ -185,111 +175,6 @@ module ActionMCP
185
175
  @subscriptions.any? { |subscription| subscription[:uri] == uri }
186
176
  end
187
177
 
188
- # Progress notification
189
- def send_progress_notification(progressToken:, progress:, total: nil, message: nil)
190
- handler = ActionMCP::Server::TransportHandler.new(self)
191
- handler.send_progress_notification(
192
- progressToken: progressToken,
193
- progress: progress,
194
- total: total,
195
- message: message
196
- )
197
- end
198
-
199
- # Registry management methods
200
- def register_tool(tool_class_or_name)
201
- tool_name = normalize_name(tool_class_or_name, :tool)
202
- return false unless tool_exists?(tool_name)
203
-
204
- self.tool_registry ||= []
205
- unless self.tool_registry.include?(tool_name)
206
- self.tool_registry << tool_name
207
- save!
208
- send_tools_list_changed_notification
209
- end
210
- true
211
- end
212
-
213
- def unregister_tool(tool_class_or_name)
214
- tool_name = normalize_name(tool_class_or_name, :tool)
215
- self.tool_registry ||= []
216
-
217
- return unless self.tool_registry.delete(tool_name)
218
-
219
- save!
220
- send_tools_list_changed_notification
221
- end
222
-
223
- def register_prompt(prompt_class_or_name)
224
- prompt_name = normalize_name(prompt_class_or_name, :prompt)
225
- return false unless prompt_exists?(prompt_name)
226
-
227
- self.prompt_registry ||= []
228
- unless self.prompt_registry.include?(prompt_name)
229
- self.prompt_registry << prompt_name
230
- save!
231
- send_prompts_list_changed_notification
232
- end
233
- true
234
- end
235
-
236
- def unregister_prompt(prompt_class_or_name)
237
- prompt_name = normalize_name(prompt_class_or_name, :prompt)
238
- self.prompt_registry ||= []
239
-
240
- return unless self.prompt_registry.delete(prompt_name)
241
-
242
- save!
243
- send_prompts_list_changed_notification
244
- end
245
-
246
- def register_resource_template(template_class_or_name)
247
- template_name = normalize_name(template_class_or_name, :resource_template)
248
- return false unless resource_template_exists?(template_name)
249
-
250
- self.resource_registry ||= []
251
- unless self.resource_registry.include?(template_name)
252
- self.resource_registry << template_name
253
- save!
254
- send_resources_list_changed_notification
255
- end
256
- true
257
- end
258
-
259
- def unregister_resource_template(template_class_or_name)
260
- template_name = normalize_name(template_class_or_name, :resource_template)
261
- self.resource_registry ||= []
262
-
263
- return unless self.resource_registry.delete(template_name)
264
-
265
- save!
266
- send_resources_list_changed_notification
267
- end
268
-
269
- def registered_tools
270
- (self.tool_registry || []).filter_map do |tool_name|
271
- ActionMCP::ToolsRegistry.find(tool_name)
272
- rescue StandardError
273
- nil
274
- end
275
- end
276
-
277
- def registered_prompts
278
- (self.prompt_registry || []).filter_map do |prompt_name|
279
- ActionMCP::PromptsRegistry.find(prompt_name)
280
- rescue StandardError
281
- nil
282
- end
283
- end
284
-
285
- def registered_resource_templates
286
- (self.resource_registry || []).filter_map do |template_name|
287
- ActionMCP::ResourceTemplatesRegistry.find(template_name)
288
- rescue StandardError
289
- nil
290
- end
291
- end
292
-
293
178
  # Consent management methods
294
179
  def consent_granted_for?(key)
295
180
  consents_hash = consents.is_a?(String) ? JSON.parse(consents) : consents
@@ -402,63 +287,12 @@ module ActionMCP
402
287
  capabilities ? capabilities.deep_dup : {}
403
288
  end
404
289
 
405
- def normalize_name(class_or_name, type)
406
- case class_or_name
407
- when String
408
- class_or_name
409
- when Class
410
- case type
411
- when :tool
412
- class_or_name.tool_name
413
- when :prompt
414
- class_or_name.prompt_name
415
- when :resource_template
416
- class_or_name.capability_name
417
- end
418
- else
419
- raise ArgumentError, "Expected String or Class, got #{class_or_name.class}"
290
+ def assign_known_attributes(attributes)
291
+ attributes.each do |key, value|
292
+ send("#{key}=", value) if respond_to?("#{key}=")
420
293
  end
421
294
  end
422
295
 
423
- def tool_exists?(tool_name)
424
- ActionMCP::ToolsRegistry.find(tool_name)
425
- true
426
- rescue ActionMCP::RegistryBase::NotFound
427
- false
428
- end
429
-
430
- def prompt_exists?(prompt_name)
431
- ActionMCP::PromptsRegistry.find(prompt_name)
432
- true
433
- rescue ActionMCP::RegistryBase::NotFound
434
- false
435
- end
436
-
437
- def resource_template_exists?(template_name)
438
- ActionMCP::ResourceTemplatesRegistry.find(template_name)
439
- true
440
- rescue ActionMCP::RegistryBase::NotFound
441
- false
442
- end
443
-
444
- def send_tools_list_changed_notification
445
- return unless server_capabilities.dig("tools", "listChanged")
446
-
447
- write(JSON_RPC::Notification.new(method: "notifications/tools/list_changed"))
448
- end
449
-
450
- def send_prompts_list_changed_notification
451
- return unless server_capabilities.dig("prompts", "listChanged")
452
-
453
- write(JSON_RPC::Notification.new(method: "notifications/prompts/list_changed"))
454
- end
455
-
456
- def send_resources_list_changed_notification
457
- return unless server_capabilities.dig("resources", "listChanged")
458
-
459
- write(JSON_RPC::Notification.new(method: "notifications/resources/list_changed"))
460
- end
461
-
462
296
  # Collection classes
463
297
  class MessageCollection < Array
464
298
  def create!(attributes)
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActionMCP
4
+ module Server
5
+ module Handlers
6
+ # Shared plumbing for the method-family handlers (prompts/, resources/, tools/, tasks/).
7
+ module Dispatching
8
+ private
9
+
10
+ def require_params_object!(params, label)
11
+ return if params.is_a?(Hash)
12
+
13
+ raise JSON_RPC::JsonRpcError.new(:invalid_params, message: "#{label} params must be an object")
14
+ end
15
+
16
+ def dispatch_rpc(handlers, family, rpc_method, id, params)
17
+ handler = handlers[rpc_method]
18
+ unless handler
19
+ Rails.logger.warn("Unknown #{family} method: #{rpc_method}")
20
+ raise JSON_RPC::JsonRpcError.new(:method_not_found, message: "Unknown #{family} method: #{rpc_method}")
21
+ end
22
+
23
+ send(handler, id, params)
24
+ end
25
+
26
+ def negotiated_capabilities(key)
27
+ capabilities = (transport.session.server_capabilities || {}).with_indifferent_access
28
+ value = capabilities[key]
29
+ value.with_indifferent_access if value.is_a?(Hash)
30
+ end
31
+
32
+ def method_not_available!(message)
33
+ raise JSON_RPC::JsonRpcError.new(:method_not_found, message: message)
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -5,22 +5,15 @@ module ActionMCP
5
5
  module Handlers
6
6
  module PromptHandler
7
7
  include ErrorAware
8
+ include Dispatching
8
9
 
9
10
  def process_prompts(rpc_method, id, params)
10
11
  params ||= {}
11
12
 
12
13
  with_error_handling(id) do
13
- unless params.is_a?(Hash)
14
- raise JSON_RPC::JsonRpcError.new(:invalid_params, message: "Prompt params must be an object")
15
- end
16
-
17
- handler = prompt_method_handlers[rpc_method]
18
- if handler
19
- send(handler, id, params)
20
- else
21
- Rails.logger.warn("Unknown prompts method: #{rpc_method}")
22
- raise JSON_RPC::JsonRpcError.new(:method_not_found, message: "Unknown prompts method: #{rpc_method}")
23
- end
14
+ require_params_object!(params, "Prompt")
15
+
16
+ dispatch_rpc(prompt_method_handlers, "prompts", rpc_method, id, params)
24
17
  end
25
18
  end
26
19
 
@@ -43,7 +36,8 @@ module ActionMCP
43
36
  raise JSON_RPC::JsonRpcError.new(:invalid_params, message: "Prompt argument values must be strings")
44
37
  end
45
38
 
46
- transport.send_prompts_get(id, name, arguments)
39
+ _meta = params["_meta"] || params[:_meta] || {}
40
+ transport.send_prompts_get(id, name, arguments, _meta)
47
41
  end
48
42
 
49
43
  def handle_prompts_list(id, params)
@@ -7,33 +7,22 @@ module ActionMCP
7
7
  module Handlers
8
8
  module ResourceHandler
9
9
  include ErrorAware
10
+ include Dispatching
10
11
 
11
12
  def process_resources(rpc_method, id, params)
12
13
  params ||= {}
13
14
 
14
15
  with_error_handling(id) do
15
- unless params.is_a?(Hash)
16
- raise JSON_RPC::JsonRpcError.new(:invalid_params, message: "Resource params must be an object")
17
- end
16
+ require_params_object!(params, "Resource")
18
17
 
19
- capabilities = negotiated_resource_capabilities
20
- unless capabilities
21
- raise JSON_RPC::JsonRpcError.new(:method_not_found,
22
- message: "Resources are not available for this session")
23
- end
18
+ capabilities = negotiated_capabilities(:resources)
19
+ method_not_available!("Resources are not available for this session") unless capabilities
24
20
 
25
21
  if subscription_method?(rpc_method) && capabilities[:subscribe] != true
26
- raise JSON_RPC::JsonRpcError.new(:method_not_found,
27
- message: "Resource subscriptions are not available for this session")
22
+ method_not_available!("Resource subscriptions are not available for this session")
28
23
  end
29
24
 
30
- handler = resource_method_handlers[rpc_method]
31
- if handler
32
- send(handler, id, params)
33
- else
34
- Rails.logger.warn("Unknown resources method: #{rpc_method}")
35
- raise JSON_RPC::JsonRpcError.new(:method_not_found, message: "Unknown resources method: #{rpc_method}")
36
- end
25
+ dispatch_rpc(resource_method_handlers, "resources", rpc_method, id, params)
37
26
  end
38
27
  end
39
28
 
@@ -49,12 +38,6 @@ module ActionMCP
49
38
  }
50
39
  end
51
40
 
52
- def negotiated_resource_capabilities
53
- capabilities = (transport.session.server_capabilities || {}).with_indifferent_access
54
- resource_capabilities = capabilities[:resources]
55
- resource_capabilities.with_indifferent_access if resource_capabilities.is_a?(Hash)
56
- end
57
-
58
41
  def subscription_method?(rpc_method)
59
42
  rpc_method == JsonRpcHandlerBase::Methods::RESOURCES_SUBSCRIBE ||
60
43
  rpc_method == JsonRpcHandlerBase::Methods::RESOURCES_UNSUBSCRIBE