Class: ActionMCP::Prompt
- Inherits:
-
Capability
- Object
- Capability
- ActionMCP::Prompt
- Includes:
- Callbacks, CurrentHelpers
- Defined in:
- lib/action_mcp/prompt.rb
Overview
Abstract base class for Prompts
Direct Known Subclasses
Instance Attribute Summary
Attributes inherited from Capability
Class Method Summary collapse
-
.argument(arg_name, description: "", required: false, default: nil, enum: nil, type: :string) ⇒ void
————————————————— Argument DSL ————————————————— Defines an argument for the prompt.
-
.arguments ⇒ Array<Hash>
Returns the list of argument definitions.
-
.call(params) ⇒ PromptResponse
————————————————— Class-level call method ————————————————— Receives a Hash of params, initializes a prompt instance, validates it, and if valid, calls the instance call method.
-
.default_prompt_name ⇒ String
(also: default_capability_name)
Returns the default prompt name based on the class name.
-
.inherited(subclass) ⇒ Object
Hook called when a class inherits from Prompt.
-
.meta(data = nil) ⇒ Object
Sets or retrieves the _meta field.
-
.prompt_name(name = nil) ⇒ String
————————————————— Prompt Name ————————————————— Gets or sets the prompt name.
-
.to_h ⇒ Hash
————————————————— Convert prompt definition to Hash —————————————————.
- .type ⇒ Object
- .unregister_from_registry ⇒ Object
Instance Method Summary collapse
-
#call ⇒ Object
Public entry point for executing the prompt Returns a PromptResponse object containing messages.
- #inspect ⇒ Object
-
#render(**args) ⇒ Object
Override render to collect messages.
Methods inherited from Capability
abstract!, abstract?, capability_name, description, #initialize, #session, #with_context
Methods included from Renderable
Constructor Details
This class inherits a constructor from ActionMCP::Capability
Class Method Details
.argument(arg_name, description: "", required: false, default: nil, enum: nil, type: :string) ⇒ void
This method returns an undefined value.
Argument DSL
Defines an argument for the prompt.
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/action_mcp/prompt.rb', line 95 def self.argument(arg_name, description: "", required: false, default: nil, enum: nil, type: :string) arg_def = { name: arg_name.to_s, description: description, required: required, default: default, enum: enum } self._argument_definitions += [ arg_def ] # Register the attribute so it's recognized by ActiveModel attribute arg_name, type, default: default validates arg_name, presence: true if required return unless enum.present? validates arg_name, inclusion: { in: enum }, allow_blank: !required end |
.arguments ⇒ Array<Hash>
Returns the list of argument definitions.
117 118 119 |
# File 'lib/action_mcp/prompt.rb', line 117 def self.arguments _argument_definitions end |
.call(params) ⇒ PromptResponse
Class-level call method
Receives a Hash of params, initializes a prompt instance, validates it, and if valid, calls the instance call method. If invalid, raises a JsonRpcError with code :invalid_params.
147 148 149 150 151 152 |
# File 'lib/action_mcp/prompt.rb', line 147 def self.call(params) prompt = new(params) # Initialize an instance with provided params # If we reach here, the prompt is valid prompt.call end |
.default_prompt_name ⇒ String Also known as: default_capability_name
Returns the default prompt name based on the class name.
45 46 47 48 49 |
# File 'lib/action_mcp/prompt.rb', line 45 def self.default_prompt_name return "" if name.nil? name.underscore.gsub("/", "__").sub(/_prompt$/, "") end |
.inherited(subclass) ⇒ Object
Hook called when a class inherits from Prompt
63 64 65 66 67 68 69 |
# File 'lib/action_mcp/prompt.rb', line 63 def inherited(subclass) super # Run the ActiveSupport load hook when a prompt is defined subclass.class_eval do ActiveSupport.run_load_hooks(:action_mcp_prompt, subclass) end end |
.meta(data = nil) ⇒ Object
Sets or retrieves the _meta field
72 73 74 75 76 77 78 79 80 |
# File 'lib/action_mcp/prompt.rb', line 72 def (data = nil) if data raise ArgumentError, "_meta must be a hash" unless data.is_a?(Hash) self. = .merge(data) else end end |
.prompt_name(name = nil) ⇒ String
Prompt Name
Gets or sets the prompt name.
18 19 20 21 22 23 24 25 26 |
# File 'lib/action_mcp/prompt.rb', line 18 def self.prompt_name(name = nil) if name self._capability_name = name re_register_if_needed name else _capability_name || default_prompt_name end end |
.to_h ⇒ Hash
Convert prompt definition to Hash
125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/action_mcp/prompt.rb', line 125 def self.to_h result = { name: prompt_name, description: description.presence, arguments: arguments.map { |arg| arg.slice(:name, :description, :required, :type) } }.compact # Add _meta if present result[:_meta] = if .any? result end |
.type ⇒ Object
54 55 56 |
# File 'lib/action_mcp/prompt.rb', line 54 def type :prompt end |
.unregister_from_registry ⇒ Object
58 59 60 |
# File 'lib/action_mcp/prompt.rb', line 58 def unregister_from_registry ActionMCP::PromptsRegistry.unregister(self) if ActionMCP::PromptsRegistry.items.values.include?(self) end |
Instance Method Details
#call ⇒ Object
Public entry point for executing the prompt Returns a PromptResponse object containing messages
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/action_mcp/prompt.rb', line 160 def call @response = PromptResponse.new # Check validations before proceeding if valid? begin run_callbacks :perform do perform # Invoke the subclass-specific logic if valid end rescue StandardError # Handle exceptions during execution @response.mark_as_error!(:internal_error, message: "Unhandled Error executing prompt") end else # Handle validation failure @response.mark_as_error!(:invalid_params, message: "Invalid input", data: errors.) end @response # Return the response with collected messages end |
#inspect ⇒ Object
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/action_mcp/prompt.rb', line 181 def inspect attributes_hash = attributes.transform_values(&:inspect) response_info = if defined?(@response) && @response "response: #{@response.messages.size} message(s)" else "response: nil" end errors_info = errors.any? ? ", errors: #{errors.full_messages}" : "" "#<#{self.class.name} #{attributes_hash.map do |k, v| "#{k}: #{v.inspect}" end.join(', ')}, #{response_info}#{errors_info}>" end |
#render(**args) ⇒ Object
Override render to collect messages
198 199 200 201 202 |
# File 'lib/action_mcp/prompt.rb', line 198 def render(**args) content = super(**args.slice(:text, :audio, :image, :resource, :mime_type, :blob)) @response.add_content(content, role: args.fetch(:role, "user")) # Add to the response content # Return the content for potential use in perform end |