Class: VectorMCP::Definitions::Resource

Inherits:
Struct
  • Object
show all
Defined in:
lib/vector_mcp/definitions.rb

Overview

Represents a resource (context or data) that can be provided to the AI model or user.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#descriptionObject

String A description of the resource content.



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/vector_mcp/definitions.rb', line 120

Resource = Struct.new(:uri, :name, :description, :mime_type, :handler) do
  # Converts the resource to its MCP definition hash.
  # @return [Hash] A hash representing the resource in MCP format.
  def as_mcp_definition
    {
      uri: uri.to_s,
      name: name,
      description: description,
      mimeType: mime_type
    }.compact
  end

  # Checks if this resource represents an image.
  # @return [Boolean] True if the resource's MIME type indicates an image.
  def image_resource?
    !!mime_type&.start_with?("image/")
  end

  # Class method to create an image resource from a file path.
  # @param uri [String] The URI for the resource.
  # @param file_path [String] Path to the image file.
  # @param name [String] Human-readable name for the resource.
  # @param description [String] Description of the resource.
  # @return [Resource] A new Resource instance configured for the image file.
  def self.from_image_file(uri:, file_path:, name: nil, description: nil)
    raise ArgumentError, "Image file not found: #{file_path}" unless File.exist?(file_path)

    # Auto-detect MIME type
    require_relative "image_util"
    image_data = File.binread(file_path)
    detected_mime_type = VectorMCP::ImageUtil.detect_image_format(image_data)

    raise ArgumentError, "Could not detect image format for file: #{file_path}" unless detected_mime_type

    # Generate name and description if not provided
    default_name = name || File.basename(file_path)
    default_description = description || "Image file: #{file_path}"

    handler = lambda do |_params|
      VectorMCP::ImageUtil.file_to_mcp_image_content(file_path)
    end

    new(uri, default_name, default_description, detected_mime_type, handler)
  end

  # Class method to create an image resource from binary data.
  # @param uri [String] The URI for the resource.
  # @param image_data [String] Binary image data.
  # @param name [String] Human-readable name for the resource.
  # @param description [String] Description of the resource.
  # @param mime_type [String, nil] MIME type (auto-detected if nil).
  # @return [Resource] A new Resource instance configured for the image data.
  def self.from_image_data(uri:, image_data:, name:, description: nil, mime_type: nil)
    require_relative "image_util"

    # Detect or validate MIME type
    detected_mime_type = VectorMCP::ImageUtil.detect_image_format(image_data)
    final_mime_type = mime_type || detected_mime_type

    raise ArgumentError, "Could not determine MIME type for image data" unless final_mime_type

    default_description = description || "Image resource: #{name}"

    handler = lambda do |_params|
      VectorMCP::ImageUtil.to_mcp_image_content(image_data, mime_type: final_mime_type)
    end

    new(uri, name, default_description, final_mime_type, handler)
  end
end

#handlerObject

Proc A callable that returns the content of the resource. It may receive parameters from the request (e.g., for dynamic resources).



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/vector_mcp/definitions.rb', line 120

