Class: VectorMCP::Definitions::Tool

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

Overview

Represents a tool that can be executed by the AI model.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#descriptionObject

String A human-readable description of what the tool does.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/vector_mcp/definitions.rb', line 21

Tool = Struct.new(:name, :description, :input_schema, :handler) do
  # Converts the tool to its MCP definition hash.
  # @return [Hash] A hash representing the tool in MCP format.
  def as_mcp_definition
    {
      name: name,
      description: description,
      inputSchema: input_schema # Expected to be a Hash representing JSON Schema
    }.compact # Remove nil values
  end

  # Class method to create a tool whose input_schema already declares a
  # base64-encoded image property. Mirrors Resource.from_image_file and
  # Prompt.with_image_support — keeps schema-building logic with the
  # definition, not with the registry.
  #
  # @param name [String] Unique name for the tool.
  # @param description [String] Human-readable description.
  # @param image_parameter [String] Name of the image parameter.
  # @param additional_parameters [Hash] Additional JSON Schema properties.
  # @param required_parameters [Array<String>] Required parameter names.
  # @param handler [Proc] Tool handler block.
  # @return [Tool]
  def self.with_image_support(name:, description:, image_parameter: "image",
                              additional_parameters: {}, required_parameters: [], &handler)
    image_property = {
      type: "string",
      description: "Base64 encoded image data or file path to image",
      contentEncoding: "base64",
      contentMediaType: "image/*"
    }

    input_schema = {
      type: "object",
      properties: { image_parameter => image_property }.merge(additional_parameters),
      required: required_parameters
    }

    new(name, description, input_schema, handler)
  end

  # Checks if this tool supports image inputs based on its input schema.
  # @return [Boolean] True if the tool's input schema includes image properties.
  def supports_image_input?
    return false unless input_schema.is_a?(Hash)

    properties = extract_schema_properties
    properties.any? { |_prop, schema| image_property?(schema) }
  end

  private

  # Extracts properties from the input schema, handling both string and symbol keys.
  def extract_schema_properties
    input_schema["properties"] || input_schema[:properties] || {}
  end

  # Checks if a property schema represents an image input.
  def image_property?(schema)
    return false unless schema.is_a?(Hash)

    explicit_image_format?(schema) || base64_image_content?(schema)
  end

  # Checks if the schema explicitly declares image format.
  def explicit_image_format?(schema)
    schema["format"] == "image" || schema[:format] == "image"
  end

  # Checks if the schema represents base64-encoded image content.
  def base64_image_content?(schema)
    string_type_with_base64_encoding?(schema) && image_media_type?(schema)
  end

  # Checks if the schema is a string type with base64 encoding.
  def string_type_with_base64_encoding?(schema)
    (schema["type"] == "string" && schema["contentEncoding"] == "base64") ||
      (schema[:type] == "string" && schema[:contentEncoding] == "base64")
  end

  # Checks if the schema has an image media type.
  def image_media_type?(schema)
    schema["contentMediaType"]&.start_with?("image/") ||
      schema[:contentMediaType]&.start_with?("image/")
  end
end

#handlerObject

Proc A callable (e.g., a Proc or lambda) that executes the tool’s logic. It receives the tool input (a Hash) as its argument. The input hash structure should match the input_schema.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/vector_mcp/definitions.rb', line 21

Tool = Struct.new(:name, :description, :input_schema, :handler) do
  # Converts the tool to its MCP definition hash.
  # @return [Hash] A hash representing the tool in MCP format.
  def as_mcp_definition
    {
      name: name,
      description: description,
      inputSchema: input_schema # Expected to be a Hash representing JSON Schema
    }.compact # Remove nil values
  end

  # Class method to create a tool whose input_schema already declares a
  # base64-encoded image property. Mirrors Resource.from_image_file and
  # Prompt.with_image_support — keeps schema-building logic with the
  # definition, not with the registry.
  #
  # @param name [String] Unique name for the tool.
  # @param description [String] Human-readable description.
  # @param image_parameter [String] Name of the image parameter.
  # @param additional_parameters [Hash] Additional JSON Schema properties.
  # @param required_parameters [Array<String>] Required parameter names.
  # @param handler [Proc] Tool handler block.
  # @return [Tool]
  def self.with_image_support(name:, description:, image_parameter: "image",
                              additional_parameters: {}, required_parameters: [], &handler)
    image_property = {
      type: "string",
      description: "Base64 encoded image data or file path to image",
      contentEncoding: "base64",
      contentMediaType: "image/*"
    }

    input_schema = {
      type: "object",
      properties: { image_parameter => image_property }.merge(additional_parameters),
      required: required_parameters
    }

    new(name, description, input_schema, handler)
  end

  # Checks if this tool supports image inputs based on its input schema.
  # @return [Boolean] True if the tool's input schema includes image properties.
  def supports_image_input?
    return false unless input_schema.is_a?(Hash)

    properties = extract_schema_properties
    properties.any? { |_prop, schema| image_property?(schema) }
  end

  private

  # Extracts properties from the input schema, handling both string and symbol keys.
  def extract_schema_properties
    input_schema["properties"] || input_schema[:properties] || {}
  end

  # Checks if a property schema represents an image input.
  def image_property?(schema)
    return false unless schema.is_a?(Hash)

    explicit_image_format?(schema) || base64_image_content?(schema)
  end

  # Checks if the schema explicitly declares image format.
  def explicit_image_format?(schema)
    schema["format"] == "image" || schema[:format] == "image"
  end

  # Checks if the schema represents base64-encoded image content.
  def base64_image_content?(schema)
    string_type_with_base64_encoding?(schema) && image_media_type?(schema)
  end

  # Checks if the schema is a string type with base64 encoding.
  def string_type_with_base64_encoding?(schema)
    (schema["type"] == "string" && schema["contentEncoding"] == "base64") ||
      (schema[:type] == "string" && schema[:contentEncoding] == "base64")
  end

  # Checks if the schema has an image media type.
  def image_media_type?(schema)
    schema["contentMediaType"]&.start_with?("image/") ||
      schema[:contentMediaType]&.start_with?("image/")
  end
