actionmcp 0.201.3 → 0.203.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 (47) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +51 -0
  3. data/app/controllers/action_mcp/application_controller.rb +21 -36
  4. data/app/jobs/action_mcp/tool_execution_job.rb +57 -27
  5. data/app/models/action_mcp/session/task.rb +2 -1
  6. data/app/models/action_mcp/session.rb +1 -233
  7. data/lib/action_mcp/callbacks.rb +22 -115
  8. data/lib/action_mcp/capability.rb +44 -0
  9. data/lib/action_mcp/configuration.rb +26 -14
  10. data/lib/action_mcp/content/audio.rb +3 -21
  11. data/lib/action_mcp/content/base.rb +7 -0
  12. data/lib/action_mcp/content/binary.rb +30 -0
  13. data/lib/action_mcp/content/image.rb +3 -21
  14. data/lib/action_mcp/content/validation.rb +37 -38
  15. data/lib/action_mcp/content.rb +0 -33
  16. data/lib/action_mcp/custom_methods.rb +61 -0
  17. data/lib/action_mcp/dev_reloader.rb +19 -0
  18. data/lib/action_mcp/lifecycle_callbacks.rb +28 -0
  19. data/lib/action_mcp/log_subscriber.rb +15 -21
  20. data/lib/action_mcp/output_schema_builder.rb +18 -31
  21. data/lib/action_mcp/prompt.rb +8 -59
  22. data/lib/action_mcp/request_meta.rb +11 -0
  23. data/lib/action_mcp/resource_callbacks.rb +22 -149
  24. data/lib/action_mcp/resource_response.rb +3 -7
  25. data/lib/action_mcp/resource_template.rb +1 -0
  26. data/lib/action_mcp/schema_context.rb +50 -0
  27. data/lib/action_mcp/server/base_session.rb +7 -173
  28. data/lib/action_mcp/server/handlers/dispatching.rb +38 -0
  29. data/lib/action_mcp/server/handlers/prompt_handler.rb +6 -12
  30. data/lib/action_mcp/server/handlers/resource_handler.rb +6 -23
  31. data/lib/action_mcp/server/handlers/task_handler.rb +14 -31
  32. data/lib/action_mcp/server/handlers/tool_handler.rb +4 -11
  33. data/lib/action_mcp/server/json_rpc_handler.rb +17 -1
  34. data/lib/action_mcp/server/prompts.rb +8 -17
  35. data/lib/action_mcp/server/resources.rb +4 -28
  36. data/lib/action_mcp/server/result_response.rb +19 -0
  37. data/lib/action_mcp/server/sampling_request.rb +26 -43
  38. data/lib/action_mcp/server/session_registries.rb +127 -0
  39. data/lib/action_mcp/server/tools.rb +23 -33
  40. data/lib/action_mcp/server/transport_handler.rb +0 -6
  41. data/lib/action_mcp/tagged_stream_logging.rb +9 -5
  42. data/lib/action_mcp/tool.rb +67 -71
  43. data/lib/action_mcp/version.rb +1 -1
  44. data/lib/action_mcp.rb +4 -0
  45. data/lib/tasks/action_mcp_tasks.rake +22 -44
  46. metadata +11 -3
  47. data/lib/action_mcp/server/handlers/router.rb +0 -33
@@ -1,122 +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 \Callbacks
8
5
  #
9
- # Action MCP provides hooks during the life cycle of a message, command, or process.
10
- # Callbacks allow you to trigger logic during this cycle. Available callbacks are:
6
+ # Hooks around the +perform+ method of tools and prompts:
7
+ # <tt>before_perform</tt>, <tt>around_perform</tt> and <tt>after_perform</tt>.
11
8
  #
