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

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
# 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

  # 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
# 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

  # 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
# 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

  # 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
# 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

  # 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

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.



34
35
36
37
38
39
# File 'lib/vector_mcp/definitions.rb', line 34

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

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