end

#input_schemaObject

Hash A JSON Schema object describing the expected input for the tool.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/vector_mcp/definitions.rb', line 21

Tool = Struct.new(:name, :description, :input_schema, :handler) do
  # Converts the tool to its MCP definition hash.
  # @return [Hash] A hash representing the tool in MCP format.
  def as_mcp_definition
    {
      name: name,
      description: description,
      inputSchema: input_schema # Expected to be a Hash representing JSON Schema
    }.compact # Remove nil values
  end

  # Class method to create a tool whose input_schema already declares a
  # base64-encoded image property. Mirrors Resource.from_image_file and
  # Prompt.with_image_support — keeps schema-building logic with the
  # definition, not with the registry.
  #
  # @param name [String] Unique name for the tool.
  # @param description [String] Human-readable description.
  # @param image_parameter [String] Name of the image parameter.
  # @param additional_parameters [Hash] Additional JSON Schema properties.
  # @param required_parameters [Array<String>] Required parameter names.
  # @param handler [Proc] Tool handler block.
  # @return [Tool]
  def self.with_image_support(name:, description:, image_parameter: "image",
                              additional_parameters: {}, required_parameters: [], &handler)
    image_property = {
      type: "string",
      description: "Base64 encoded image data or file path to image",
      contentEncoding: "base64",
      contentMediaType: "image/*"
    }

    input_schema = {
      type: "object",
      properties: { image_parameter => image_property }.merge(additional_parameters),
      required: required_parameters
    }

    new(name, description, input_schema, handler)
  end

  # Checks if this tool supports image inputs based on its input schema.
  # @return [Boolean] True if the tool's input schema includes image properties.
  def supports_image_input?
    return false unless input_schema.is_a?(Hash)

    properties = extract_schema_properties
    properties.any? { |_prop, schema| image_property?(schema) }
  end

  private

  # Extracts properties from the input schema, handling both string and symbol keys.
  def extract_schema_properties
    input_schema["properties"] || input_schema[:properties] || {}
  end

  # Checks if a property schema represents an image input.
  def image_property?(schema)
    return false unless schema.is_a?(Hash)

    explicit_image_format?(schema) || base64_image_content?(schema)
  end

  # Checks if the schema explicitly declares image format.
  def explicit_image_format?(schema)
    schema["format"] == "image" || schema[:format] == "image"
  end

  # Checks if the schema represents base64-encoded image content.
  def base64_image_content?(schema)
    string_type_with_base64_encoding?(schema) && image_media_type?(schema)
  end

  # Checks if the schema is a string type with base64 encoding.
  def string_type_with_base64_encoding?(schema)
    (schema["type"] == "string" && schema["contentEncoding"] == "base64") ||
      (schema[:type] == "string" && schema[:contentEncoding] == "base64")
  end

  # Checks if the schema has an image media type.
  def image_media_type?(schema)
    schema["contentMediaType"]&.start_with?("image/") ||
      schema[:contentMediaType]&.start_with?("image/")
  end
end

#nameObject

String The unique name of the tool.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/vector_mcp/definitions.rb', line 21

