Class: Mathpix::MCP::Tools::ConvertStrokesTool

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

Overview

Convert Strokes Tool

Converts handwritten strokes to LaTeX, text, or other formats Thin delegate to Mathpix::Client#snap with strokes format

Class Method Summary collapse

Class Method Details

.artifact_contents(result) ⇒ Object

Available OCR formats as a format => content hash.



86
87
88
89
90
91
92
93
# File 'lib/mathpix/mcp/tools/convert_strokes_tool.rb', line 86

def self.artifact_contents(result)
  {
    'latex' => result.latex,
    'text' => result.text,
    'mathml' => result.mathml,
    'asciimath' => result.asciimath
  }.compact
end

.call(strokes:, server_context:, formats: nil, width: nil, height: nil, output_path: nil) ⇒ Object



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

def self.call(strokes:, server_context:, formats: nil, width: nil, height: nil, output_path: nil)
  safe_execute do
    client = mathpix_client(server_context)

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

    # Delegate to the strokes endpoint (transposes points internally)
    options = { formats: output_formats }
    options[:width] = width if width
    options[:height] = height if height
    result = client.convert_strokes(strokes, **options)

    # Collect available formats and write them to disk so the recognized
    # content never enters the model context.
    contents = artifact_contents(result)
    base = output_path && !output_path.empty? ? output_path : default_artifact_path('strokes', 'tex')
    saved = write_artifacts(contents, base)

    response_data = {
      success: true,
      input_type: 'strokes',
      stroke_count: strokes.length,
      formats: output_formats,
      confidence: result.confidence,
      is_handwritten: result.handwritten?,
      saved_files: saved,
      preview: preview_of(result.latex || result.text)
    }

    json_response(response_data)
  end
end