12
- # * <tt>before_perform</tt>
13
- # * <tt>around_perform</tt>
14
- # * <tt>after_perform</tt>
15
- module Callbacks
16
- extend ActiveSupport::Concern
17
- include ActiveSupport::Callbacks
18
-
19
- class << self
20
- include ActiveSupport::Callbacks
21
- define_callbacks :execute
22
- end
23
-
24
- included do
25
- define_callbacks :perform, skip_after_callbacks_if_terminated: true
26
- end
27
-
28
- # These methods will be included into any Action MCP capability, adding
29
- # callbacks for the +perform+ method.
30
- class_methods do
31
- # Defines a callback that will get called right before the
32
- # object's perform method is executed.
33
- #
34
- # class AnalyzeCsvTool < ApplicationMCPTool
35
- # description "Analyze a CSV file"
36
- #
37
- # property :filepath, type: "string", description: "Path to CSV file"
38
- # collection :operations, type: "string", description: "Operations to perform"
39
- #
40
- # validates :operations, inclusion: { in: %w[sum average count] }
41
- #
42
- # before_perform do |mcp|
43
- # Rails.logger.info("Starting CSV analysis for: #{mcp.filepath}")
44
- # end
45
- #
46
- # def perform
47
- # result = operations.to_h { |op| [ op, rand(1..100) ] }
48
- # render text: result.to_json
49
- # end
50
- # end
51
- #
52
- def before_perform(*filters, &blk)
53
- set_callback(:perform, :before, *filters, &blk)
54
- end
55
-
56
- # Defines a callback that will get called right after the
57
- # object's perform method has finished.
58
- #
59
- # class GreetingPrompt < ApplicationMCPPrompt
60
- # description "Generates a personalized greeting message"
61
- #
62
- # argument :name, description: "The name to greet", required: true
63
- # argument :style, description: "Style of greeting", enum: %w[formal casual friendly], default: "friendly"
64
- #
65
- # after_perform do |mcp|
66
- # Rails.logger.info("Generated #{mcp.style} greeting for #{mcp.name}")
67
- # end
68
- #
69
- # def perform
70
- # render text: "Please create a greeting for #{name}"
71
- # render text: "I'd be happy to create a #{style} greeting for #{name}!", role: "assistant"
72
- # render text: "The greeting should be in #{style} style."
73
- # end
74
- # end
75
- #
76
- def after_perform(*filters, &blk)
77
- set_callback(:perform, :after, *filters, &blk)
78
- end
79
-
80
- # Defines a callback that will get called around the object's perform method.
81
- #
82
- # class AnalyzeCsvTool < ApplicationMCPTool
83
- # description "Analyze a CSV file"
84
- #
85
- # property :filepath, type: "string", description: "Path to CSV file"
86
- # collection :operations, type: "string", description: "Operations to perform"
87
- #
88
- # validates :operations, inclusion: { in: %w[sum average count] }
89
- #
90
- # around_perform do |mcp, block|
91
- # start_time = Time.current
92
- # Rails.logger.info("Starting CSV analysis for: #{mcp.filepath}")
93
- # block.call
94
- # duration = Time.current - start_time
95
- # Rails.logger.info("Completed CSV analysis in #{duration}s")
96
- # end
97
- #
98
- # def perform
99
- # result = operations.to_h { |op| [ op, rand(1..100) ] }
100
- # render text: result.to_json
101
- # end
102
- # end
103
- #
104
- # You can access the return value of the perform only if the execution wasn't halted.
105
- #
106
- # class GreetingPrompt < ApplicationMCPPrompt
107
- # around_perform do |mcp, block|
108
- # value = block.call
109
- # puts value # => Result of render operations
110
- # end
111
- #
112
- # def perform
113
- # render text: "Hello #{name}!"
114
- # end
115
- # end
116
- #
117
- def around_perform(*filters, &blk)
118
- set_callback(:perform, :around, *filters, &blk)
119
- end
120
- end
121
- end
9
+ # class AnalyzeCsvTool < ApplicationMCPTool
10
+ # property :filepath, type: "string", description: "Path to CSV file"
11
+ #
12
+ # before_perform do |mcp|
13
+ # Rails.logger.info("Starting CSV analysis for: #{mcp.filepath}")
14
+ # end
15
+ #
16
+ # around_perform do |mcp, block|
17
+ # started = Time.current
18
+ # block.call
19
+ # Rails.logger.info("Analysis took #{Time.current - started}s")
20
+ # end
21
+ #
22
+ # after_perform do |mcp|
23
+ # Rails.logger.info("Finished CSV analysis for: #{mcp.filepath}")
24
+ # end
25
+ # end
26
+ #
27
+ # A callback that halts the chain skips the after callbacks.
28
+ Callbacks = LifecycleCallbacks.for(:perform)
122
29
  end
