Class: ActionMCP::Client::Blueprint

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

Overview

Blueprints

A collection that manages and provides access to URI templates (blueprints) for Model Context Protocol (MCP) resource discovery. These blueprints allow dynamic construction of resource URIs by filling in variable placeholders with specific values. The class supports lazy loading of templates when initialized with a client.

Example usage:

# Eager loading
template_data = client.list_resource_templates # Returns array of URI template definitions
blueprints = Blueprint.new(template_data)

# Lazy loading
blueprints = Blueprint.new([], client)
templates = blueprints.all # Templates are loaded here

# Access a specific blueprint by pattern
file_blueprint = Blueprint.find_by_pattern("file://{path}")

# Generate a concrete URI from a blueprint with parameters
uri = Blueprint.construct("file://{path}", { path: "/logs/app.log" })

Defined Under Namespace

Classes: ResourceTemplate

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(templates, client) ⇒ Blueprint

Initialize a new Blueprints collection with URI template definitions

Parameters:

  • templates (Array<Hash>)

    Array of URI template definition hashes, each containing uriTemplate, name, description, and optionally mimeType keys

  • client (Object, nil)

    Optional client for lazy loading of templates



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

def initialize(templates, client)
  super(templates, client)
  self.templates = @collection_data
  @load_method = :list_resource_templates
end

Instance Method Details

#construct(pattern, params) ⇒ String

Construct a concrete URI by applying parameters to a blueprint

Parameters:

  • pattern (String)

    URI template pattern to use

  • params (Hash)

    Parameters to substitute into the pattern

Returns:

  • (String)

    The constructed URI with parameters applied

Raises:

  • (KeyError)

    If a required parameter is missing



61
62
63
64
65
66
# File 'lib/action_mcp/client/blueprint.rb', line 61

def construct(pattern, params)
  blueprint = find_by_pattern(pattern)
  raise ArgumentError, "Unknown blueprint pattern: #{pattern}" unless blueprint

  blueprint.construct(params)
end

#contains?(pattern) ⇒ Boolean

Check if the collection contains a blueprint with the given pattern

Parameters:

  • pattern (String)

    The blueprint pattern to check for

Returns:

  • (Boolean)

    true if a blueprint with the pattern exists



72
73
74
# File 'lib/action_mcp/client/blueprint.rb', line 72

def contains?(pattern)
  all.any? { |blueprint| blueprint.pattern == pattern }
end

#find_by_name(name) ⇒ Array<Blueprint>

Find blueprints by name

Parameters:

  • name (String)

    Name of the blueprints to find

Returns:

  • (Array<Blueprint>)

    Blueprints with the given name



51
52
53
# File 'lib/action_mcp/client/blueprint.rb', line 51

def find_by_name(name)
  all.select { |blueprint| blueprint.name == name }
end

#find_by_pattern(pattern) ⇒ Blueprint?

Find a blueprint by its URI pattern

Parameters:

  • pattern (String)

    URI template pattern to find

Returns:

  • (Blueprint, nil)

    The blueprint with the given pattern, or nil if not found



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

def find_by_pattern(pattern)
  all.find { |blueprint| blueprint.pattern == pattern }
end

#group_by_protocolHash<String, Array<Blueprint>>

Group blueprints by their base protocol

Returns:

  • (Hash<String, Array<Blueprint>>)

    Hash mapping protocols to arrays of blueprints



79
80
81
# File 'lib/action_mcp/client/blueprint.rb', line 79

def group_by_protocol
  all.group_by(&:protocol)
end

#templates=(templates) ⇒ Object

Convert raw template data into ResourceTemplate objects

Parameters:

  • templates (Array<Hash>)

    Array of template definition hashes



86
87
88
# File 'lib/action_mcp/client/blueprint.rb', line 86

def templates=(templates)
  @collection_data = templates.map { |template_data| ResourceTemplate.new(template_data) }
end