Resource = Struct.new(:uri, :name, :description, :mime_type, :handler) do
  # Converts the resource to its MCP definition hash.
  # @return [Hash] A hash representing the resource in MCP format.
  def as_mcp_definition
    {
      uri: uri.to_s,
      name: name,
      description: description,
      mimeType: mime_type
    }.compact
  end

  # Checks if this resource represents an image.
  # @return [Boolean] True if the resource's MIME type indicates an image.
  def image_resource?
    !!mime_type&.start_with?("image/")
  end

  # Class method to create an image resource from a file path.
  # @param uri [String] The URI for the resource.
  # @param file_path [String] Path to the image file.
  # @param name [String] Human-readable name for the resource.
  # @param description [String] Description of the resource.
  # @return [Resource] A new Resource instance configured for the image file.
  def self.from_image_file(uri:, file_path:, name: nil, description: nil)
    raise ArgumentError, "Image file not found: #{file_path}" unless File.exist?(file_path)

    # Auto-detect MIME type
    require_relative "image_util"
    image_data = File.binread(file_path)
    detected_mime_type = VectorMCP::ImageUtil.detect_image_format(image_data)

    raise ArgumentError, "Could not detect image format for file: #{file_path}" unless detected_mime_type

    # Generate name and description if not provided
    default_name = name || File.basename(file_path)
    default_description = description || "Image file: #{file_path}"

    handler = lambda do |_params|
      VectorMCP::ImageUtil.file_to_mcp_image_content(file_path)
    end

    new(uri, default_name, default_description, detected_mime_type, handler)
  end

  # Class method to create an image resource from binary data.
  # @param uri [String] The URI for the resource.
  # @param image_data [String] Binary image data.
  # @param name [String] Human-readable name for the resource.
  # @param description [String] Description of the resource.
  # @param mime_type [String, nil] MIME type (auto-detected if nil).
  # @return [Resource] A new Resource instance configured for the image data.
  def self.from_image_data(uri:, image_data:, name:, description: nil, mime_type: nil)
    require_relative "image_util"

    # Detect or validate MIME type
    detected_mime_type = VectorMCP::ImageUtil.detect_image_format(image_data)
    final_mime_type = mime_type || detected_mime_type

    raise ArgumentError, "Could not determine MIME type for image data" unless final_mime_type

    default_description = description || "Image resource: #{name}"

    handler = lambda do |_params|
      VectorMCP::ImageUtil.to_mcp_image_content(image_data, mime_type: final_mime_type)
    end

    new(uri, name, default_description, final_mime_type, handler)
  end
end

#mime_typeObject

String The MIME type of the resource content (e.g., “text/plain”, “application/json”, “image/jpeg”).



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/vector_mcp/definitions.rb', line 120

Resource = Struct.new(:uri, :name, :description, :mime_type, :handler) do
  # Converts the resource to its MCP definition hash.
  # @return [Hash] A hash representing the resource in MCP format.
  def as_mcp_definition
    {
      uri: uri.to_s,
      name: name,
      description: description,
      mimeType: mime_type
    }.compact
  end

  # Checks if this resource represents an image.
  # @return [Boolean] True if the resource's MIME type indicates an image.
  def image_resource?
    !!mime_type&.start_with?("image/")
  end

  # Class method to create an image resource from a file path.
  # @param uri [String] The URI for the resource.
  # @param file_path [String] Path to the image file.
  # @param name [String] Human-readable name for the resource.
  # @param description [String] Description of the resource.
  # @return [Resource] A new Resource instance configured for the image file.
  def self.from_image_file(uri:, file_path:, name: nil, description: nil)
    raise ArgumentError, "Image file not found: #{file_path}" unless File.exist?(file_path)

    # Auto-detect MIME type
    require_relative "image_util"
    image_data = File.binread(file_path)
    detected_mime_type = VectorMCP::ImageUtil.detect_image_format(image_data)

    raise ArgumentError, "Could not detect image format for file: #{file_path}" unless detected_mime_type

    # Generate name and description if not provided
    default_name = name || File.basename(file_path)
    default_description = description || "Image file: #{file_path}"

    handler = lambda do |_params|
      VectorMCP::ImageUtil.file_to_mcp_image_content(file_path)
    end

    new(uri, default_name, default_description, detected_mime_type, handler)
  end

  # Class method to create an image resource from binary data.
  # @param uri [String] The URI for the resource.
  # @param image_data [String] Binary image data.
  # @param name [String] Human-readable name for the resource.
  # @param description [String] Description of the resource.
  # @param mime_type [String, nil] MIME type (auto-detected if nil).
  # @return [Resource] A new Resource instance configured for the image data.
  def self.from_image_data(uri:, image_data:, name:, description: nil, mime_type: nil)
    require_relative "image_util"

    # Detect or validate MIME type
    detected_mime_type = VectorMCP::ImageUtil.detect_image_format(image_data)
    final_mime_type = mime_type || detected_mime_type

    raise ArgumentError, "Could not determine MIME type for image data" unless final_mime_type

    default_description = description || "Image resource: #{name}"

    handler = lambda do |_params|
      VectorMCP::ImageUtil.to_mcp_image_content(image_data, mime_type: final_mime_type)
    end

    new(uri, name, default_description, final_mime_type, handler)
  end
end

#nameObject

String A human-readable name for the resource.



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/vector_mcp/definitions.rb', line 120

