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:
- 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_PREAMBLEand context from previous steps; - Runs
qwenin 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
wolframscriptbinary 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
qwenCLI 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-sslmode only when used locally; PGP encryption via QR code is recommended for end-to-end protection.
Security configuration is implemented at three levels:
- 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 viasubprocess.run(..., timeout=...). - File: All graphs are saved exclusively in
/tmp/mrl_graphics/, and sessions are stored inPROJECT_DIR/sessions/. TheGRAPHICS_DIRpath is checked for absolute validity and existence viais_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, initializessession_YYYYMMDD_HHMMSS.mdand_assets/./continue—resumes the last active session./resume <id>—opens a specific session by ID./peek—displays the lastPEEK_LINES=40lines of the current log./archive—moves a session toarchived/, removing it from the index./close—finalizes a session: copiessolution.wltoalgorithms/, generatessummary.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[...].
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 includesdescription.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: specifyWHITELIST_EMAIL,PROJECT_DIR, andQWEN_CMD. - Run initialization:
bash init.sh—this will createsessions/,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
No comments yet.