mcp-sage
v0.2.0MCP server providing persistent SageMath sessions (Sage REPL over the Model Context Protocol)
An MCP server providing persistent SageMath sessions to any MCP-compatible host: Claude Code, Claude Desktop, Cursor, or an agent harness such as mcp-minion. Without this server, each SageMath computation runs in isolation: loading a list of graphs, analyzing one, then querying its automorphism group would require reloading the graph file three times. The Sage REPL server maintains state across calls, enabling iterative mathematical exploration. Installed from PyPI (the package is mcp-sage; the unrelated sage-mcp on PyPI is a different project):
Summary extracted from the README.
mcp-sage
An MCP server providing persistent SageMath sessions to any MCP-compatible host: Claude Code, Claude Desktop, Cursor, or an agent harness such as mcp-minion.
Without this server, each SageMath computation runs in isolation: loading a list of graphs, analyzing one, then querying its automorphism group would require reloading the graph file three times. The Sage REPL server maintains state across calls, enabling iterative mathematical exploration.
Quickstart
Installed from PyPI (the package is
mcp-sage; the unrelated sage-mcp on PyPI is a different project):
uvx mcp-sage
Claude Code / Claude Desktop config (.mcp.json or ~/.claude.json):
{
"mcpServers": {
"sage-repl": {
"command": "uvx",
"args": ["mcp-sage"]
}
}
}
Alternatively, the server is a single file using UV's inline dependency syntax (PEP 723), so a checkout runs without installation:
uv run sage_repl_server.py
An optional --directory /path/to/workdir argument sets the server's working
directory, so that relative paths in Sage code (e.g. a file written by the
agent) resolve inside a chosen run folder.
Tools
| Tool | Purpose |
|---|---|
sage_start |
Create a new named session (auto-created on first sage_exec if omitted) |
sage_exec |
Execute Sage code and return results |
sage_interrupt |
Send Ctrl+C to stop long-running computations |
sage_stop |
Terminate a session and release resources |
sage_list |
Show all active sessions |
sage_exec runs the code through the Sage preparser, as the sage shell does:
^ is exponentiation and ^^ is xor, integer and rational literals are exact
Sage numbers (1/2 is a Rational), float literals are Sage reals with at
least 53 bits (more for literals with more digits), 1r is a Python int, and
R.<x> = QQ[], f(x) = x^2 and [1..5] work. Code inside an exec string is
not preparsed. Sage numbers are not Python numbers for every library:
json.dumps(1) and time.sleep(0.5) raise TypeError (write 0.5r or
float(0.5), or use preparse: false). IPython magics such as
%time and shell escapes such as !echo hi work. With preparse: false the code
runs as plain Python (^ is xor, 1/2 and 2.5 are floats). As in the
sage shell, x is predefined as a symbolic variable until the code assigns
it. Variables persist across calls.
sage_exec returns one text: the value of the last expression, followed by
sections headed [stdout], [stderr] and [error] where these are not empty,
or (no output). Calls on one session run one at a time. The timeout (default
60 seconds) covers the whole call: at the timeout the call returns what was
printed so far with an error, but the computation keeps running in the kernel.
A later call waits for it up to its own timeout; if the kernel is still busy,
that call returns an error saying that its code was not run. sage_interrupt
stops the computation and returns once the kernel accepts new code again. If
that does not happen within 5 seconds (code that ignores Ctrl+C), it returns a
message that the interrupt was sent but the kernel may still be busy, and
suggests sage_interrupt again or sage_stop.
Architecture
The server drives SageMath through the Jupyter kernel protocol rather than raw
subprocess management: reliable prompt detection, clean separation of stdout /
stderr / return values, native interrupt support, and robust multi-line input.
It injects a KernelSpec directly into the KernelManager, so SageMath's
bundled Python runs with ipykernel without touching the user's Jupyter
configuration. On session start the server imports sage.all, making all Sage
functions immediately available, defines the symbolic variable x, and
registers Sage's preparser as an IPython input transformer. See SPEC.md for the full tool specification.
The tools carry MCP annotations, so clients can tell them apart: sage_list
is marked read-only, and sage_exec is marked destructive and open-world,
since it runs arbitrary code.
Kernels end with the server. Each kernel has a small watcher process. When
the server is gone for any reason, the watcher kills the kernel and the
programs it started, such as GAP and Maxima, and removes the kernel's
connection file. The server exits at once when its stdin closes and on
SIGTERM, SIGINT or SIGHUP, also while a computation runs. A server whose
uv/uvx wrapper was killed ends when the client closes the connection. This cleanup
was tested with Sage on macOS (Sage 10.9 and 10.10). On Linux only the watcher
was tested, without Sage, on a stand-in process tree (Debian 13, dash,
procps-ng 4.0.4); it needs pgrep and pkill.
Restricted file system
Sage writes caches and GAP workspaces to DOT_SAGE (default ~/.sage), and
the kernel connection file and Sage's temporary files go to TMPDIR. With a
read-only home directory, sage_start fails with PermissionError on
~/.sage/ unless DOT_SAGE points to a writable directory. With DOT_SAGE
set, the session works, and IPython keeps its files in
DOT_SAGE/ipython-5.0.0 (set IPYTHONDIR to move them). JUPYTER_RUNTIME_DIR
is not used. Started through uvx, uv also needs a writable UV_TOOL_DIR;
otherwise it stops with failed to create directory .../uv/tools: Permission denied. These facts were checked with Homebrew Sage 10.9, with uv's cache
writable.
Requirements
- SageMath 10.6+ in
PATH. For a conda-forge or pip Sage, whosesagehas no-pythonoption, the server runs the Python named in the first line of thesagescript and puts itsbindirectory first in the kernel'sPATH. Otherwise it launchessage -python. SetMCP_SAGE_PYTHONto Sage's Python interpreter to override both; that interpreter needsipykernel. - uv
Dependencies (resolved automatically by uv run): mcp (1.x low-level server
API; pinned <2), jupyter_client, ipykernel.
Citation
This server was built for and used in the experiments of:
David Seka and Stefan Szeider. Neurosymbolic Discovery of Algebraic Graph Constructions. NeSy 2026 (20th International Conference on Neurosymbolic Learning and Reasoning), PMLR vol. 284. Supplementary archive: https://doi.org/10.5281/zenodo.21850460
License
MIT