SymPy Sandbox MCP

SymPy Sandbox MCP

A secure mathematical computation sandbox that enables LLMs to perform symbolic math operations like algebra, calculus, and equation solving via SymPy. It features low-latency execution through pre-warmed process pools and provides standardized JSON outputs for reliable agent integration.

Category
访问服务器

README

SymPy Sandbox MCP

English | 中文版

A production-focused MCP service that lets agents/LLMs run SymPy safely and efficiently. It combines AST policy checks, runtime resource limits, and prewarmed workers to deliver low-noise, parse-friendly results.

Features

  • Single tool: sympy (input only requires code)
  • Prewarmed worker pool to avoid repeated import sympy
  • Two-layer safety: AST guard + runtime resource limits
  • Compact structured JSON output for low token overhead
  • Standardized error codes for reliable auto-retry workflows

Typical Use Cases

  • Symbolic algebra, differentiation, integration, equation solving
  • MCP tool integration for Codex / Cursor / Claude Desktop / custom MCP clients
  • Agent workflows that need controllable failures and clean error signals

Recommended Integration (MCP client via stdio)

Call example:

fastmcp call \
  --command 'python -m sym_mcp.server' \
  --target sympy \
  --input-json '{"code":"import sympy as sp\\nx=sp.Symbol(\"x\")\\nprint(sp.factor(x**2-1))"}'

Client config (python -m, recommended):

{
  "mcpServers": {
    "sympy-sandbox": {
      "command": "python",
      "args": ["-m", "sym_mcp.server"]
    }
  }
}

Client config (installed as sym-mcp):

{
  "mcpServers": {
    "sympy-sandbox": {
      "command": "sym-mcp",
      "args": []
    }
  }
}

Client config (uvx):

{
  "mcpServers": {
    "sympy-sandbox": {
      "command": "uvx",
      "args": ["sym-mcp"]
    }
  }
}

Quick Start

1) Requirements

  • Python 3.11+
  • Linux / macOS (Linux recommended for production)

2) Install (Tsinghua mirror first)

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple -e .
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple -e ".[dev]"

3) Run server (stdio)

python -m sym_mcp.server

4) Verify tool

fastmcp list --command 'python -m sym_mcp.server'

Tool Contract

Tool name

  • sympy

Input

  • code: str

Notes:

  • You must print() final outputs.
  • If nothing is printed, out may be empty.

Output (always compact JSON string)

Success:

{"out":"x**2/2"}

Failure:

{"code":"E_RUNTIME","line":3,"err":"ZeroDivisionError: division by zero","hint":"Runtime error. Check variable types, division-by-zero, or undefined names near the reported line."}

Field definitions:

  • out: stdout text on success
  • code: error code
  • line: user code error line, or null
  • err: compact error message (traceback noise removed)
  • hint: fix hint (based on configured hint level)
  • If out / err / hint is too long, it will be truncated with ...[truncated]

Error Codes

  • E_AST_BLOCK: blocked by AST safety policy
  • E_SYNTAX: syntax error
  • E_TIMEOUT: timeout
  • E_MEMORY: memory limit triggered
  • E_RUNTIME: general runtime error
  • E_WORKER: worker communication/state failure
  • E_INTERNAL: internal server error

Recommended Agent Prompt Rules

  1. Use math-only Python code.
  2. Only import sympy or math.
  3. Always print() final answers.
  4. For multiple outputs, use multiple print() lines.
  5. On failure, patch minimally near line and retry.
  6. For E_TIMEOUT, reduce scale first; for E_MEMORY, reduce object size/dimension; for E_AST_BLOCK, remove unsafe statements.

Example:

import sympy as sp
x = sp.Symbol("x")
expr = (x + 1)**5
print(sp.expand(expr))

Security Model

Before execution (AST policy)

  • Only sympy / math imports are allowed
  • Dangerous capabilities are blocked (eval, exec, open, __import__, etc.)
  • Dunder attribute traversal is blocked (e.g. __class__)

During execution (OS resource limits)

  • Per-task CPU time limit + timeout kill
  • Per-worker memory limit via setrlimit
  • Worker auto-rebuild on failure to keep server healthy

Architecture

  • src/sym_mcp/server.py: MCP entrypoint and tool registration
  • src/sym_mcp/security/ast_guard.py: AST validation
  • src/sym_mcp/executor/worker_main.py: worker loop
  • src/sym_mcp/executor/pool.py: async prewarmed process pool
  • src/sym_mcp/executor/sandbox.py: restricted execution and stdout capture
  • src/sym_mcp/errors/parser.py: error normalization and code mapping
  • src/sym_mcp/config.py: runtime configuration

Configuration (Environment Variables)

  • SYMMCP_POOL_SIZE: worker pool size, default 10
  • SYMMCP_EXEC_TIMEOUT_SEC: per execution timeout (sec), default 3
  • SYMMCP_MEMORY_LIMIT_MB: memory cap per worker (MB), default 150
  • SYMMCP_QUEUE_WAIT_SEC: queue wait timeout (sec), default 2
  • SYMMCP_LOG_LEVEL: log level, default INFO
  • SYMMCP_MAX_OUTPUT_CHARS: output truncation threshold, default 1200
  • SYMMCP_HINT_LEVEL: hint level (none/short/medium), default medium

FAQ

Why is out empty?

Most likely the code does not print() the final result.

Why return compact JSON string?

It is easier for agents to parse reliably and reduces token cost.

Is memory limiting always stable on macOS?

setrlimit behavior differs by OS. Linux is preferred for production.

Does it support HTTP/SSE?

Current primary delivery is stdio. HTTP/SSE can be added later via FastMCP transport extensions.

Known Limits

  • This is restricted Python execution, not VM/container-grade isolation
  • Memory limit behavior is OS-dependent
  • Output is truncated at threshold, with ...[truncated] suffix

Development

Run tests

PYTHONPATH=src pytest -q

Benchmark

PYTHONPATH=src python scripts/benchmark.py --concurrency 100 --total 500

Contributing

  • Run PYTHONPATH=src pytest -q before submitting PRs
  • When adding new capabilities, update:
    • error code docs
    • README examples
    • related unit/integration tests
  • Publishing process: PUBLISHING.md

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选