actionmcp 0.201.3 → 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 +8 -59
  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 +24 -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
@@ -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
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/callbacks"
4
+
5
+ module ActionMCP
6
+ # Builds a concern that defines one callback chain plus its
7
+ # before_<event>, after_<event> and around_<event> class macros.
8
+ module LifecycleCallbacks
9
+ def self.for(event)
10
+ Module.new do
11
+ extend ActiveSupport::Concern
12
+ include ActiveSupport::Callbacks
13
+
14
+ included do
15
+ define_callbacks event, skip_after_callbacks_if_terminated: true
16
+ end
17
+
18
+ class_methods do
19
+ %i[before after around].each do |kind|
20
+ define_method(:"#{kind}_#{event}") do |*filters, &blk|
21
+ set_callback(event, kind, *filters, &blk)
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -118,16 +118,7 @@ module ActionMCP
118
118
  end
119
119
 
120
120
  # Format the metric
121
- formatter = formatters&.dig(key)
122
- formatted_value = if formatter.respond_to?(:call)
123
- formatter.call(value)
124
- elsif value.is_a?(Float)
125
- format("%.1fms", value)
126
- else
127
- value.to_s
128
- end
129
-
130
- formatted_metric = "#{key}: #{formatted_value}"
121
+ formatted_metric = format_metric(key, value)
131
122
 
132
123
  # Add to appropriate group (or "other")
133
124
  if group
@@ -145,18 +136,21 @@ module ActionMCP
145
136
  end.compact.join(" | ")
146
137
  else
147
138
  # No grouping, just format all metrics
148
- custom_metrics.map do |key, value|
149
- formatter = formatters&.dig(key)
150
- formatted_value = if formatter.respond_to?(:call)
151
- formatter.call(value)
152
- elsif value.is_a?(Float)
153
- format("%.1fms", value)
154
- else
155
- value.to_s
156
- end
157
- "#{key}: #{formatted_value}"
158
- end.join(" | ")
139
+ custom_metrics.map { |key, value| format_metric(key, value) }.join(" | ")
140
+ end
141
+ end
142
+
143
+ # "key: value", using a registered formatter, milliseconds for floats, or to_s.
144
+ def self.format_metric(key, value)
145
+ formatter = formatters&.dig(key)
146
+ formatted_value = if formatter.respond_to?(:call)
147
+ formatter.call(value)
148
+ elsif value.is_a?(Float)
149
+ format("%.1fms", value)
150
+ else
151
+ value.to_s
159
152
  end
153
+ "#{key}: #{formatted_value}"
160
154
  end
161
155
 
162
156
  # Register a custom formatter for a specific metric
@@ -27,10 +27,7 @@ module ActionMCP
27
27
  schema["description"] = description if description
28
28
  schema.merge!(options) if options.any?
29
29
 
30
- @properties[name.to_s] = schema
31
- @required << name.to_s if required
32
-
33
- name.to_s
30
+ add_property(name, schema, required)
34
31
  end
35
32
 
36
33
  # Define a string property
@@ -44,14 +41,7 @@ module ActionMCP
44
41
  schema["minLength"] = min_length if min_length
45
42
  schema["maxLength"] = max_length if max_length
46
43
 
47
- if name
48
- @properties[name.to_s] = schema
49
- @required << name.to_s if required
50
- name.to_s
51
- else
52
- # Return schema for use in array items or other contexts
53
- schema
54
- end
44
+ add_property(name, schema, required)
55
45
  end
56
46
 
57
47
  # Define a number property
@@ -63,10 +53,7 @@ module ActionMCP
63
53
  schema["maximum"] = maximum if maximum
64
54
  schema["default"] = default if default
65
55
 
66
- @properties[name.to_s] = schema
67
- @required << name.to_s if required
68
-
69
- name.to_s
56
+ add_property(name, schema, required)
70
57
  end
71
58
 
72
59
  # Define a boolean property
@@ -75,10 +62,7 @@ module ActionMCP
75
62
  schema["description"] = description if description
76
63
  schema["default"] = default unless default.nil?
77
64
 
78
- @properties[name.to_s] = schema
79
- @required << name.to_s if required
80
-
81
- name.to_s
65
+ add_property(name, schema, required)
82
66
  end
83
67
 
84
68
  # Define an array property
@@ -122,9 +106,7 @@ module ActionMCP
122
106
  schema["items"] = { "type" => "string" }
123
107
  end
124
108
 
125
- @properties[name.to_s] = schema
126
-
127
- name.to_s
109
+ add_property(name, schema, false)
128
110
  end
129
111
 
130
112
  # Define an object property
@@ -150,14 +132,7 @@ module ActionMCP
150
132
  # Add additionalProperties if specified
151
133
  add_additional_properties_to_schema(schema, additional_properties)
152
134
 
153
- if name
154
- @properties[name.to_s] = schema
155
- @required << name.to_s if required
156
- name.to_s
157
- else
158
- # Return schema directly for use in array items
159
- schema
160
- end
135
+ add_property(name, schema, required)
161
136
  end
162
137
 
163
138
  # Set additionalProperties for the root schema
@@ -186,5 +161,17 @@ module ActionMCP
186
161
 
187
162
  schema
188
163
  end
164
+
165
+ private
166
+
167
+ # Registers a named property and returns its name; an unnamed schema is
168
+ # returned as-is so it can serve as array items.
169
+ def add_property(name, schema, required)
170
+ return schema unless name
171
+
172
+ @properties[name.to_s] = schema
173
+ @required << name.to_s if required
174
+ name.to_s
175
+ end
189
176
  end
190
177
  end