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) ⇒ 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

Raises:

  • (ArgumentError)


31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/mcp_client/resource_content.rb', line 31

def initialize(uri:, name:, title: nil, mime_type: nil, text: nil, blob: nil, annotations: 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
end

Instance Attribute Details

#annotationsObject (readonly)

Returns the value of attribute annotations.



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

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

#blobString? (readonly)

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

Returns:

  • (String, nil)

    base64-encoded binary content (mutually exclusive with text)



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

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

#mime_typeString? (readonly)

Returns optional MIME type.

Returns:

  • (String, nil)

    optional MIME type



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

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

#nameString (readonly)

Returns the name of the resource.

Returns:

  • (String)

    the name of the resource



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

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

#textString? (readonly)

Returns text content (mutually exclusive with blob).

Returns:

  • (String, nil)

    text content (mutually exclusive with blob)



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

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

#titleString? (readonly)

Returns optional human-readable name for display purposes.

Returns:

  • (String, nil)

    optional human-readable name for display purposes



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

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

#uriString (readonly)

Returns unique identifier for the resource.

Returns:

  • (String)

    unique identifier for the resource



21
22
23
# File 'lib/mcp_client/resource_content.rb', line 21

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:



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/mcp_client/resource_content.rb', line 47

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']
  )
end

Instance Method Details

#binary?Boolean

Check if content is binary

Returns:

  • (Boolean)

    true if content is binary



67
68
69
# File 'lib/mcp_client/resource_content.rb', line 67

def binary?
  !@blob.nil?
end

#contentString

Get the content (text or decoded blob)

Returns:

  • (String)

    the content



73
74
75
76
77
78
# File 'lib/mcp_client/resource_content.rb', line 73

def content
  return @text if text?

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

#text?Boolean

Check if content is text

Returns:

  • (Boolean)

    true if content is text



61
62
63
# File 'lib/mcp_client/resource_content.rb', line 61

def text?
  !@text.nil?
end