Back to Home

AI agent for mathematics on Mac mini

Described is the creation of an autonomous mathematical laboratory on Mac mini using Wolfram Engine, Qwen Code CLI and Delta Chat. The system accepts tasks from the messenger, generates and executes Wolfram code locally, returns results with graphs and saves the full session history.

Mathematical laboratory on Mac mini: AI + Wolfram
Advertisement 728x90

AI Agent for Mathematical Research: Wolfram Engine + Qwen Code CLI on Mac mini

A fully autonomous laboratory for mathematical research has been created, accessible via a messaging app—without cloud APIs, external servers, or loss of control over code and data. The system runs locally on a Mac mini, receives tasks from Delta Chat, generates algorithms in the Wolfram Language using Qwen Code CLI, executes them through Wolfram Engine, and returns results with graphs directly to the chat. This isn’t an interface to a pre-built solution—it’s a research environment that captures the entire thought process, from problem formulation to verification.

Architecture Without Intermediate Services

The entire message-processing pipeline is implemented as a local workflow with no dependencies on third-party APIs or SaaS platforms. The key advantage is complete control over input prompts, executed code, and data lifecycles. There are no hidden cloud calls, no automatic sending of code to LLM services, and no collection of metrics outside the user’s machine.

A message from Delta Chat (via IMAP/SMTP) enters the Python bot math_bot.py, which:

Google AdInline article slot
  • Validates the sender against a WHITELIST_EMAIL;
  • Starts a new session or resumes an existing one;
  • Constructs an atomic prompt that includes a system preamble from QWEN_PREAMBLE and context from previous steps;
  • Runs qwen in headless mode with the flags --yolo --continue;
  • Captures stdout in JSON format;
  • Parses graphic files from /tmp/mrl_graphics/ by extensions .png, .jpg, .svg, .pdf;
  • Sends the result back to the chat while saving the history in sessions/.

Importantly, Qwen Code CLI isn’t used as a “black box.” It’s launched as a local subprocess, and all wolframscript commands are executed in a strictly defined directory with explicit path specifications. Graphs are exported only to a dedicated temporary folder—no side effects are written to the home directory or ~/Downloads.

Environment Requirements and Execution Security

The system is designed for macOS 14+, but it’s also compatible with Linux and Windows through cross-platform components. Critical dependencies include:

  • Wolfram Engine 14.1+—the free version of the engine available for personal use. It’s installed via pkg, adding the wolframscript binary to PATH. All computations happen locally; no expressions are sent to Wolfram Cloud.
  • Qwen Code CLI v3.5 Plus—a headless version of the model running on Node.js. It doesn’t require an API key. The model is loaded locally, weighs about 2.1 GB (quantized GGUF), and is launched via the qwen CLI with --model qwen3.5-plus.Q4_K_M.gguf.
  • Delta Chat RPC Server—a native binary (deltachat-rpc-server) that manages the connection to the mailbox. It operates in --no-verify-ssl mode only when used locally; PGP encryption via QR code is recommended for end-to-end protection.

Security configuration is implemented at three levels:

Google AdInline article slot
  • Network: Delta Chat uses standard IMAP/SMTP ports without port forwarding, VPNs, or whitelisted IPs. Traffic is encrypted with TLS 1.3.
  • Process: Qwen is launched with a timeout of QWEN_TIMEOUT=600, limiting the execution of complex tasks. If the limit is exceeded, the process is forcibly terminated via subprocess.run(..., timeout=...).
  • File: All graphs are saved exclusively in /tmp/mrl_graphics/, and sessions are stored in PROJECT_DIR/sessions/. The GRAPHICS_DIR path is checked for absolute validity and existence via is_dir() before use.

Session Lifecycle Implementation

The bot supports seven session-management commands, each of which modifies the state in SESSIONS_DIR:

  • /start—creates a new session with a unique UUID, initializes session_YYYYMMDD_HHMMSS.md and _assets/.
  • /continue—resumes the last active session.
  • /resume <id>—opens a specific session by ID.
  • /peek—displays the last PEEK_LINES=40 lines of the current log.
  • /archive—moves a session to archived/, removing it from the index.
  • /close—finalizes a session: copies solution.wl to algorithms/, generates summary.md, and clears _assets/.
  • /help—outputs help without access to internal paths.

Each session is a Markdown file documenting the dialogue chronologically, with every step containing four mandatory blocks:

  • Verbal algorithm (numbered steps);
  • Wolfram Language code (exact text ready for execution);
  • Actual output from wolframscript (stdout/stderr);
  • Interpretation of the result (1–2 sentences).

This ensures reproducibility: any step can be manually repeated via wolframscript -code "...", and all graphs can be regenerated from Export[...].

Google AdInline article slot

Example of Working Interaction

A user sends the following message to the chat:

Find the intersection points of y = x^2 and y = 2x + 3. Plot the graph.

The bot forwards the request to Qwen Code CLI with the preamble:

Use only wolframscript (not Python). Save graphs in /tmp/mrl_graphics/. Labels on graphs should be in Latin characters.

Find the intersection points of y = x^2 and y = 2x + 3. Plot the graph.

Qwen returns a structured response:

  • Verbal algorithm

1. Set the functions equal: x^2 == 2x + 3

2. Rearrange to standard form: x^2 - 2x - 3 == 0

3. Solve the quadratic equation

4. Plot both functions on the same graph

  • Wolfram Code

---wl---

eq = x^2 == 2 x + 3;

solutions = Solve[eq, x];

Plot[{x^2, 2 x + 3}, {x, -3, 5},

Epilog -> {Red, PointSize[Large],

Point[{x, x^2} /. solutions]},

PlotLabel -> "Intersection of y=x^2 and y=2x+3",

AxesLabel -> {"x", "y"}]

Export["/tmp/mrl_graphics/intersection.png", %]

---wl---

  • Wolframscript Result

{{x -> -1}, {x -> 3}}

$Graphics$

  • Interpretation of the Result

Intersection points: (-1, 1) and (3, 9). The graph is saved as intersection.png.

The bot extracts the PNG from /tmp/mrl_graphics/, sends it to the chat, and adds this block to the session log.

What Matters

  • Local execution guarantees privacy: no mathematical object, formula, or graph ever leaves the user’s machine.
  • Wolfram Engine isn’t a replacement for Python—it’s a specialized calculator: it provides precise symbolic algebra, visualization, and numerical methods without the need to write custom solvers.
  • Qwen Code CLI acts as a code generator, not a solver: the model never outputs a “final answer” but only generates executable code and waits for actual output from wolframscript.
  • Delta Chat provides end-to-end encryption without servers: PGP keys are exchanged via QR code, and messages are encrypted on-device.
  • The algorithms/ structure creates a repository of reproducible solutions: each algorithm includes description.md, reasoning.md, and `solution.wl”—ready for integration into CI/CD pipelines or documentation.

Setup and Deployment

To deploy, you’ll need to:

  • Install Wolfram Engine from the official website and run wolframscript --install.
  • Install Node.js 20+ and execute npm install -g qwen-cli.
  • Create a conda environment: conda create -n mathbot python=3.12 && conda activate mathbot.
  • Install dependencies: pip install -r requirements.txt.
  • Configure config.py: specify WHITELIST_EMAIL, PROJECT_DIR, and QWEN_CMD.
  • Run initialization: bash init.sh—this will create sessions/, algorithms/, and _index.md.
  • Start the bot: python math_bot.py.

After startup, the bot begins polling the mailbox every 30 seconds. For testing, simply send /start from an authorized email address. Sessions are logged in real time, and errors are recorded in math_bot.log.

— Editorial Team

Advertisement 728x90

Read Next