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

.meta_valueObject (readonly)

Returns the value of attribute meta_value.



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

def meta_value
  @meta_value
end

.title_valueObject (readonly)

Returns the value of attribute title_value.



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

def title_value
  @title_value
end

Class Method Details

.arguments(value = NOT_SET) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/mcp/prompt.rb', line 64

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

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



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/mcp/prompt.rb', line 80

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

.description(value = NOT_SET) ⇒ Object



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

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

.inherited(subclass) ⇒ Object



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

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

.meta(value = NOT_SET) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/mcp/prompt.rb', line 72

def meta(value = NOT_SET)
  if value == NOT_SET
    @meta_value
  else
    @meta_value = value
  end
end

.name_valueObject



44
45
46
# File 'lib/mcp/prompt.rb', line 44

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

.prompt_name(value = NOT_SET) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/mcp/prompt.rb', line 36

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)


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

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

.title(value = NOT_SET) ⇒ Object



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

def title(value = NOT_SET)
  if value == NOT_SET
    @title_value
  else
    @title_value = value
  end
end

.to_hObject



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

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

.validate_arguments!(args) ⇒ Object



93
94
95
96
97
98
99
100
# File 'lib/mcp/prompt.rb', line 93

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