Skip to main content
Glama
calculator-mcp-server

calculator-mcp-server

by cyanheads

Version License Docker MCP SDK npm TypeScript Bun

Install in Claude Desktop Install in Cursor Install in VS Code

Framework

公共托管服务器: https://calculator.caseyjhand.com/mcp


工具

一个工具即可完成所有数学运算:

工具名称

描述

calculate

对数学表达式求值、化简代数表达式,或计算符号导数。

calculate

一个工具即可覆盖服务器的全部用途。operation 参数默认为 evaluate,因此常见用法只需 { expression: "..." }。

  • 求值(Evaluate) — 算术、三角函数、对数、统计、矩阵、复数、单位换算、组合数学

  • 化简(Simplify) — 以符号方式化简代数表达式(例如 2x + 3x -> 5 * x)。支持代数和三角恒等式

  • 求导(Derivative) — 计算符号导数(例如 3x^2 + 2x + 1 -> 6 * x + 2)

  • 通过 scope 参数设置变量作用域:{ "x": 5, "y": 3 }

  • 数值结果精度可配置

  • 基于表单的 MCP 客户端传入的空白可选 variable 和 precision 值将被视为未提供


Related MCP server: pycodemath

资源

URI 模式

描述

calculator://help

可用函数、运算符、常量和语法参考。


特性

基于 @cyanheads/mcp-ts-core 构建:

  • 声明式工具定义 — 每个工具一个文件,框架负责注册和验证

  • 所有工具统一错误处理

  • 结构化日志,支持可选的 OpenTelemetry 追踪

  • 本地运行(stdio/HTTP)或 Docker 运行

计算器专属特性:

  • 加固的 math.js v15 实例 — 禁用危险函数,通过 vm.runInNewContext() 在沙箱中求值并设置超时

  • 无需认证 — 所有操作均为只读且无状态

  • 输入验证:表达式长度限制、拒绝表达式分隔符(分号和换行)、仅允许数值型作用域值

  • 结果验证:阻止危险结果类型(函数、解析器、结果集),可配置最大结果大小

  • 作用域净化:仅允许数值型值,防止原型污染(阻止 __proto__、constructor 等)


快速开始

公共托管实例

公共实例位于 https://calculator.caseyjhand.com/mcp — 无需安装。通过 Streamable HTTP 将任意 MCP 客户端指向该地址:

{
  "mcpServers": {
    "calculator-mcp-server": {
      "type": "streamable-http",
      "url": "https://calculator.caseyjhand.com/mcp"
    }
  }
}

自托管 / 本地

将以下配置之一添加到您的 MCP 客户端配置文件中:

{
  "mcpServers": {
    "calculator-mcp-server": {
      "type": "stdio",
      "command": "bunx",
      "args": ["@cyanheads/calculator-mcp-server@latest"]
    }
  }
}

或使用 npx(无需 Bun):

{
  "mcpServers": {
    "calculator-mcp-server": {
      "type": "stdio",
      "command": "npx",
      "args": ["-y", "@cyanheads/calculator-mcp-server@latest"]
    }
  }
}

或使用 Docker:

{
  "mcpServers": {
    "calculator-mcp-server": {
      "type": "stdio",
      "command": "docker",
      "args": [
        "run", "-i", "--rm",
        "-e", "MCP_TRANSPORT_TYPE=stdio",
        "ghcr.io/cyanheads/calculator-mcp-server:latest"
      ]
    }
  }
}

前置要求

安装

  1. 克隆仓库:

git clone https://github.com/cyanheads/calculator-mcp-server.git
  1. 进入目录:

cd calculator-mcp-server
  1. 安装依赖:

bun install

配置

变量

描述

默认值

CALC_MAX_EXPRESSION_LENGTH

允许的最大表达式字符串长度(10–10,000)。

1000

CALC_EVALUATION_TIMEOUT_MS

最大求值时间(毫秒)(100–30,000)。

5000

CALC_MAX_RESULT_LENGTH

最大结果字符串长度(字符数)(1,000–1,000,000)。

100000

MCP_TRANSPORT_TYPE

传输方式:stdio 或 http。

stdio

MCP_HTTP_HOST

HTTP 服务器的主机名。

localhost

MCP_HTTP_PORT

HTTP 服务器的端口。

3010

MCP_HTTP_ENDPOINT_PATH

HTTP MCP 端点的路径。

/mcp

MCP_HTTP_MAX_BODY_BYTES

最大入站 HTTP 请求大小;0 表示不限制。

1048576

MCP_AUTH_MODE

认证模式:none、jwt 或 oauth。

none

MCP_LOG_LEVEL

日志级别(RFC 5424)。

info

有关可选的会话、可恢复性、日志和遥测设置,请参阅 .env.example。


运行服务器

本地开发

  • 构建并运行生产版本:

    bun run build
    bun run start:http   # or start:stdio
  • 运行检查和测试:

    bun run devcheck     # Lints, formats, type-checks
    bun run test         # Runs test suite

Docker

docker build -t calculator-mcp-server .
docker run -p 3010:3010 calculator-mcp-server

该镜像默认以 Streamable HTTP 模式运行在端口 3010 上,使用无状态会话,日志输出到 /var/log/calculator-mcp-server。


项目结构

目录

用途

src/mcp-server/tools/

工具定义(*.tool.ts)。

src/mcp-server/resources/

资源定义(*.resource.ts)。

src/services/

领域服务集成(MathService)。

src/config/

使用 Zod 进行环境变量解析和验证。

docs/

生成的目录树。


开发指南

有关开发指南和架构规则,请参阅 AGENTS.md 或 CLAUDE.md。简要说明:

  • 处理器抛出异常,框架负责捕获 — 工具逻辑中不使用 try/catch

  • 使用 ctx.log 记录日志

  • 在 src/index.ts 中注册新工具和资源


贡献

欢迎提交 Issue 和 Pull Request。提交前请先运行检查:

bun run devcheck
bun run test

许可证

Apache-2.0 — 详情请参阅 LICENSE。

Tool Schema Changelog

Recent tool additions, removals, and schema changes observed during successful MCP inspections.

Loading tool schema changelog…

    Related MCP Connectors

    Related MCP Servers

    • A
      license
      B
      quality
      D
      maintenance
      High-precision mathematics server for MCP clients, providing exact integer arithmetic, symbolic derivatives, and numerical calculus via LaTeX-style input.
      6
      5 npm
      2
      MIT
    • A
      license
      Not graded
      quality
      B
      maintenance
      Provides a token-efficient exact math engine for AI agents, enabling computation of derivatives, integrals, equations, and optimized Python/NumPy code via a single MCP tool.
      155 PyPI
      8
      MIT
    • A
      license
      A
      quality
      D
      maintenance
      Safe MCP server for math operations: evaluate, simplify, and differentiate expressions with configurable precision, using a sandboxed worker thread and strict AST allowlist to block dangerous input.
      3
      MIT
    • A
      license
      Not graded
      quality
      B
      maintenance
      Enables AI agents to perform safe sandboxed expression evaluation, symbolic calculus such as derivatives, simplification, equation solving and integration, statistical analysis, and JSON-native matrix operations. Everything is computed statelessly through the Model Context Protocol over stdio, HTTP, or SSE.
      MIT