Class: ActionMCP::Content::Resource

Inherits:
Base
  • Object
show all
Defined in:
lib/action_mcp/content/resource.rb

Overview

Resource content references a server-managed resource. It includes a URI, MIME type, and optionally text content or a base64-encoded blob.

Instance Attribute Summary collapse

Attributes inherited from Base

#type

Instance Method Summary collapse

Methods inherited from Base

#to_json

Constructor Details

#initialize(uri, mime_type = "text/plain", text: nil, blob: nil, annotations: nil, meta: nil) ⇒ Resource

Initializes a new Resource content.

Parameters:

  • uri (String)

    The URI of the resource.

  • mime_type (String) (defaults to: "text/plain")

    The MIME type of the resource.

  • text (String, nil) (defaults to: nil)

    The text content of the resource (optional).

  • blob (String, nil) (defaults to: nil)

    The base64-encoded blob of the resource (optional).

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

    Optional annotations for the resource.

  • meta (Hash, #to_hash, #to_h, nil) (defaults to: nil)

    Optional extension metadata. Emitted on the wire as _meta.

Raises:

  • (ArgumentError)


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/action_mcp/content/resource.rb', line 23

def initialize(uri, mime_type = "text/plain", text: nil, blob: nil, annotations: nil, meta: nil)
  raise ArgumentError, "uri must be a non-empty string" unless uri.is_a?(String) && uri.present?
  unless mime_type.nil? || (mime_type.is_a?(String) && mime_type.present?)
    raise ArgumentError, "mime_type must be a non-empty string or nil"
  end
  unless [ !text.nil?, !blob.nil? ].one?
    raise ArgumentError, "embedded resources require exactly one of text or blob"
  end
  raise ArgumentError, "text must be a string" if !text.nil? && !text.is_a?(String)
  raise ArgumentError, "blob must be a base64 string" if !blob.nil? && !blob.is_a?(String)

  super("resource", annotations: annotations)
  @uri = uri
  @mime_type = mime_type
  @text = text
  @blob = blob
  @meta = meta.nil? ? nil : Validation.copy_object!(meta, "meta")
  to_h
end

Instance Attribute Details

#annotationsString, ... (readonly)

Returns:

  • (String)

    The URI of the resource.

  • (String)

    The MIME type of the resource.

  • (String, nil)

    The text content of the resource (optional).

  • (String, nil)

    The base64-encoded blob of the resource (optional).

  • (Hash, nil)

    Optional extension metadata (serialized on the wire as _meta).



13
14
15
# File 'lib/action_mcp/content/resource.rb', line 13

def annotations
  @annotations
end

#blobString, ... (readonly)

Returns:

  • (String)

    The URI of the resource.

  • (String)

    The MIME type of the resource.

  • (String, nil)

    The text content of the resource (optional).

  • (String, nil)

    The base64-encoded blob of the resource (optional).

  • (Hash, nil)

    Optional extension metadata (serialized on the wire as _meta).



13
14
15
# File 'lib/action_mcp/content/resource.rb', line 13

def blob
  @blob
end

#metaString, ... (readonly)

Returns:

  • (String)

    The URI of the resource.

  • (String)

    The MIME type of the resource.

  • (String, nil)

    The text content of the resource (optional).

  • (String, nil)

    The base64-encoded blob of the resource (optional).

  • (Hash, nil)

    Optional extension metadata (serialized on the wire as _meta).



13
14
15
# File 'lib/action_mcp/content/resource.rb', line 13

def meta
  @meta
end

#mime_typeString, ... (readonly)

Returns:

  • (String)

    The URI of the resource.

  • (String)

    The MIME type of the resource.

  • (String, nil)

    The text content of the resource (optional).

  • (String, nil)

    The base64-encoded blob of the resource (optional).

  • (Hash, nil)

    Optional extension metadata (serialized on the wire as _meta).



13
14
15
# File 'lib/action_mcp/content/resource.rb', line 13

def mime_type
  @mime_type
end

#textString, ... (readonly)

Returns:

  • (String)

    The URI of the resource.

  • (String)

    The MIME type of the resource.

  • (String, nil)

    The text content of the resource (optional).

  • (String, nil)

    The base64-encoded blob of the resource (optional).

  • (Hash, nil)

    Optional extension metadata (serialized on the wire as _meta).



13
14
15
# File 'lib/action_mcp/content/resource.rb', line 13

def text
  @text
end

#uriString, ... (readonly)

Returns:

  • (String)

    The URI of the resource.

  • (String)

    The MIME type of the resource.

  • (String, nil)

    The text content of the resource (optional).

  • (String, nil)

    The base64-encoded blob of the resource (optional).

  • (Hash, nil)

    Optional extension metadata (serialized on the wire as _meta).



13
14
15
# File 'lib/action_mcp/content/resource.rb', line 13

def uri
  @uri
end

Instance Method Details

#to_hHash

Returns a hash representation of the resource content. Per MCP spec, embedded resources have type "resource" with a nested resource object. meta is emitted as _meta on the inner resource hash (TextResourceContents / BlobResourceContents), not on the outer content envelope.

Returns:

  • (Hash)

    The hash representation of the resource content.



49
50
51
52
53
54
55
56
57
# File 'lib/action_mcp/content/resource.rb', line 49

def to_h
  inner = { uri: @uri }
  inner[:mimeType] = @mime_type if @mime_type
  inner[:text] = @text if @text
  inner[:blob] = @blob if @blob
  inner[:_meta] = @meta if @meta && !@meta.empty?

  super.merge(resource: inner).tap { |result| Validation.validate_content_block!(result) }
end