Class: ActionMCP::Client::PromptBook

Inherits:
Collection show all
Defined in:
lib/action_mcp/client/prompt_book.rb

Overview

PromptBook

A collection that manages and provides access to prompt templates from the MCP server. The class stores prompt definitions along with their arguments and provides methods for retrieving, filtering, and accessing prompts. It supports lazy loading of prompts when initialized with a client.

Example usage:

# Eager loading
prompts_data = client.list_prompts # Returns array of prompt definitions
book = PromptBook.new(prompts_data)

# Lazy loading
book = PromptBook.new([], client)
prompts = book.all # Prompts are loaded here

# Access a specific prompt by name
summary_prompt = book.find("summarize_text")

# Get all prompts matching a criteria
text_prompts = book.filter { |p| p.name.include?("text") }

Defined Under Namespace

Classes: Prompt

Constant Summary

Constants included from RequestTimeouts

RequestTimeouts::DEFAULT_TIMEOUT

Instance Attribute Summary

Attributes inherited from Collection

#client, #loaded, #next_cursor, #total

Instance Method Summary collapse

Methods inherited from Collection

#all, #all!, #each, #each_page, #filter, #has_more_pages?, #next_page, #page, #size

Methods included from RequestTimeouts

#load_with_timeout

Constructor Details

#initialize(prompts, client) ⇒ PromptBook

Initialize a new PromptBook with prompt definitions

Parameters:

  • prompts (Array<Hash>)

    Array of prompt definition hashes, each containing name, description, and arguments keys

  • client (Object, nil)

    Optional client for lazy loading of prompts



33
34
35
36
37
# File 'lib/action_mcp/client/prompt_book.rb', line 33

def initialize(prompts, client)
  super(prompts, client)
  self.prompts = @collection_data
  @load_method = :list_prompts
end

Instance Method Details

#contains?(name) ⇒ Boolean

Check if the collection contains a prompt with the given name

Parameters:

  • name (String)

    The prompt name to check for

Returns:

  • (Boolean)

    true if a prompt with the name exists



58
59
60
# File 'lib/action_mcp/client/prompt_book.rb', line 58

def contains?(name)
  all.any? { |prompt| prompt.name == name }
end

#find(name) ⇒ Prompt?

Find a prompt by name

Parameters:

  • name (String)

    Name of the prompt to find

Returns:

  • (Prompt, nil)

    The prompt with the given name, or nil if not found



43
44
45
# File 'lib/action_mcp/client/prompt_book.rb', line 43

def find(name)
  all.find { |prompt| prompt.name == name }
end

#namesArray<String>

Get a list of all prompt names

Returns:

  • (Array<String>)

    Names of all prompts in the collection



50
51
52
# File 'lib/action_mcp/client/prompt_book.rb', line 50

def names
  all.map(&:name)
end

#prompts=(prompts) ⇒ Object

Convert raw prompt data into Prompt objects

Parameters:

  • prompts (Array<Hash>)

    Array of prompt definition hashes



65
66
67
# File 'lib/action_mcp/client/prompt_book.rb', line 65

def prompts=(prompts)
  @collection_data = prompts.map { |data| Prompt.new(data) }
end