@@ -9,10 +9,12 @@ module ActionMCP
9
9
  include Callbacks
10
10
  include Instrumentation::Instrumentation
11
11
  include Renderable
12
+ include RequestMeta
12
13
 
13
14
  class_attribute :_capability_name, instance_accessor: false
14
15
  class_attribute :_registered_name, instance_accessor: false
15
16
  class_attribute :_description, instance_accessor: false, default: ""
17
+ class_attribute :_meta, instance_accessor: false, default: {}
16
18
 
17
19
  attr_reader :execution_context
18
20
 
@@ -30,6 +32,14 @@ module ActionMCP
30
32
  execution_context[:session]
31
33
  end
32
34
 
35
+ def inspect
36
+ attribute_list = attributes.transform_values(&:inspect).map { |k, v| "#{k}: #{v.inspect}" }.join(", ")
37
+ response_info = defined?(@response) && @response ? "response: #{response_summary}" : "response: nil"
38
+ errors_info = errors.any? ? ", errors: #{errors.full_messages}" : ""
39
+
40
+ "#<#{self.class.name} #{attribute_list}, #{response_info}#{errors_info}>"
41
+ end
42
+
33
43
  delegate :session_data, to: :session, allow_nil: true
34
44
 
35
45
  # Returns true when the connected client advertises the MCP Apps UI extension
@@ -76,5 +86,39 @@ module ActionMCP
76
86
  _description
77
87
  end
78
88
  end
