Skip to main content
Glama
doudou-fly

Calculator MCP Server

by doudou-fly
README.md
# Calculator MCP Server

一个简单的计算器 MCP 服务器,基于 [Model Context Protocol](https://modelcontextprotocol.io/) 协议,提供加减乘除四则运算功能。

## 项目简介

Calculator MCP Server 是一个基于 TypeScript 开发的 Model Context Protocol 服务器,为 AI 客户端提供标准化的计算工具能力。通过 MCP 协议,AI 助手可以自动调用加、减、乘、除四则运算工具,无需用户手动计算。

### 功能特性

- ➕ **加法运算**: 计算两个数的和
- ➖ **减法运算**: 计算两个数的差
- ✖️ **乘法运算**: 计算两个数的积
- ➗ **除法运算**: 计算两个数的商(自动校验除数不为零)
- 🔒 **参数校验**: 基于 zod 的自动参数类型校验
- 📝 **调用日志**: 服务器端输出每次工具调用的详细日志

### 工具列表

| 工具名 | 功能 | 参数 |
|--------|------|------|
| `add` | 加法 | `a: number`, `b: number` |
| `subtract` | 减法 (a - b) | `a: number`, `b: number` |
| `multiply` | 乘法 | `a: number`, `b: number` |
| `divide` | 除法 (a ÷ b) | `a: number`, `b: number` |

### 技术栈

- TypeScript + Node.js
- @modelcontextprotocol/sdk(McpServer 高层 API)
- zod(参数校验)

## 部署指南

### 环境依赖

- Node.js >= 18

### 本地构建

```bash
# 安装依赖
npm install

# 构建
npm run build
```

构建完成后生成 `dist/index.js`,MCP 客户端会通过 stdio 方式调用它,无需手动启动。

### 服务配置

在 MCP 客户端中添加以下 STDIO 类型服务配置:

```json
{
  "mcpServers": {
    "calculator": {
      "command": "npx",
      "args": [
        "-y",
        "calculator-mcp-server"
      ]
    }
  }
}
```

Windows 系统下,建议使用以下配置:

```json
{
  "mcpServers": {
    "calculator": {
      "command": "cmd",
      "args": [
        "/c",
        "npx",
        "-y",
        "calculator-mcp-server"
      ]
    }
  }
}
```

如果使用本地源码运行,请将 `args` 替换为实际路径:

```json
{
  "mcpServers": {
    "calculator": {
      "command": "node",
      "args": ["D:\\project\\open-mcp\\dist\\index.js"]
    }
  }
}
```

### 各客户端配置示例

#### Trae

打开 Trae 设置 → MCP 服务器,添加以下配置:

```json
{
  "mcpServers": {
    "calculator": {
      "command": "node",
      "args": ["D:\\project\\open-mcp\\dist\\index.js"]
    }
  }
}
```

#### MCP Inspector 调试

```bash
npx @modelcontextprotocol/inspector node dist/index.js
```

浏览器打开后,可以在可视化界面中直接测试所有工具。

#### 命令行测试

项目提供了测试脚本 `test.cjs`:

```bash
# 查看所有工具
node test.cjs list

# 调用具体工具(参数:工具名 a b)
node test.cjs call add 3 5
node test.cjs call multiply 123 456
node test.cjs call divide 100 3
```

## 使用示例

### 示例 1:加法运算

在 AI 客户端中输入:

> 帮我算一下 3 + 5 等于多少?

AI 自动调用 `add` 工具,返回结果:

```
3 + 5 = 8
```

### 示例 2:乘法运算

> 帮我算一下 123 × 456 等于多少?

AI 自动调用 `multiply` 工具,返回结果:

```
123 × 456 = 56088
```

### 示例 3:除法运算

> 帮我算一下 100 ÷ 3 等于多少?

AI 自动调用 `divide` 工具,返回结果:

```
100 ÷ 3 = 33.333333333333336
```

### 服务器运行日志

工具调用时,服务器终端会输出调用日志:

```
计算器 MCP 服务器已启动
[2026-08-04T05:46:55.953Z] 收到调用: multiply {"a":123,"b":456}
[2026-08-04T05:46:55.954Z] 结果(multiply): 123 × 456 = 56088
```

TDQS

A4/5.0

Scored across 4 tools

Disambiguation5/5

Each tool performs a unique arithmetic operation (addition, subtraction, multiplication, division) with no overlap. The distinct mathematical symbols in descriptions remove any ambiguity.

Naming Consistency5/5

All tool names follow the same verb pattern using the operation name (add, subtract, multiply, divide). This is perfectly consistent and predictable.

Tool Count5/5

Four tools is an ideal scope for a basic calculator server, covering the core arithmetic operations without unnecessary bloat.

Completeness4/5

The four basic operations cover the primary use case for a calculator. A modulo or power operation could be added, but the surface is not severely incomplete.

Maintenance

ActivitySlowing
ResponsivenessNo issues