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.
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 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 |
# File 'lib/vector_mcp/definitions.rb', line 90 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).
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 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 |
# File 'lib/vector_mcp/definitions.rb', line 90 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").
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 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 |
# File 'lib/vector_mcp/definitions.rb', line 90 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.
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 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 |
# File 'lib/vector_mcp/definitions.rb', line 90 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.
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 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 |
# File 'lib/vector_mcp/definitions.rb', line 90 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.
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/vector_mcp/definitions.rb', line 142 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.
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/vector_mcp/definitions.rb', line 114 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.
93 94 95 96 97 98 99 100 |
# File 'lib/vector_mcp/definitions.rb', line 93 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.
104 105 106 |
# File 'lib/vector_mcp/definitions.rb', line 104 def image_resource? !!mime_type&.start_with?("image/") end |