Class: Mathpix::MCP::Tools::ConvertImageTool

Inherits:
BaseTool
  • Object
show all
Defined in:
lib/mathpix/mcp/tools/convert_image_tool.rb

Overview

Convert Image Tool

Converts images (PNG, JPG, etc.) to LaTeX, text, or other formats Thin delegate to Mathpix::Client#snap

Class Method Summary collapse

Class Method Details

.artifact_contents(result, include_line_data, include_word_data) ⇒ Object

Available OCR formats (and optional bounding-box data) as a format => content hash.



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/mathpix/mcp/tools/convert_image_tool.rb', line 94

def self.artifact_contents(result, include_line_data, include_word_data)
  contents = {
    'latex' => result.latex,
    'text' => result.text,
    'mathml' => result.mathml,
    'asciimath' => result.asciimath
  }.compact
  contents['line_data'] = result.line_data if include_line_data && !result.line_data.empty?
  contents['word_data'] = result.word_data if include_word_data && !result.word_data.empty?
  contents
end

.call(image_path:, server_context:, formats: nil, include_line_data: false, include_word_data: false, confidence_threshold: nil, output_path: nil) ⇒ Object



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
# File 'lib/mathpix/mcp/tools/convert_image_tool.rb', line 48

def self.call(image_path:, server_context:, formats: nil, include_line_data: false, include_word_data: false,
              confidence_threshold: nil, output_path: nil)
  safe_execute do
    client = mathpix_client(server_context)

    # Normalize path (expand ~, resolve relative paths)
    image_path = normalize_path(image_path) unless url?(image_path)

    # Extract formats or use defaults
    output_formats = extract_formats(formats, client)

    # Delegate to core gem
    result = client.snap(image_path,
                         **snap_options(output_formats, include_line_data, include_word_data,
                                        confidence_threshold))

    # Write recognized content (and any requested bounding-box data) to
    # files so it never enters the model context.
    contents = artifact_contents(result, include_line_data, include_word_data)
    stem = url?(image_path) ? 'image' : File.basename(image_path, File.extname(image_path))
    base = output_path && !output_path.empty? ? output_path : default_artifact_path(stem, 'tex')

    json_response(
      success: true,
      image_path: image_path,
      formats: output_formats,
      confidence: result.confidence,
      is_printed: result.printed?,
      is_handwritten: result.handwritten?,
      saved_files: write_artifacts(contents, base),
      preview: preview_of(result.latex || result.text)
    )
  end
end

.snap_options(formats, include_line_data, include_word_data, confidence_threshold) ⇒ Object

Build the options hash passed to Client#snap.



84
85
86
87
88
89
90
# File 'lib/mathpix/mcp/tools/convert_image_tool.rb', line 84

def self.snap_options(formats, include_line_data, include_word_data, confidence_threshold)
  options = { formats: formats }
  options[:include_line_data] = true if include_line_data
  options[:include_word_data] = true if include_word_data
  options[:confidence_threshold] = confidence_threshold if confidence_threshold
  options
end