XBP-Europe/sagemath-mcp
If you are the rightful owner of sagemath-mcp and would like to certify it and/or have it hosted online, please leave a comment on the right or send an email to dayong@mcphub.com.
The SageMath MCP Server is a Model Context Protocol server that facilitates stateful SageMath computations for LLM clients using FastMCP as the transport layer.
SageMath MCP Server
A Model Context Protocol (MCP) server that exposes stateful SageMath computations to LLM clients. The server uses FastMCP as the transport layer and maintains a dedicated SageMath session for each MCP conversation so variables, functions, and assumptions persist across tool calls.
LLM Usage Notes
Clients connecting through MCP receive the following guidance:
- Stateful sessions — every conversation owns a dedicated Sage worker. Define symbols once
(e.g.,
var('x'),f = ...) and reuse them across subsequent tool calls. - Core toolset — use
evaluate_sagefor arbitrary Sage code, or reach for helpers likecalculate_expression,solve_equation,differentiate_expression,integrate_expression,matrix_multiply, andstatistics_summaryfor structured JSON outputs. - Runtime feedback — long computations emit heartbeat progress events roughly every 1.5 seconds;
the optional
timeoutparameter lets you extend or shorten the wait. - Session control —
reset_sage_sessionclears state whilecancel_sage_sessionrestarts the worker. Monitoring data (success/failure counts, latency, security violations) is available viaresource://sagemath/monitoring/metrics. - Security sandbox — the AST validator blocks arbitrary imports,
eval/exec, filesystem/process calls, and other unsafe constructs. Prefer Sage primitives; if a violation occurs, rewrite the workflow using supported APIs.
Features
- Stateful SageMath execution with per-session variable scope.
- Automatic LaTeX string generation for expression results (configurable per call).
- Support for capturing
stdoutoutput from Sage code. - Tools for evaluating Sage code, cancelling long-running jobs, and resetting the session.
- Optional resource exposing the active workspace summary (with idle timers) to clients.
- Higher-level helpers for common math tasks (calculation, equation solving, calculus, statistics, matrices).
- Fully type-annotated package (
py.typed) plus Ruff/pytest integration for CI.
Requirements
- Python 3.11+
- A local SageMath installation available on the
PATH(tested with Sage 10.x). - FastMCP-compatible MCP client (e.g. Claude Desktop).
Quick Start
Install from PyPI
pip install sagemath-mcp
# Run the server over stdio (default)
sagemath-mcp
# Or expose an HTTP endpoint
sagemath-mcp -- --transport streamable-http --host 127.0.0.1 --port 8314
If the command is not on your PATH, run python -m sagemath_mcp.server -- --help.
Develop from source
# Install dependencies (use uv or pip)
uv pip install -e .[cli]
# Run the server over stdio (default)
uv run sagemath-mcp
# Run with streaming-friendly HTTP transport
uv run sagemath-mcp -- --transport streamable-http --host 127.0.0.1 --port 8314
See for Windows/macOS tooling tips, Docker notes, and guidance on installing Sage locally.
Optional: start a Sage container automatically
If you'd like a ready-to-use Sage runtime without installing it locally, run:
make sage-container # or ./scripts/setup_sage_container.sh
On Windows PowerShell:
pwsh -File scripts/setup_sage_container.ps1
Docker Image
Build a ready-to-run container with the MCP server baked in:
docker build -t sagemath-mcp:latest .
docker run -p 8314:8314 sagemath-mcp:latest --transport streamable-http
This pulls the sagemath/sagemath:latest image (overridable via
SAGEMATH_MCP_DOCKER_IMAGE) and launches a long-lived container named sage-mcp
mounting the current repository at /workspace.
Released images are published to ghcr.io/xbp-europe/sagemath-mcp and signed with Cosign.
Verify a downloaded artifact with:
cosign verify ghcr.io/xbp-europe/sagemath-mcp:latest \
--certificate-identity "https://github.com/XBP-Europe/sagemath-mcp/.github/workflows/release.yml@refs/tags/vX.Y.Z" \
--certificate-oidc-issuer "https://token.actions.githubusercontent.com"
Docker Compose
To bootstrap a local SageMath MCP stack with a single command, use the provided
docker-compose.yml:
docker compose up --build
The compose service exposes port 8314 on both host and container and mounts the repository at
/workspace. Containers run as the non-root sage user (UID/GID 1000) to match the
base image. Tweak runtime settings by editing the environment block (for example,
increase SAGEMATH_MCP_EVAL_TIMEOUT or adjust SAGEMATH_MCP_MAX_STDOUT) before launch.
If your checkout directory is not writable by UID/GID 1000, run chown -R 1000:1000 .
or point the volume at a suitable path before bringing the stack up.
Kubernetes (Helm)
The charts/sagemath-mcp Helm chart deploys the MCP server to Kubernetes. To install
directly from this repository:
helm install sagemath charts/sagemath-mcp \
--set image.repository=ghcr.io/xbp-europe/sagemath-mcp \
--set image.tag=latest
Key values:
service.port/service.targetPort: external and container ports (default 8314).env: map of environment overrides forSageSettings(e.g.,SAGEMATH_MCP_EVAL_TIMEOUT).args: optional CLI arguments appended after the entrypoint (e.g.,--transport http).ingress.*: enable and configure HTTP ingress resources.
Review values.yaml for the full list of configurable knobs, including pull secrets, resource limits,
and volume mounts. The chart enforces non-root execution (runAsUser/runAsGroup 1000, dropped
capabilities) so the packaged image must support the sage user. The default image points to
ghcr.io/xbp-europe/sagemath-mcp, published automatically by the release workflow.
To connect from Claude Desktop, add the following configuration snippet to claude_desktop_config.json:
{
"mcpServers": {
"sagemath": {
"command": "uv",
"args": ["run", "sagemath-mcp"]
}
}
}
Usage
The server exposes several tools and resources:
evaluate_sage: Execute SageMath code with persistent state. Returns the result, captured stdout, and optional LaTeX. While code runs the server emits progress heartbeats, and request cancellations restart the worker safely.calculate_expression: Evaluate a Sage expression and receive string/numeric forms.solve_equation,differentiate_expression,integrate_expression: Convenience wrappers for algebra and calculus workflows.statistics_summary: Produce population/sample mean, variance, and standard deviation for numeric datasets.matrix_multiply: Multiply matrices and return the product as nested lists.cancel_sage_session/reset_sage_session: Manage the current session state.resource://sagemath/session/{scope}: Inspect active sessions and their idle timers.resource://sagemath/monitoring/{scope}: Retrieve evaluation metrics (success/failure counts, latency stats, security violations) withscope = "metrics"or"all".resource://sagemath/docs/{scope}: Retrieve documentation links such as the SageMath reference manual (scope = "all"for every link).
Lightweight Markdown exports covering the landing page, search index, plotting (2D/3D), calculus,
rings, and statistics live under docs/reference_md/ for quick client-side lookups. See
for JSON prompt examples and MCP usage patterns.
See for guidance on scraping the metrics resource and wiring it into dashboards. See for platform-specific setup notes.
Using with Codex CLI
Codex CLI can connect over stdio:
codex mcp add sagemath \
--command uv \
--args "run" "sagemath-mcp"
For streaming progress, expose the HTTP endpoint first:
sage -python -m uv run sagemath-mcp -- --transport streamable-http --host 0.0.0.0 --port 8314
codex mcp add sagemath-http --url http://127.0.0.1:8314/mcp
Using with gemini_cli
gemini_cli mcp add sagemath \
--transport stdio \
--command uv \
--arg run \
--arg sagemath-mcp
Or point to the HTTP transport:
gemini_cli mcp add sagemath-http --transport http --url http://127.0.0.1:8314/mcp
Using with qwen_cli
qwen_cli mcp add sagemath \
--command uv \
--args "run" "sagemath-mcp"
If you prefer HTTP:
qwen_cli mcp add sagemath-http \
--url http://127.0.0.1:8314/mcp \
--transport http
In all cases, restart the CLI session after adding the MCP server so the new tools and resources are discovered.
Example interaction
> evaluate_sage({"code": "a = var('a'); f = (a + 1)^5"})
→ {"result": null, "stdout": "", "result_type": "statement"}
> evaluate_sage({"code": "expand(f)"})
→ {"result": "a^5 + 5*a^4 + 10*a^3 + 10*a^2 + 5*a + 1", "latex": "a^{5} + 5 a^{4} + 10 a^{3} + 10 a^{2} + 5 a + 1"}
> cancel_sage_session({})
→ {"message": "Session cancelled and restarted"}
> reset_sage_session({})
→ {"message": "Session cleared"}
Configuration
Environment variables influence runtime behavior:
| Variable | Description | Default |
|---|---|---|
SAGEMATH_MCP_SAGE_BINARY | Path to the sage executable. | sage |
SAGEMATH_MCP_STARTUP | Sage code executed during session bootstrap. | from sage.all import * |
SAGEMATH_MCP_IDLE_TTL | Seconds of inactivity before a session is culled. | 900 |
SAGEMATH_MCP_EVAL_TIMEOUT | Per-evaluation timeout in seconds. | 30 |
SAGEMATH_MCP_MAX_STDOUT | Maximum characters of stdout returned per call. | 100000 |
SAGEMATH_MCP_SHUTDOWN_GRACE | Grace period before a stuck worker is terminated. | 2 |
SAGEMATH_MCP_FORCE_PYTHON_WORKER | Use the pure-Python worker (helpful for tests/CI). | false |
SAGEMATH_MCP_PURE_PYTHON | When set to 1, load math stdlib instead of Sage modules. | unset |
SAGEMATH_MCP_SECURITY_ENABLED | Enable/disable AST-based code validation. | true |
SAGEMATH_MCP_SECURITY_MAX_SOURCE | Maximum source length in characters. | 8000 |
SAGEMATH_MCP_SECURITY_MAX_AST_NODES | Maximum AST node count allowed. | 2500 |
SAGEMATH_MCP_SECURITY_MAX_AST_DEPTH | Maximum AST depth allowed. | 75 |
SAGEMATH_MCP_SECURITY_ALLOW_IMPORTS | Permit import statements when set to true. | false |
SAGEMATH_MCP_SECURITY_FORBID_GLOBAL | Block global statements when true. | true |
SAGEMATH_MCP_SECURITY_FORBID_NONLOCAL | Block nonlocal statements when true. | true |
SAGEMATH_MCP_SECURITY_LOG_VIOLATIONS | Emit warnings when code is blocked. | true |
SAGEMATH_MCP_SECURITY_ALLOWED_IMPORTS | Comma-separated allowlist of exact module names permitted in imports. | math,cmath,sage,sage.all |
SAGEMATH_MCP_SECURITY_ALLOWED_IMPORT_PREFIXES | Comma-separated prefixes treated as safe import namespaces. | sage. |
Development
uv pip install -e .[dev]
make lint
make test
make integration-test # requires running sage-mcp Docker container
make build
uv run python scripts/build_release.py # build documented sdist/wheel into dist/
# Publish a release
git tag vX.Y.Z && git push origin vX.Y.Z
(GitHub Actions will build wheels/sdist and publish to PyPI.)
GitHub Actions runs
make lint,make test,make integration-test, andmake buildon every push/pull request. Runmake integration-testlocally once thesage-mcpcontainer is available.
Without a local SageMath installation you can still run unit tests—the test suite replaces the Sage worker with a lightweight Python interpreter to validate session plumbing.
Project Layout
sagemath-mcp/
├── pyproject.toml
├── README.md
├── src/
│ └── sagemath_mcp/
│ ├── __init__.py
│ ├── config.py
│ ├── models.py
│ ├── server.py
│ ├── session.py
│ ├── _sage_worker.py
│ └── py.typed
└── tests/
├── __init__.py
└── test_session.py
Roadmap
- Disk-backed session persistence for long-running workloads.
- Streaming partial outputs for long calculations.
- Fine-grained resource templates exposing saved worksheets.
Contributing
We welcome issues and pull requests! Review the and before opening a PR. For vulnerability disclosures, follow the steps in . Ownership defaults are defined in .
License
MIT