89
+
90
+ # Sets or retrieves the _meta field
91
+ def self.meta(data = nil)
92
+ return _meta unless data
93
+ raise ArgumentError, "_meta must be a hash" unless data.is_a?(Hash)
94
+
95
+ self._meta = _meta.merge(data)
96
+ end
97
+
98
+ # Name derived from the class: Admin::ReportTool => "admin__report".
99
+ def self.default_capability_name
100
+ return "" if name.nil?
101
+
102
+ name.underscore.gsub("/", "__").sub(/_#{type}$/, "")
103
+ end
104
+
105
+ def self.unregister_from_registry
106
+ registry.unregister(self) if registry.store.values.include?(self)
107
+ end
108
+
109
+ def self.assign_capability_name(name)
110
+ self._capability_name = name
111
+ re_register_if_needed
112
+ name
113
+ end
114
+
115
+ # Re-registers under the new name when the class was registered under an older one.
116
+ def self.re_register_if_needed
117
+ return if abstract? || !_registered_name || _registered_name == capability_name
118
+
119
+ registry.re_register(self, _registered_name)
120
+ end
121
+
122
+ private_class_method :assign_capability_name, :re_register_if_needed
79
123
  end
80
124
  end
@@ -2,6 +2,7 @@
2
2
 
3
3
  require "monitor"
4
4
  require_relative "gateway"
5
+ require_relative "custom_methods"
5
6
 
6
7
  module ActionMCP
7
8
  # Configuration class to hold settings for the ActionMCP server.
@@ -109,9 +110,21 @@ module ActionMCP
109
110
  # explicitly add their trusted origin hosts.
110
111
  @allowed_origins = %w[localhost 127.0.0.1 ::1].freeze
111
112
 
113
+ @custom_methods = CustomMethods.new
114
+
112
115
  @mcp_components_monitor = Monitor.new
113
116
  end
114
117
 
118
+ # Vendor JSON-RPC methods registered with #register_method.
119
+ # @return [ActionMCP::CustomMethods]
120
+ attr_reader :custom_methods
121
+
122
+ # Serves a vendor JSON-RPC method ("vendor/method") next to the MCP core methods.
123
+ # The handler is called with (params, session) and its return value is the result.
124
+ def register_method(name, callable = nil, &block)
125
+ custom_methods.register(name, callable, &block)
126
+ end
127
+
115
128
  def name
116
129
  @name || Rails.application.name
117
130
  end
@@ -255,28 +268,17 @@ module ActionMCP
255
268
 
256
269
  # Filter tools based on active profile
257
270
  def filtered_tools
258
- return ToolsRegistry.non_abstract if should_include_all?(:tools)
259
-
260
- tool_names = @profiles[active_profile][:tools] || []
261
- # Convert tool names to underscored format
262
- tool_names = tool_names.map { |name| name.to_s.underscore }
263
- ToolsRegistry.non_abstract.select { |tool| tool_names.include?(tool.name.underscore) }
271
+ filter_by_profile(ToolsRegistry, :tools) { |name| name.to_s.underscore }
264
272
  end
265
273
 
266
274
  # Filter prompts based on active profile
267
275
  def filtered_prompts
268
- return PromptsRegistry.non_abstract if should_include_all?(:prompts)
269
-
270
- prompt_names = @profiles[active_profile][:prompts] || []
271
- PromptsRegistry.non_abstract.select { |prompt| prompt_names.include?(prompt.name) }
276
+ filter_by_profile(PromptsRegistry, :prompts)
272
277
  end
273
278
 
274
279
  # Filter resources based on active profile
275
280
  def filtered_resources
276
- return ResourceTemplatesRegistry.non_abstract if should_include_all?(:resources)
277
-
278
- resource_names = @profiles[active_profile][:resources] || []
279
- ResourceTemplatesRegistry.non_abstract.select { |resource| resource_names.include?(resource.name) }
281
+ filter_by_profile(ResourceTemplatesRegistry, :resources)
280
282
  end
281
283
 
282
284
  # Returns capabilities based on active profile
@@ -477,6 +479,16 @@ module ActionMCP
477
479
  end
478
480
  end
479
481
 
482
+ # Non-abstract entries of +registry+ listed under +key+ in the active profile.
483
+ # The block normalizes names on both sides before comparing.
484
+ def filter_by_profile(registry, key, &normalize)
485
+ return registry.non_abstract if should_include_all?(key)
486
+
487
+ normalize ||= :itself.to_proc
488
+ names = (@profiles[active_profile][key] || []).map(&normalize)
489
+ registry.non_abstract.select { |item| names.include?(normalize.call(item.name)) }
490
+ end
491
+
480
492
  def should_include_all?(type)
481
493
  return false unless @profiles[active_profile]
482
494
 
@@ -2,31 +2,13 @@
2
2
 
3
3
  module ActionMCP
4
4
  module Content
5
- # Audio content includes a base64-encoded audio clip and its MIME type.
6
- class Audio < Base
7
- # @return [String] The base64-encoded audio data.
8
- # @return [String] The MIME type of the audio data.
9
- attr_reader :data, :mime_type
10
-
11
- # Initializes a new Audio content.
12
- #
5
+ # Audio content includes base64-encoded audio data and its MIME type.
6
+ class Audio < Binary
13
7
  # @param data [String] The base64-encoded audio data.
14
8
  # @param mime_type [String] The MIME type of the audio data.
15
9
  # @param annotations [Hash, nil] Optional annotations for the audio content.
16
10
  def initialize(data, mime_type, annotations: nil)
17
- super("audio", annotations: annotations)
18
- @data = data
19
- @mime_type = mime_type
20
- to_h
21
- end
22
-
23
- # Returns a hash representation of the audio content.
24
- #
25
- # @return [Hash] The hash representation of the audio content.
26
- def to_h
27
- super.merge(data: @data, mimeType: @mime_type).tap do |result|
28
- Validation.validate_content_block!(result)
29
- end
11
+ super("audio", data, mime_type, annotations: annotations)
30
12
  end
31
13
  end
32
14
  end
@@ -25,6 +25,13 @@ module ActionMCP
25
25
  Validation.validate_annotations!(@annotations)
26
26
  h
27
27
  end
28
+
29
+ # Returns a JSON representation of the content.
30
+ #
31
+ # @return [String] The JSON representation.
32
+ def to_json(*)
33
+ MultiJSON.generate(to_h, *)
34
+ end
28
35
  end
29
36
  end
30
37
  end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActionMCP
4
+ module Content
5
+ # Base64-encoded media content (image, audio) with its MIME type.
6
+ class Binary < Base
7
+ # @return [String] The base64-encoded data.
8
+ # @return [String] The MIME type of the data.
9
+ attr_reader :data, :mime_type
10
+
11
+ # @param type [String] The content type ("image", "audio").
12
+ # @param data [String] The base64-encoded data.
13
+ # @param mime_type [String] The MIME type of the data.
14
+ # @param annotations [Hash, nil] Optional annotations for the content.
15
+ def initialize(type, data, mime_type, annotations: nil)
16
+ super(type, annotations: annotations)
17
+ @data = data
18
+ @mime_type = mime_type
19
+ to_h
20
+ end
21
+
22
+ # @return [Hash] The validated content block.
23
+ def to_h
24
+ super.merge(data: @data, mimeType: @mime_type).tap do |result|
25
+ Validation.validate_content_block!(result)
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -2,31 +2,13 @@
2
2
 
3
3
  module ActionMCP
4
4
  module Content
5
- # Image content includes a base64-encoded image and its MIME type.
6
- class Image < Base
7
- # @return [String] The base64-encoded image data.
8
- # @return [String] The MIME type of the image data.
9
- attr_reader :data, :mime_type
10
-
11
- # Initializes a new Image content.
12
- #
5
+ # Image content includes base64-encoded image data and its MIME type.
6
+ class Image < Binary
13
7
  # @param data [String] The base64-encoded image data.
14
8
  # @param mime_type [String] The MIME type of the image data.
15
9
  # @param annotations [Hash, nil] Optional annotations for the image content.
16
10
  def initialize(data, mime_type, annotations: nil)
17
- super("image", annotations: annotations)
18
- @data = data
19
- @mime_type = mime_type
20
- to_h
21
- end
22
-
23
- # Returns a hash representation of the image content.
24
- #
25
- # @return [Hash] The hash representation of the image content.
26
- def to_h
27
- super.merge(data: @data, mimeType: @mime_type).tap do |result|
28
- Validation.validate_content_block!(result)
29
- end
11
+ super("image", data, mime_type, annotations: annotations)
30
12
  end
31
13
  end
32
14
  end
@@ -14,6 +14,29 @@ module ActionMCP
14
14
  "_meta" => { "type" => "object" }
15
15
  }.freeze
