Class: ActionMCP::Content::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/action_mcp/content.rb,
lib/action_mcp/content/base.rb

Overview

Base class for all content types, supporting annotations.

Direct Known Subclasses

Audio, Image, Resource, ResourceLink, Text

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, annotations: nil) ⇒ Base

Initializes a new content base.

Parameters:

  • type (String)

    The type of the content (e.g., "text", "image", etc.)

  • annotations (Hash, nil) (defaults to: nil)

    Optional annotations for the content.



16
17
18
19
20
# File 'lib/action_mcp/content.rb', line 16

def initialize(type, annotations: nil)
  @type = type
  @annotations = annotations.nil? ? nil : Validation.copy_object!(annotations, "annotations")
  Validation.validate_annotations!(@annotations)
end

Instance Attribute Details

#annotationsSymbol, ... (readonly)

Returns:

  • (Symbol)

    The type of content.

  • (Hash, nil)

    Optional annotations for the content.



10
11
12
# File 'lib/action_mcp/content.rb', line 10

def annotations
  @annotations
end

#typeSymbol, ... (readonly)

Returns:

  • (Symbol)

    The type of content.

  • (Hash, nil)

    Optional annotations for the content.



10
11
12
# File 'lib/action_mcp/content.rb', line 10

def type
  @type
end

Instance Method Details

#to_hHash

Returns a hash representation of the base content.

Returns:

  • (Hash)

    The hash representation of the base content.



25
26
27
28
29
30
# File 'lib/action_mcp/content.rb', line 25

def to_h
  h = { type: @type }
  h[:annotations] = @annotations if @annotations
  Validation.validate_annotations!(@annotations)
  h
end

#to_jsonString

Returns a JSON representation of the content.

Returns:

  • (String)

    The JSON representation.



35
36
37
# File 'lib/action_mcp/content.rb', line 35

def to_json(*)
  MultiJson.dump(to_h, *)
end