Class: MCP::Prompt

Inherits:
Object
  • Object
show all
Defined in:
lib/mcp/prompt.rb,
lib/mcp/prompt/result.rb,
lib/mcp/prompt/message.rb,
lib/mcp/prompt/argument.rb

Defined Under Namespace

Classes: Argument, Message, Result

Constant Summary collapse

NOT_SET =
Object.new

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.arguments_valueObject (readonly)

Returns the value of attribute arguments_value.



10
11
12
# File 'lib/mcp/prompt.rb', line 10

def arguments_value
  @arguments_value
end

.description_valueObject (readonly)

Returns the value of attribute description_value.



9
10
11
# File 'lib/mcp/prompt.rb', line 9

def description_value
  @description_value
end

Class Method Details

.arguments(value = NOT_SET) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/mcp/prompt.rb', line 47

def arguments(value = NOT_SET)
  if value == NOT_SET
    @arguments_value
  else
    @arguments_value = value
  end
end

.define(name: nil, description: nil, arguments: [], &block) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/mcp/prompt.rb', line 55

def define(name: nil, description: nil, arguments: [], &block)
  Class.new(self) do
    prompt_name name
    description description
    arguments arguments
    define_singleton_method(:template) do |args, server_context: nil|
      instance_exec(args, server_context:, &block)
    end
  end
end

.description(value = NOT_SET) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/mcp/prompt.rb', line 39

def description(value = NOT_SET)
  if value == NOT_SET
    @description_value
  else
    @description_value = value
  end
end

.inherited(subclass) ⇒ Object



20
21
22
23
24
25
# File 'lib/mcp/prompt.rb', line 20

def inherited(subclass)
  super
  subclass.instance_variable_set(:@name_value, nil)
  subclass.instance_variable_set(:@description_value, nil)
  subclass.instance_variable_set(:@arguments_value, nil)
end

.name_valueObject



35
36
37
# File 'lib/mcp/prompt.rb', line 35

def name_value
  @name_value || StringUtils.handle_from_class_name(name)
end

.prompt_name(value = NOT_SET) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/mcp/prompt.rb', line 27

def prompt_name(value = NOT_SET)
  if value == NOT_SET
    @name_value
  else
    @name_value = value
  end
end

.template(args, server_context: nil) ⇒ Object

Raises:

  • (NotImplementedError)


12
13
14
# File 'lib/mcp/prompt.rb', line 12

def template(args, server_context: nil)
  raise NotImplementedError, "Subclasses must implement template"
end

.to_hObject



16
17
18
# File 'lib/mcp/prompt.rb', line 16

def to_h
  { name: name_value, description: description_value, arguments: arguments_value.map(&:to_h) }.compact
end

.validate_arguments!(args) ⇒ Object



66
67
68
69
70
71
72
73
# File 'lib/mcp/prompt.rb', line 66

def validate_arguments!(args)
  missing = required_args - args.keys
  return if missing.empty?

  raise MCP::Server::RequestHandlerError.new(
    "Missing required arguments: #{missing.join(", ")}", nil, error_type: :missing_required_arguments
  )
end