16
16
 
17
+ def self.ref(name)
18
+ { "$ref" => "#/$defs/#{name}" }
19
+ end
20
+
21
+ def self.refs(*names)
22
+ names.map { |name| ref(name) }
23
+ end
24
+
25
+ ANNOTATED_PROPERTIES = META_PROPERTY.merge("annotations" => ref("Annotations")).freeze
26
+
27
+ # Base64 media block (ImageContent, AudioContent).
28
+ def self.media_content(type)
29
+ {
30
+ "type" => "object",
31
+ "required" => %w[data mimeType type],
32
+ "properties" => ANNOTATED_PROPERTIES.merge(
33
+ "data" => { "type" => "string", "format" => "byte" },
34
+ "mimeType" => { "type" => "string" },
35
+ "type" => { "type" => "string", "const" => type }
36
+ )
37
+ }
38
+ end
39
+
17
40
  ANNOTATIONS = {
18
41
  "type" => "object",
19
42
  "properties" => {
@@ -43,32 +66,13 @@ module ActionMCP
43
66
  "TextContent" => {
44
67
  "type" => "object",
45
68
  "required" => %w[text type],
46
- "properties" => META_PROPERTY.merge(
47
- "annotations" => { "$ref" => "#/$defs/Annotations" },
69
+ "properties" => ANNOTATED_PROPERTIES.merge(
48
70
  "text" => { "type" => "string" },
49
71
  "type" => { "type" => "string", "const" => "text" }
50
72
  )
51
73
  },
52
- "ImageContent" => {
53
- "type" => "object",
54
- "required" => %w[data mimeType type],
55
- "properties" => META_PROPERTY.merge(
56
- "annotations" => { "$ref" => "#/$defs/Annotations" },
57
- "data" => { "type" => "string", "format" => "byte" },
58
- "mimeType" => { "type" => "string" },
59
- "type" => { "type" => "string", "const" => "image" }
60
- )
61
- },
62
- "AudioContent" => {
63
- "type" => "object",
64
- "required" => %w[data mimeType type],
65
- "properties" => META_PROPERTY.merge(
66
- "annotations" => { "$ref" => "#/$defs/Annotations" },
67
- "data" => { "type" => "string", "format" => "byte" },
68
- "mimeType" => { "type" => "string" },
69
- "type" => { "type" => "string", "const" => "audio" }
70
- )
71
- },
74
+ "ImageContent" => media_content("image"),
75
+ "AudioContent" => media_content("audio"),
72
76
  "TextResourceContents" => {
73
77
  "type" => "object",
74
78
  "required" => %w[text uri],
@@ -90,12 +94,11 @@ module ActionMCP
90
94
  "EmbeddedResource" => {
91
95
  "type" => "object",
92
96
  "required" => %w[resource type],
93
- "properties" => META_PROPERTY.merge(
94
- "annotations" => { "$ref" => "#/$defs/Annotations" },
97
+ "properties" => ANNOTATED_PROPERTIES.merge(
95
98
  "resource" => {
96
99
  "anyOf" => [
97
- { "$ref" => "#/$defs/TextResourceContents" },
98
- { "$ref" => "#/$defs/BlobResourceContents" }
100
+ ref("TextResourceContents"),
101
+ ref("BlobResourceContents")
99
102
  ]
100
103
  },
101
104
  "type" => { "type" => "string", "const" => "resource" }
@@ -104,10 +107,9 @@ module ActionMCP
104
107
  "ResourceLink" => {
105
108
  "type" => "object",
106
109
  "required" => %w[name type uri],
107
- "properties" => META_PROPERTY.merge(
108
- "annotations" => { "$ref" => "#/$defs/Annotations" },
110
+ "properties" => ANNOTATED_PROPERTIES.merge(
109
111
  "description" => { "type" => "string" },
110
- "icons" => { "type" => "array", "items" => { "$ref" => "#/$defs/Icon" } },
112
+ "icons" => { "type" => "array", "items" => ref("Icon") },
111
113
  "mimeType" => { "type" => "string" },
112
114
  "name" => { "type" => "string" },
113
115
  "size" => { "type" => "integer" },
@@ -117,17 +119,14 @@ module ActionMCP
117
119
  )
118
120
  },
119
121
  "ContentBlock" => {
120
- "anyOf" => %w[TextContent ImageContent AudioContent ResourceLink EmbeddedResource].map do |name|
121
- { "$ref" => "#/$defs/#{name}" }
122
- end
122
+ "anyOf" => refs(*%w[TextContent ImageContent AudioContent ResourceLink EmbeddedResource])
123
123
  },
124
124
  "Resource" => {
125
125
  "type" => "object",
126
126
  "required" => %w[name uri],
127
- "properties" => META_PROPERTY.merge(
128
- "annotations" => { "$ref" => "#/$defs/Annotations" },
127
+ "properties" => ANNOTATED_PROPERTIES.merge(
129
128
  "description" => { "type" => "string" },
130
- "icons" => { "type" => "array", "items" => { "$ref" => "#/$defs/Icon" } },
129
+ "icons" => { "type" => "array", "items" => ref("Icon") },
131
130
  "mimeType" => { "type" => "string" },
132
131
  "name" => { "type" => "string" },
133
132
  "size" => { "type" => "integer" },
@@ -139,7 +138,7 @@ module ActionMCP
139
138
  "type" => "object",
140
139
  "required" => %w[content role],
141
140
  "properties" => {
142
- "content" => { "$ref" => "#/$defs/ContentBlock" },
141
+ "content" => ref("ContentBlock"),
143
142
  "role" => { "type" => "string", "enum" => %w[assistant user] }
144
143
  }
145
144
  },
@@ -148,14 +147,14 @@ module ActionMCP
148
147
  "required" => [ "messages" ],
149
148
  "properties" => META_PROPERTY.merge(
150
149
  "description" => { "type" => "string" },
151
- "messages" => { "type" => "array", "items" => { "$ref" => "#/$defs/PromptMessage" } }
150
+ "messages" => { "type" => "array", "items" => ref("PromptMessage") }
152
151
  )
153
152
  },
154
153
  "CallToolResult" => {
155
154
  "type" => "object",
156
155
  "required" => [ "content" ],
157
156
  "properties" => META_PROPERTY.merge(
158
- "content" => { "type" => "array", "items" => { "$ref" => "#/$defs/ContentBlock" } },
157
+ "content" => { "type" => "array", "items" => ref("ContentBlock") },
159
158
  "isError" => { "type" => "boolean" },
160
159
  "structuredContent" => { "type" => "object" }
161
160
  )
@@ -3,38 +3,5 @@
3
3
  module ActionMCP
4
4
  # Module for managing content within ActionMCP.
5
5
  module Content
6
- # Base class for MCP content items.
7
- class Base
8
- # @return [Symbol] The type of content.
9
- # @return [Hash, nil] Optional annotations for the content.
10
- attr_reader :type, :annotations
11
-
12
- # Initializes a new content item.
13
- #
14
- # @param type [Symbol] The type of content.
15
- # @param annotations [Hash, nil] Optional annotations for the content.
16
- def initialize(type, annotations: nil)
17
- @type = type
18
- @annotations = annotations.nil? ? nil : Validation.copy_object!(annotations, "annotations")
19
- Validation.validate_annotations!(@annotations)
20
- end
21
-
22
- # Returns a hash representation of the content.
23
- #
24
- # @return [Hash] The hash representation.
25
- def to_h
26
- h = { type: @type }
27
- h[:annotations] = @annotations if @annotations
28
- Validation.validate_annotations!(@annotations)
29
- h
30
- end
31
-
32
- # Returns a JSON representation of the content.
33
- #
34
- # @return [String] The JSON representation.
35
- def to_json(*)
36
- MultiJSON.generate(to_h, *)
37
- end
38
- end
39
6
  end
40
7
  end
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActionMCP
4
+ # Registry of vendor JSON-RPC methods served next to the MCP core methods.
5
+ #
6
+ # Names take the form "vendor/method" and the vendor segment stays clear of the
7
+ # MCP namespaces, so a spec revision can never collide with an application method.
8
+ # The handler receives the request params and the session; its return value
9
+ # becomes the JSON-RPC result. Raise JSON_RPC::JsonRpcError to answer with a
10
+ # specific error code.
11
+ #
12
+ # @example config/initializers/action_mcp.rb
13
+ # ActionMCP.configure do |config|
14
+ # config.register_method("acme/toolsets/list") do |params, session|
15
+ # { toolsets: Toolset.visible_to(ActionMCP::Current.user).as_json }
16
+ # end
17
+ # end
18
+ class CustomMethods
19
+ RESERVED_NAMESPACES = %w[
20
+ completion elicitation initialize logging notifications ping
21
+ prompts resources roots sampling tasks tools
22
+ ].freeze
23
+
24
+ NAME_FORMAT = %r{\A[a-z0-9][a-z0-9_.-]*/[a-z0-9_./-]*[a-z0-9_]\z}i
25
+
26
+ def initialize
27
+ @handlers = {}
28
+ end
29
+
30
+ def register(name, callable = nil, &block)
31
+ name = name.to_s
32
+ handler = callable || block
33
+
34
+ raise ArgumentError, "Custom method '#{name}' needs a handler responding to #call" unless handler.respond_to?(:call)
35
+ raise ArgumentError, "Custom method '#{name}' must look like 'vendor/method'" unless name.match?(NAME_FORMAT)
36
+
37
+ namespace = name.split("/", 2).first
38
+ if RESERVED_NAMESPACES.include?(namespace.downcase)
39
+ raise ArgumentError, "Custom method '#{name}' uses the reserved MCP namespace '#{namespace}/'"
40
+ end
41
+
42
+ @handlers[name] = handler
43
+ end
44
+
45
+ def unregister(name)
46
+ @handlers.delete(name.to_s)
47
+ end
48
+
49
+ def [](name)
50
+ @handlers[name.to_s]
51
+ end
52
+
53
+ def names
54
+ @handlers.keys
55
+ end
56
+
57
+ def clear
58
+ @handlers.clear
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActionMCP
4
+ # Runs a block inside the Rails reloader in development so edited components
5
+ # are picked up, carrying ActionMCP::Current across the reloader boundary.
6
+ module DevReloader
7
+ CARRIED_CURRENT_ATTRIBUTES = %i[user gateway session_data].freeze
8
+
9
+ def self.wrap
10
+ return yield unless Rails.env.development? && Rails.application&.reloader
11
+
12
+ carried = CARRIED_CURRENT_ATTRIBUTES.index_with { |attribute| ActionMCP::Current.public_send(attribute) }
13
+ Rails.application.reloader.wrap do
14
+ carried.each { |attribute, value| ActionMCP::Current.public_send(:"#{attribute}=", value) }
15
+ yield
16
+ end
17
+ end
18
+ end
19
+ end