Resource = Struct.new(:uri, :name, :description, :mime_type, :handler) do
  # Converts the resource to its MCP definition hash.
  # @return [Hash] A hash representing the resource in MCP format.
  def as_mcp_definition
    {
      uri: uri.to_s,
      name: name,
      description: description,
      mimeType: mime_type
    }.compact
  end

  # Checks if this resource represents an image.
  # @return [Boolean] True if the resource's MIME type indicates an image.
  def image_resource?
    !!mime_type&.start_with?("image/")
  end

  # Class method to create an image resource from a file path.
  # @param uri [String] The URI for the resource.
  # @param file_path [String] Path to the image file.
  # @param name [String] Human-readable name for the resource.
  # @param description [String] Description of the resource.
  # @return [Resource] A new Resource instance configured for the image file.
  def self.from_image_file(uri:, file_path:, name: nil, description: nil)
    raise ArgumentError, "Image file not found: #{file_path}" unless File.exist?(file_path)

    # Auto-detect MIME type
    require_relative "image_util"
    image_data = File.binread(file_path)
    detected_mime_type = VectorMCP::ImageUtil.detect_image_format(image_data)

    raise ArgumentError, "Could not detect image format for file: #{file_path}" unless detected_mime_type

    # Generate name and description if not provided
    default_name = name || File.basename(file_path)
    default_description = description || "Image file: #{file_path}"

    handler = lambda do |_params|
      VectorMCP::ImageUtil.file_to_mcp_image_content(file_path)
    end

    new(uri, default_name, default_description, detected_mime_type, handler)
  end

  # Class method to create an image resource from binary data.
  # @param uri [String] The URI for the resource.
  # @param image_data [String] Binary image data.
  # @param name [String] Human-readable name for the resource.
  # @param description [String] Description of the resource.
  # @param mime_type [String, nil] MIME type (auto-detected if nil).
  # @return [Resource] A new Resource instance configured for the image data.
  def self.from_image_data(uri:, image_data:, name:, description: nil, mime_type: nil)
    require_relative "image_util"

    # Detect or validate MIME type
    detected_mime_type = VectorMCP::ImageUtil.detect_image_format(image_data)
    final_mime_type = mime_type || detected_mime_type

    raise ArgumentError, "Could not determine MIME type for image data" unless final_mime_type

    default_description = description || "Image resource: #{name}"

    handler = lambda do |_params|
      VectorMCP::ImageUtil.to_mcp_image_content(image_data, mime_type: final_mime_type)
    end

    new(uri, name, default_description, final_mime_type, handler)
  end
end

#uriObject

URI, String The unique URI identifying the resource.



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/vector_mcp/definitions.rb', line 120

Resource = Struct.new(:uri, :name, :description, :mime_type, :handler) do
  # Converts the resource to its MCP definition hash.
  # @return [Hash] A hash representing the resource in MCP format.
  def as_mcp_definition
    {
      uri: uri.to_s,
      name: name,
      description: description,
      mimeType: mime_type
    }.compact
  end

  # Checks if this resource represents an image.
  # @return [Boolean] True if the resource's MIME type indicates an image.
  def image_resource?
    !!mime_type&.start_with?("image/")
  end

  # Class method to create an image resource from a file path.
  # @param uri [String] The URI for the resource.
  # @param file_path [String] Path to the image file.
  # @param name [String] Human-readable name for the resource.
  # @param description [String] Description of the resource.
  # @return [Resource] A new Resource instance configured for the image file.
  def self.from_image_file(uri:, file_path:, name: nil, description: nil)
    raise ArgumentError, "Image file not found: #{file_path}" unless File.exist?(file_path)

    # Auto-detect MIME type
    require_relative "image_util"
    image_data = File.binread(file_path)
    detected_mime_type = VectorMCP::ImageUtil.detect_image_format(image_data)

    raise ArgumentError, "Could not detect image format for file: #{file_path}" unless detected_mime_type

    # Generate name and description if not provided
    default_name = name || File.basename(file_path)
    default_description = description || "Image file: #{file_path}"

    handler = lambda do |_params|
      VectorMCP::ImageUtil.file_to_mcp_image_content(file_path)
    end

    new(uri, default_name, default_description, detected_mime_type, handler)
  end

  # Class method to create an image resource from binary data.
  # @param uri [String] The URI for the resource.
  # @param image_data [String] Binary image data.
  # @param name [String] Human-readable name for the resource.
  # @param description [String] Description of the resource.
  # @param mime_type [String, nil] MIME type (auto-detected if nil).
  # @return [Resource] A new Resource instance configured for the image data.
  def self.from_image_data(uri:, image_data:, name:, description: nil, mime_type: nil)
    require_relative "image_util"

    # Detect or validate MIME type
    detected_mime_type = VectorMCP::ImageUtil.detect_image_format(image_data)
    final_mime_type = mime_type || detected_mime_type

    raise ArgumentError, "Could not determine MIME type for image data" unless final_mime_type

    default_description = description || "Image resource: #{name}"

    handler = lambda do |_params|
      VectorMCP::ImageUtil.to_mcp_image_content(image_data, mime_type: final_mime_type)
    end

    new(uri, name, default_description, final_mime_type, handler)
  end
