MCP SageMath Server
Enables execution of SageMath mathematical computations through a local SageMath installation. Provides tools to check SageMath version and evaluate SageMath scripts with configurable timeouts and error handling.
README
MCP SageMath Server
基于 Model Context Protocol (MCP) 的本地 SageMath 服务端,当前提供两项工具:
sagemath.version:查询本地 SageMath 版本。sagemath.evaluate:执行 SageMath 脚本并返回标准输出/错误。
项目处于早期预览阶段(v0.0.1)。
功能概览
- 双传输模式:默认 STDIO,可通过环境变量切换到 HTTP(同时支持
GET /mcp与POST /mcp)。 - 无状态 HTTP 会话:避免重复初始化导致的错误。
- 可靠的子进程封装:当 SageMath 不可用时返回结构化错误,不会崩溃。
- 可配置的 SageMath 路径:支持源代码配置与环境变量覆盖,默认回退到系统 PATH。
环境要求
- Node.js 18 及以上版本(推荐 20+)。
- 本地已安装 SageMath,并能够通过命令行访问其可执行文件。
配置 SageMath 路径
项目在运行时按照以下优先级查找 SageMath 可执行文件:
src/config.ts中的config.sagePath(若设置为非空字符串)。- 环境变量
SAGE_PATH。 - 系统 PATH 中的
sage命令。
默认情况下,config.sagePath 会读取 SAGE_PATH 环境变量的值;如需固定路径,可在该文件内显式填写,例如:
export const config = {
sagePath: "/opt/sage/bin/sage",
};
安装
- 本项目根目录下执行
npm install
运行方式
STDIO(默认模式)
- 构建:
npm run build - 运行:
node dist/index.js - 测试示例客户端:
npx -y tsx src/test/stdio-client.ts
HTTP 模式(需手动启用)
- 启动开发服务器:
MCP_TRANSPORT=http npm run dev - 默认监听
http://localhost:3000/mcp,可通过PORT环境变量调整端口。 - 开箱测试:
MCP_TRANSPORT=http npx -y tsx src/test/client.ts - 端点说明:
GET /mcp:用于 SSE/流式 JSON-RPC。POST /mcp:标准 JSON-RPC over HTTP。
MCP 客户端配置示例
STDIO
{
"mcpServers": {
"sagemath-server": {
"command": "node",
"args": ["/absolute/path/to/mcp-server-sagemath/dist/index.js"],
"autoApprove": ["sagemath.version", "sagemath.evaluate"],
"env": {
// "SAGE_PATH": "/absolute/path/to/sage" // 可选
}
}
}
}
HTTP
{
"mcpServers": {
"sagemath-server-http": {
"command": "node",
"args": ["/absolute/path/to/mcp-server-sagemath/dist/index.js"],
"env": {
"MCP_TRANSPORT": "http",
"PORT": "3000"
}
}
}
}
提供的工具
sagemath.version
- 输出字段:
stdout,stderr,exitCode,durationMs,timedOut。 - 适用于检测 SageMath 是否安装及版本信息。
sagemath.evaluate
- 输入:
code(string) — 必填,SageMath 脚本。timeoutMs(number) — 可选,超时时间,默认 10000 ms。
- 输出同上。
- 执行流程:将代码写入临时文件后调用 SageMath 执行。
测试
- STDIO 回归测试:
npx -y tsx src/test/stdio-client.ts - HTTP 回归测试:
MCP_TRANSPORT=http npx -y tsx src/test/client.ts
安全提示
sagemath.evaluate可运行任意 SageMath 代码,请仅在可信环境使用。- 建议在需要时结合容器或沙箱进一步隔离,并设置资源配额。
Roadmap
- 扩展更多 SageMath 功能(绘图、符号计算等)。
- 优化长时间任务的会话复用与资源管理。
- 增强错误分类与限流策略。
许可证
MIT License,详见 LICENSE。
鸣谢
- Model Context Protocol 社区及 SDK。
- SageMath 开源数学系统。
Recommended Servers
playwright-mcp
A Model Context Protocol server that enables LLMs to interact with web pages through structured accessibility snapshots without requiring vision models or screenshots.
Magic Component Platform (MCP)
An AI-powered tool that generates modern UI components from natural language descriptions, integrating with popular IDEs to streamline UI development workflow.
Audiense Insights MCP Server
Enables interaction with Audiense Insights accounts via the Model Context Protocol, facilitating the extraction and analysis of marketing insights and audience data including demographics, behavior, and influencer engagement.
VeyraX MCP
Single MCP tool to connect all your favorite tools: Gmail, Calendar and 40 more.
graphlit-mcp-server
The Model Context Protocol (MCP) Server enables integration between MCP clients and the Graphlit service. Ingest anything from Slack to Gmail to podcast feeds, in addition to web crawling, into a Graphlit project - and then retrieve relevant contents from the MCP client.
Kagi MCP Server
An MCP server that integrates Kagi search capabilities with Claude AI, enabling Claude to perform real-time web searches when answering questions that require up-to-date information.
E2B
Using MCP to run code via e2b.
Neon Database
MCP server for interacting with Neon Management API and databases
Exa Search
A Model Context Protocol (MCP) server lets AI assistants like Claude use the Exa AI Search API for web searches. This setup allows AI models to get real-time web information in a safe and controlled way.
Qdrant Server
This repository is an example of how to create a MCP server for Qdrant, a vector search engine.