Skip to main content

MAGI Developer Guide: Encoding & Decoding

This guide provides examples of how to programmatically encode (create) and decode (parse) MAGI (Markdown for Agent Guidance & Instruction) documents in different environments. MAGI files typically use the .mda extension.

Core Concepts

  • Encoding: Combining separate pieces of information (metadata from Front Matter, human-readable Markdown content, AI instructions within ai-script blocks, and document relationships defined in Footnotes) into the MAGI string format (.mda).
  • Decoding: Parsing a MAGI string (from an .mda file or source) to extract its distinct structured components: YAML Front Matter, the main Markdown content, ai-script JSON blocks, and Footnote relationship JSON payloads.
Common libraries used:
  • YAML Parsers: For handling the Front Matter (e.g., js-yaml or gray-matter in Node.js, PyYAML or python-frontmatter in Python).
  • Markdown Parsers: While standard Markdown parsers can render the core content, specialized logic or regular expressions are often needed to correctly identify and extract the ai-script blocks and JSON-based footnotes without treating them as plain text or standard code/footnotes. Libraries like marked or markdown-it (Node.js) and markdown or mistune (Python) can be adapted.
  • JSON Parsers: Built-in functionality in most languages (JSON.parse/JSON.stringify in JS, json module in Python).

TypeScript / JavaScript Examples

Libraries needed: gray-matter (recommended for robust front matter parsing), marked (or similar, optional for final Markdown rendering).

Encoding MAGI


Python Examples

Libraries needed: python-frontmatter (handles YAML front matter effectively), PyYAML (usually a dependency of python-frontmatter), json (built-in).

Encoding MAGI

Decoding MAGI

Some more text here. decoded = decode_magi(magi_input) print(json.dumps(decoded, indent=2))

Expected output structure is similar to the TypeScript example

Decoding MAGI via Command Line (Conceptual)

Directly and reliably decoding MAGI (.mda) files using only standard Unix command-line tools (grep, sed, awk) is complex due to the nested structures (YAML, JSON within Markdown). A more robust command-line approach typically involves a dedicated script:
  1. Script Input: The script (e.g., Python, Node.js using the libraries above) accepts a .mda file path or piped input.
  2. Parsing Logic: It uses libraries like python-frontmatter or gray-matter, combined with regular expressions or custom parsing logic, to extract the Front Matter, ai-script blocks, footnote relationships, and main content.
  3. Structured Output: The script outputs the extracted components in a machine-readable format, such as a single JSON object containing all parts, or separate files for each component (e.g., metadata.yaml, content.md, scripts.json, relationships.json).
Example Python Script Snippet (Conceptual):
This scripted approach provides a reliable way to integrate MAGI parsing into command-line workflows.