mathematica-mcp is a Rust MCP server that exposes a local Wolfram Language / Mathematica kernel over stdio. It is intended for MCP-compatible clients that need symbolic computation, numerical evaluation, finance lookups, or notebook-style experimentation backed by a real local kernel instead of a remote API.
The repository also includes a local REPL that exercises the same session-management and evaluation path without requiring an MCP host, which makes it useful for development, debugging, and validating kernel setup.
The server starts one or more local Wolfram kernel processes and presents them through a small MCP tool surface:
- Create a kernel-backed session.
- Execute arbitrary Wolfram Language code inside a specific session.
- Close a session and release its resources.
- List active sessions and their idle times.
- Return current local and UTC time.
- Provide a convenience helper around
FinancialData[...].
Each session is isolated in its own worker thread and is cleaned up automatically after 30 minutes of inactivity.
Most MCP clients speak stdio well, but they do not know how to launch and manage a local Mathematica kernel. This project fills that gap:
rmcphandles the MCP server and stdio transport.wstphandles the Wolfram Symbolic Transfer Protocol bridge into the kernel.- The crate adds session lifecycle management, kernel discovery, evaluation wrappers, and a local debugging workflow.
The binary supports two modes.
Runs the MCP server over stdio for use by MCP hosts such as desktop assistants, editor integrations, or custom clients.
cargo run -- serveThis is also the default when no subcommand is provided:
cargo runWhen launched directly from an interactive terminal, the binary now defaults to repl instead of serve, because serve expects an MCP client to speak JSON-RPC over stdio. MCP hosts still get server mode automatically because they launch the process with piped stdio rather than a terminal.
Runs an interactive local shell that calls the same session manager and evaluation path without MCP in the middle.
cargo run -- replThe REPL is useful when:
- you want to verify that the Wolfram kernel launches correctly;
- you want to debug evaluation behavior before testing through an MCP client;
- you want to inspect raw output, logs, and graphics handling locally.
The server currently exposes these tools from src/mcp.rs:
mathematica_create_sessionLaunch a new kernel session and return a session id.mathematica_execute_codeEvaluate Wolfram Language code in a specific session.mathematica_close_sessionShut down a session.mathematica_list_sessionsReturn active sessions, creation time, and idle time.mathematica_timeReturn local and UTC time in RFC3339 format.mathematica_get_financeBuild and execute aFinancialData[...]expression for a given ticker and optional property/date range/interval.
Recommended usage flow:
- Call
mathematica_create_session. - Reuse the returned
session_idfor one or moremathematica_execute_codeormathematica_get_financecalls. - Call
mathematica_close_sessionwhen you are done.
The evaluation pipeline lives primarily in src/wolfram.rs and src/session.rs.
At a high level:
- The server resolves a Wolfram kernel executable.
- A new session spawns a dedicated kernel process through WSTP.
- Each session owns a worker thread and receives requests over a channel.
- User code is wrapped before evaluation so the server can return structured JSON.
- The wrapper captures:
output: the result rendered withToString[..., InputForm]graphics: a PNG, Base64-encoded, when the result looks like a graphics objectlogs: text packets such asPrint[...]output
- The MCP layer measures elapsed time and returns the structured result.
This design keeps the transport layer thin and concentrates kernel behavior in a small number of files.
Sessions are managed by SessionManager in src/session.rs.
Important behavior:
- Each session gets its own kernel process.
- Each session tracks
created_atandlast_accessed. - Idle sessions are closed automatically after 30 minutes.
- Eval requests are timeout-bound per call.
- Closing a session joins the worker thread and removes it from the internal map.
Session ids are human-readable four-part tokens such as quick_fox-kind_sloth-bright_auk-calm_mole. The generator and verifier live in src/session_id.rs.
Kernel resolution is implemented in src/wolfram.rs with platform-specific helpers in src/platform/.
Resolution order:
- platform-specific path from
mathematica-mcp.toml WOLFRAM_KERNEL_PATHas a legacy override- platform discovery via
wolfram-app-discovery - executable lookup on
PATH - fallback to a bare
WolframKernelcommand name
Runtime configuration now lives in mathematica-mcp.toml:
[kernel]
linux_path = "/usr/local/Wolfram/WolframEngine/15.0/Executables/WolframKernel"
windows_path = "C:\\Program Files\\Wolfram Research\\Wolfram\\14.3\\WolframKernel.exe"At runtime, the binary automatically selects linux_path or windows_path based on the current platform, expands the path, and validates that it points to a usable kernel executable.
If you want to keep the config somewhere else, set MATHEMATICA_MCP_CONFIG to the TOML file path before launching the server.
WOLFRAM_KERNEL_PATH is still supported as a fallback for compatibility, but the preferred place for explicit kernel configuration is the TOML file.
Tracing is initialized in src/main.rs.
Important detail: in server mode, logs are written to stderr, not stdout, because stdio MCP transport uses stdout for protocol traffic.
The logger uses tracing-subscriber with RUST_LOG support:
RUST_LOG=debug cargo run -- serveThe REPL implementation lives in src/repl.rs.
Supported commands:
mathematica_create_sessionmathematica_list_sessionsmathematica_timemathematica_execute_code <code...>mathematica_get_finance <SYMBOL> [PROPERTY] [START YYYY-MM-DD] [END YYYY-MM-DD] [INTERVAL]mathematica_close_session [SESSION_ID]exitquit
REPL history is stored at .cache/mathematica_repl_history.txt.
- Rust toolchain with Cargo
- A local Wolfram / Mathematica installation with a usable kernel
- Native toolchain support needed by the
wstpdependency on your platform
cargo build
cargo run -- --help
cargo run -- serve
cargo run -- repl
cargo testOn Windows PowerShell, the REPL and server can be run directly against the configured kernel path from mathematica-mcp.toml:
cargo run -- replTop-level structure:
Cargo.tomlMain crate manifest and dependency list.src/Application source code for the MCP server, REPL, session manager, and Wolfram integration.docs/Reference notes, migration material, release process notes, and internal project documentation.wstp-sys-patched/Local patched replacement for thewstp-syscrate used through[patch.crates-io].tmp/Scratch/reference area ignored by git for temporary migration inputs or notes.target/Cargo build output.
src/main.rsCLI entrypoint, tracing setup, and subcommand dispatch.src/mcp.rsMCP server implementation and tool definitions.src/repl.rsInteractive local shell for manual testing.src/session.rsSession lifecycle, worker threads, idle cleanup, and eval dispatch.src/session_id.rsHuman-readable session id generation and format validation.src/wolfram.rsKernel discovery, WSTP launch, evaluation wrapper, and finance helper code generation.src/platform/mod.rsPlatform abstraction for kernel path discovery and validation.src/platform/linux.rsLinux-specific kernel path handling.src/platform/windows.rsWindows-specific kernel path handling.
The docs/reference/ tree is supporting project documentation, not runtime code. It currently contains:
- release and semver notes;
- migration structure and roadmap documents;
- tool references used during development;
- AI/reference notes used while shaping the project.
If you are trying to understand runtime behavior, start with src/ first. If you are trying to understand maintenance workflow, planned structure, or release process, then docs/reference/ is relevant.
This repository patches wstp-sys locally:
Cargo.tomlredirects the crate with[patch.crates-io].- the directory provides the low-level WSTP FFI crate used by the upstream
wstpcrate; generated/contains pre-generated bindings used during builds.
This exists because WSTP integration is the most platform-sensitive part of the stack, and local patching gives the project control over that boundary.
Key runtime dependencies from Cargo.toml:
rmcpMCP server framework and stdio transport.tokioAsync runtime for the server and cleanup tasks.wstpRust bindings over Wolfram's WSTP.wolfram-exprExpression handling for WSTP interactions.wolfram-app-discoveryKernel path discovery across supported installations.rustylineInteractive REPL support.tracingandtracing-subscriberStructured logging.clapCLI parsing.flumeCross-thread request channel for session workers.
- This project depends on a locally installed proprietary Wolfram runtime.
- The MCP surface is intentionally small; most Wolfram functionality currently flows through generic code execution rather than many specialized tools.
- Graphics results are detected with a simple wrapper and returned as Base64 PNG, which is practical but not a full notebook rendering model.
- Session ids are human-readable and format-validated, but they are not durable credentials and should be treated as local process identifiers.
Useful files for maintainers:
docs/reference/RELEASE.mdRelease and semver policy.docs/reference/migration/structure.mdNotes for migration-oriented project organization.
The repository is already a functional local MCP bridge:
- MCP server mode works over stdio.
- REPL mode exercises the same kernel/session path locally.
- Session creation, evaluation, listing, finance lookup, and shutdown are implemented.
- Linux and Windows kernel discovery paths are present.
The main area to be careful with is environment setup around WSTP and local kernel discovery, since that is the part most likely to vary by machine.