Class: MCPClient::ResourceContent

Inherits:
Object
  • Object
show all
Defined in:
lib/mcp_client/resource_content.rb

Overview

Representation of MCP resource content Resources can contain either text or binary data

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri:, name:, title: nil, mime_type: nil, text: nil, blob: nil, annotations: nil, meta: nil) ⇒ ResourceContent

Initialize resource content

Parameters:

  • uri (String) —

    unique identifier for the resource

  • name (String) —

    the name of the resource

  • title (String, nil) (defaults to: nil) —

    optional human-readable name for display purposes

  • mime_type (String, nil) (defaults to: nil) —

    optional MIME type

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

    text content (mutually exclusive with blob)

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

    base64-encoded binary content (mutually exclusive with text)

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

    optional annotations that provide hints to clients

  • meta (Hash, nil) (defaults to: nil) —

    optional _meta metadata attached to the resource contents (MCP 2025-11-25)

Raises:

  • (ArgumentError)


36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/mcp_client/resource_content.rb', line 36

def initialize(uri:, name:, title: nil, mime_type: nil, text: nil, blob: nil, annotations: nil, meta: nil)
  raise ArgumentError, 'ResourceContent cannot have both text and blob' if text && blob
  raise ArgumentError, 'ResourceContent must have either text or blob' if !text && !blob

  @uri = uri
  @name = name
  @title = title
  @mime_type = mime_type
  @text = text
  @blob = blob
  @annotations = annotations
  @meta = meta
end

Instance Attribute Details

#annotations ⇒ Hash? (readonly)

Returns optional annotations that provide hints to clients.

Returns:

  • (Hash, nil) —

    optional annotations that provide hints to clients



25
# File 'lib/mcp_client/resource_content.rb', line 25

attr_reader :uri, :name, :title, :mime_type, :text, :blob, :annotations, :meta

#blob ⇒ String? (readonly)

Returns base64-encoded binary content (mutually exclusive with text).

Returns:

  • (String, nil) —

    base64-encoded binary content (mutually exclusive with text)



25
# File 'lib/mcp_client/resource_content.rb', line 25

attr_reader :uri, :name, :title, :mime_type, :text, :blob, :annotations, :meta

#meta ⇒ Object (readonly)

Returns the value of attribute meta.



25
# File 'lib/mcp_client/resource_content.rb', line 25

attr_reader :uri, :name, :title, :mime_type, :text, :blob, :annotations, :meta

#mime_type ⇒ String? (readonly)

Returns optional MIME type.

Returns:

  • (String, nil) —

    optional MIME type



25
# File 'lib/mcp_client/resource_content.rb', line 25

attr_reader :uri, :name, :title, :mime_type, :text, :blob, :annotations, :meta

#name ⇒ String (readonly)

Returns the name of the resource.

Returns:

  • (String) —

    the name of the resource



25
# File 'lib/mcp_client/resource_content.rb', line 25

attr_reader :uri, :name, :title, :mime_type, :text, :blob, :annotations, :meta

#text ⇒ String? (readonly)

Returns text content (mutually exclusive with blob).

Returns:

  • (String, nil) —

    text content (mutually exclusive with blob)



25
# File 'lib/mcp_client/resource_content.rb', line 25

attr_reader :uri, :name, :title, :mime_type, :text, :blob, :annotations, :meta

#title ⇒ String? (readonly)

Returns optional human-readable name for display purposes.

Returns:

  • (String, nil) —

    optional human-readable name for display purposes



25
# File 'lib/mcp_client/resource_content.rb', line 25

attr_reader :uri, :name, :title, :mime_type, :text, :blob, :annotations, :meta

#uri ⇒ String (readonly)

Returns unique identifier for the resource.

Returns:

  • (String) —

    unique identifier for the resource



25
26
27
# File 'lib/mcp_client/resource_content.rb', line 25

def uri
  @uri
end

Class Method Details

.from_json(data) ⇒ MCPClient::ResourceContent

Create a ResourceContent instance from JSON data

Parameters:

  • data (Hash) —

    JSON data from MCP server

Returns:



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/mcp_client/resource_content.rb', line 71

def self.from_json(data)
  new(
    uri: data['uri'],
    name: data['name'],
    title: data['title'],
    mime_type: data['mimeType'],
    text: data['text'],
    blob: data['blob'],
    annotations: data['annotations'],
    meta: data['_meta']
  )
end

Instance Method Details

#binary? ⇒ Boolean

Check if content is binary

Returns:

  • (Boolean) —

    true if content is binary



92
93
94
# File 'lib/mcp_client/resource_content.rb', line 92

def binary?
  !@blob.nil?
end

#content ⇒ String

Get the content (text or decoded blob)

Returns:

  • (String) —

    the content



98
99
100
101
102
103
# File 'lib/mcp_client/resource_content.rb', line 98

def content
  return @text if text?

  require 'base64'
  Base64.decode64(@blob)
end

#initialize_copy(source) ⇒ void

This method returns an undefined value.

A copy that shares nothing mutable with the original (cached contents are handed out as copies, so a caller's edits stay its own).

Parameters:



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/mcp_client/resource_content.rb', line 54

def initialize_copy(source)
  super
  @uri = source.uri.dup if source.uri
  @name = source.name.dup if source.name
  @title = source.title.dup if source.title
  @mime_type = source.mime_type.dup if source.mime_type
  @text = source.text.dup if source.text
  @blob = source.blob.dup if source.blob
  # Iterative: peer-supplied annotations or _meta may be nested deeper
  # than the Ruby stack allows.
  @annotations = MCPClient::DeepCopy.copy(source.annotations)
  @meta = MCPClient::DeepCopy.copy(source.meta)
end

#text? ⇒ Boolean

Check if content is text

Returns:

  • (Boolean) —

    true if content is text



86
87
88
# File 'lib/mcp_client/resource_content.rb', line 86

def text?
  !@text.nil?
end