Class: RubyLLM::MCP::Prompt

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_llm/mcp/prompt.rb

Defined Under Namespace

Classes: Argument

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(adapter, prompt) ⇒ Prompt

Returns a new instance of Prompt.



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

def initialize(adapter, prompt)
  @adapter = adapter
  @name = prompt["name"]
  @description = prompt["description"]
  @arguments = parse_arguments(prompt["arguments"])
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



24
25
26
# File 'lib/ruby_llm/mcp/prompt.rb', line 24

def adapter
  @adapter
end

#argumentsObject (readonly)

Returns the value of attribute arguments.



24
25
26
# File 'lib/ruby_llm/mcp/prompt.rb', line 24

def arguments
  @arguments
end

#descriptionObject (readonly)

Returns the value of attribute description.



24
25
26
# File 'lib/ruby_llm/mcp/prompt.rb', line 24

def description
  @description
end

#nameObject (readonly)

Returns the value of attribute name.



24
25
26
# File 'lib/ruby_llm/mcp/prompt.rb', line 24

def name
  @name
end

Instance Method Details

#ask(chat, arguments: {}) ⇒ Object Also known as: say



45
46
47
48
49
# File 'lib/ruby_llm/mcp/prompt.rb', line 45

def ask(chat, arguments: {}, &)
  include(chat, arguments: arguments)

  chat.complete(&)
end

#complete(argument, value, context: nil) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/ruby_llm/mcp/prompt.rb', line 53

def complete(argument, value, context: nil)
  if @adapter.capabilities.completion?
    result = @adapter.completion_prompt(name: @name, argument: argument, value: value, context: context)
    if result.error?
      return result.to_error
    end

    response = result.value["completion"]

    Completion.new(argument: argument, values: response["values"], total: response["total"],
                   has_more: response["hasMore"])
  else
    message = "Completion is not available for this MCP server"
    raise Errors::Capabilities::CompletionNotAvailable.new(message: message)
  end
end

#fetch(arguments = {}) ⇒ Object



33
34
35
# File 'lib/ruby_llm/mcp/prompt.rb', line 33

def fetch(arguments = {})
  fetch_prompt_messages(arguments)
end

#include(chat, arguments: {}) ⇒ Object



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

def include(chat, arguments: {})
  validate_arguments!(arguments)
  messages = fetch_prompt_messages(arguments)

  messages.each { |message| chat.add_message(message) }
  chat
end

#to_hObject Also known as: to_json



70
71
72
73
74
75
76
# File 'lib/ruby_llm/mcp/prompt.rb', line 70

def to_h
  {
    name: @name,
    description: @description,
    arguments: @arguments.map(&:to_h)
  }
end