end

Class Method Details

.from_image_data(uri:, image_data:, name:, description: nil, mime_type: nil) ⇒ Resource

Class method to create an image resource from binary data.

Parameters:

  • uri (String)

    The URI for the resource.

  • image_data (String)

    Binary image data.

  • name (String)

    Human-readable name for the resource.

  • description (String) (defaults to: nil)

    Description of the resource.

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

    MIME type (auto-detected if nil).

Returns:

  • (Resource)

    A new Resource instance configured for the image data.

Raises:

  • (ArgumentError)


172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/vector_mcp/definitions.rb', line 172

def self.from_image_data(uri:, image_data:, name:, description: nil, mime_type: nil)
  require_relative "image_util"

  # Detect or validate MIME type
  detected_mime_type = VectorMCP::ImageUtil.detect_image_format(image_data)
  final_mime_type = mime_type || detected_mime_type

  raise ArgumentError, "Could not determine MIME type for image data" unless final_mime_type

  default_description = description || "Image resource: #{name}"

  handler = lambda do |_params|
    VectorMCP::ImageUtil.to_mcp_image_content(image_data, mime_type: final_mime_type)
  end

  new(uri, name, default_description, final_mime_type, handler)
end

.from_image_file(uri:, file_path:, name: nil, description: nil) ⇒ Resource

Class method to create an image resource from a file path.

Parameters:

  • uri (String)

    The URI for the resource.

  • file_path (String)

    Path to the image file.

  • name (String) (defaults to: nil)

    Human-readable name for the resource.

  • description (String) (defaults to: nil)

    Description of the resource.

Returns:

  • (Resource)

    A new Resource instance configured for the image file.

Raises:

  • (ArgumentError)


144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/vector_mcp/definitions.rb', line 144

def self.from_image_file(uri:, file_path:, name: nil, description: nil)
  raise ArgumentError, "Image file not found: #{file_path}" unless File.exist?(file_path)

  # Auto-detect MIME type
  require_relative "image_util"
  image_data = File.binread(file_path)
  detected_mime_type = VectorMCP::ImageUtil.detect_image_format(image_data)

  raise ArgumentError, "Could not detect image format for file: #{file_path}" unless detected_mime_type

  # Generate name and description if not provided
  default_name = name || File.basename(file_path)
  default_description = description || "Image file: #{file_path}"

  handler = lambda do |_params|
    VectorMCP::ImageUtil.file_to_mcp_image_content(file_path)
  end

  new(uri, default_name, default_description, detected_mime_type, handler)
end

Instance Method Details

#as_mcp_definitionHash

Converts the resource to its MCP definition hash.

Returns:

  • (Hash)

    A hash representing the resource in MCP format.



123
124
125
126
127
128
129
130
# File 'lib/vector_mcp/definitions.rb', line 123

def as_mcp_definition
  {
    uri: uri.to_s,
    name: name,
    description: description,
    mimeType: mime_type
  }.compact
end

#image_resource?Boolean

Checks if this resource represents an image.

Returns:

  • (Boolean)

    True if the resource’s MIME type indicates an image.



134
135
136
# File 'lib/vector_mcp/definitions.rb', line 134

def image_resource?
  !!mime_type&.start_with?("image/")
end