Tool = Struct.new(:name, :description, :input_schema, :handler) do
  # Converts the tool to its MCP definition hash.
  # @return [Hash] A hash representing the tool in MCP format.
  def as_mcp_definition
    {
      name: name,
      description: description,
      inputSchema: input_schema # Expected to be a Hash representing JSON Schema
    }.compact # Remove nil values
  end

  # Class method to create a tool whose input_schema already declares a
  # base64-encoded image property. Mirrors Resource.from_image_file and
  # Prompt.with_image_support — keeps schema-building logic with the
  # definition, not with the registry.
  #
  # @param name [String] Unique name for the tool.
  # @param description [String] Human-readable description.
  # @param image_parameter [String] Name of the image parameter.
  # @param additional_parameters [Hash] Additional JSON Schema properties.
  # @param required_parameters [Array<String>] Required parameter names.
  # @param handler [Proc] Tool handler block.
  # @return [Tool]
  def self.with_image_support(name:, description:, image_parameter: "image",
                              additional_parameters: {}, required_parameters: [], &handler)
    image_property = {
      type: "string",
      description: "Base64 encoded image data or file path to image",
      contentEncoding: "base64",
      contentMediaType: "image/*"
    }

    input_schema = {
      type: "object",
      properties: { image_parameter => image_property }.merge(additional_parameters),
      required: required_parameters
    }

    new(name, description, input_schema, handler)
  end

  # Checks if this tool supports image inputs based on its input schema.
  # @return [Boolean] True if the tool's input schema includes image properties.
  def supports_image_input?
    return false unless input_schema.is_a?(Hash)

    properties = extract_schema_properties
    properties.any? { |_prop, schema| image_property?(schema) }
  end

  private

  # Extracts properties from the input schema, handling both string and symbol keys.
  def extract_schema_properties
    input_schema["properties"] || input_schema[:properties] || {}
  end

  # Checks if a property schema represents an image input.
  def image_property?(schema)
    return false unless schema.is_a?(Hash)

    explicit_image_format?(schema) || base64_image_content?(schema)
  end

  # Checks if the schema explicitly declares image format.
  def explicit_image_format?(schema)
    schema["format"] == "image" || schema[:format] == "image"
  end

  # Checks if the schema represents base64-encoded image content.
  def base64_image_content?(schema)
    string_type_with_base64_encoding?(schema) && image_media_type?(schema)
  end

  # Checks if the schema is a string type with base64 encoding.
  def string_type_with_base64_encoding?(schema)
    (schema["type"] == "string" && schema["contentEncoding"] == "base64") ||
      (schema[:type] == "string" && schema[:contentEncoding] == "base64")
  end

  # Checks if the schema has an image media type.
  def image_media_type?(schema)
    schema["contentMediaType"]&.start_with?("image/") ||
      schema[:contentMediaType]&.start_with?("image/")
  end
end

Class Method Details

.with_image_support(name:, description:, image_parameter: "image", additional_parameters: {}, required_parameters: [], &handler) ⇒ Tool

Class method to create a tool whose input_schema already declares a base64-encoded image property. Mirrors Resource.from_image_file and Prompt.with_image_support — keeps schema-building logic with the definition, not with the registry.

Parameters:

  • name (String)

    Unique name for the tool.

  • description (String)

    Human-readable description.

  • image_parameter (String) (defaults to: "image")

    Name of the image parameter.

  • additional_parameters (Hash) (defaults to: {})

    Additional JSON Schema properties.

  • required_parameters (Array<String>) (defaults to: [])

    Required parameter names.

  • handler (Proc)

    Tool handler block.

Returns:



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/vector_mcp/definitions.rb', line 44

def self.with_image_support(name:, description:, image_parameter: "image",
                            additional_parameters: {}, required_parameters: [], &handler)
  image_property = {
    type: "string",
    description: "Base64 encoded image data or file path to image",
    contentEncoding: "base64",
    contentMediaType: "image/*"
  }

  input_schema = {
    type: "object",
    properties: { image_parameter => image_property }.merge(additional_parameters),
    required: required_parameters
  }

  new(name, description, input_schema, handler)
end

Instance Method Details

#as_mcp_definitionHash

Converts the tool to its MCP definition hash.

Returns:

  • (Hash)

    A hash representing the tool in MCP format.



24
25
26
27
28
29
30
# File 'lib/vector_mcp/definitions.rb', line 24

def as_mcp_definition
  {
    name: name,
    description: description,
    inputSchema: input_schema # Expected to be a Hash representing JSON Schema
  }.compact # Remove nil values
end

#supports_image_input?Boolean

Checks if this tool supports image inputs based on its input schema.

Returns:

  • (Boolean)

    True if the tool’s input schema includes image properties.



64
65
66
67
68
69
# File 'lib/vector_mcp/definitions.rb', line 64

def supports_image_input?
  return false unless input_schema.is_a?(Hash)

  properties = extract_schema_properties
  properties.any? { |_prop, schema| image_property?(schema) }
end