Class: VectorMCP::Definitions::Resource
- Inherits:
-
Struct
- Object
- Struct
- VectorMCP::Definitions::Resource
- 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
-
#description ⇒ Object
String A description of the resource content.
-
#handler ⇒ Object
Proc A callable that returns the content of the resource.
-
#mime_type ⇒ Object
String The MIME type of the resource content (e.g., “text/plain”, “application/json”, “image/jpeg”).
-
#name ⇒ Object
String A human-readable name for the resource.
-
#uri ⇒ Object
URI, String The unique URI identifying the resource.
Class Method Summary collapse
-
.from_image_data(uri:, image_data:, name:, description: nil, mime_type: nil) ⇒ Resource
Class method to create an image resource from binary data.
-
.from_image_file(uri:, file_path:, name: nil, description: nil) ⇒ Resource
Class method to create an image resource from a file path.
Instance Method Summary collapse
-
#as_mcp_definition ⇒ Hash
Converts the resource to its MCP definition hash.
-
#image_resource? ⇒ Boolean
Checks if this resource represents an image.
Instance Attribute Details
#description ⇒ Object
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 |
#handler ⇒ Object
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_type ⇒ Object
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 |
#name ⇒ Object
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 |
#uri ⇒ Object
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.
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.
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_definition ⇒ Hash
Converts the resource to its MCP definition hash.
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.
134 135 136 |
# File 'lib/vector_mcp/definitions.rb', line 134 def image_resource? !!mime_type&.start_with?("image/") end |