Skip to main content

Mathemagic

An AI calculator agent that interprets and solves science, technology, mathematics, and engineering problems in Python.

Mathemagic

Features

  • Converts natural language math problems to Python code using LLMs (Claude is currently the only supported model)
  • Handles units using the Pint library
  • Provides access through MCP and CLI interfaces
  • Supports complex mathematical operations with proper unit handling

Examples

  • "Convert 100 kilometers per hour to miles per hour"
  • "What is the volume of a cylinder with radius 5cm and height 10cm?"
  • "If I have 3 apples and 4 oranges, how many pieces of fruit do I have?"
  • "Calculate the kinetic energy of a 2kg object moving at 10 meters per second"

Basic Usage

Enter command to start CLI

mathemagic

CLI initializes, and you type in your math problem

Mathemagic Calculator (Press Ctrl+C to exit)
Enter your math problem:
: Two hundred miles times 634 feet in meters squared.
Processing: Two hundred miles times 634 feet in meters squared.

Result:
62198956.89216 meter ** 2

Installation

# Clone the repository
git clone https://github.com/yourusername/mathemagic.git
cd mathemagic

# Install dependencies using UV
uv pip install -e .

Set up environment variables. Create a .env file with your Anthropic API key in the project root directory.

echo "ANTHROPIC_API_KEY=your_api_key_here" >> .env

Usage

Using the CLI

# Run in interactive mode
mathemagic

# Run the CLI with a math problem
mathemagic "100 miles times 234 acres in liters"

# Show the generated Python code along with the result
mathemagic "100 miles times 234 acres in liters" --output-python

# help menu
mathemagic --help

Output the Executed Python Code

Using the -p or --output-python flags

mathemagic -p
Mathemagic Calculator (Press Ctrl+C to exit)
Enter your math problem:
: Two hundred miles times 634 feet in meters squared.
Processing: Two hundred miles times 634 feet in meters squared.

Generated Python code:
```python
from pint import UnitRegistry

ureg = UnitRegistry()
Q_ = ureg.Quantity

# Define the variables with their respective units
miles = Q_(200, 'mile')  # distance in miles
feet = Q_(634, 'foot')  # distance in feet

# Perform the multiplication
result = miles * feet

# Convert the result to meters squared
result_m2 = result.to('meter**2')

print(result_m2)
```
Result:
62198956.89216 meter ** 2

Handles Arbitrary Units

mathemagic uses Pint to handle units.

Enter another problem:
: 100 bananas times 66 guavas
Processing: 100 bananas times 66 guavas

Result:
6600 banana * guava

Using the MCP Server

Starting the Server

# Start the MCP server
uv run src/mathemagic/server.py

Add the MCP Server to Claude Code

Assuming you have claude code installed, type this command, then follow the wizard. You'll enter the command above when prompted.

claude mcp add

Architecture

The application consists of:

  • mathemagic.py: Core functionality for converting natural language to Python code and executing it
  • server.py: MCP server that provides API endpoints for the calculator
  • cli.py: Command-line interface for interacting with the calculator

TODO

  1. Clean up python output through server (currently outputs in a dict and not pretty)
  2. Improve CLI (make it pretty)
  3. Add symbolic math support
  4. Add linear algebra support
  5. Update to use openrouter
  6. Add tests
  7. Astropy support?
  8. Auth as alternative to .env for remote use?

Metadata

Release files for mathemagic-ai 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mathemagic-ai 0.1.0
File Size Uploaded Provenance
mathemagic_ai-0.1.0.tar.gz 5.9 kB With provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for mathemagic-ai 0.1.0
File Interpreter ABI Platform
mathemagic_ai-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 12.9 kB

Release files / mathemagic_ai-0.1.0.tar.gz

Download URL mathemagic_ai-0.1.0.tar.gz
Size 5.9 kB
Tags Source
SHA-256 checksum
How to use checksums
9746b7683ed8515ed1339725dfa407492612e3a8937e25a3a3c80f15bf9993cf
BLAKE2b-256 checksum
How to use checksums
27e3cef8fd79b3e6eb1b9978b3dab71be12a3bc2b12d51ead544e3c1a96c72a7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Mar 30, 2025.

Transparency log

Release files / mathemagic_ai-0.1.0-py3-none-any.whl

Download URL mathemagic_ai-0.1.0-py3-none-any.whl
Size 7.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fc4a4c24f2d5e286bf758248b76e3c9bb6d1e70dede7b2c2b8fe643df8fea7a3
BLAKE2b-256 checksum
How to use checksums
e4e5776621c0f8572a69a478034e6be4f63fe09cd8cb2fd244fe48b42e314540
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Mar 30, 2025.

Transparency log

Security for mathemagic-ai 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for mathemagic_ai-0.1.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Mar 30, 2025.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page