计算器MCP服务,输入字符串的计算式,返回计算结果,更新信息:添加幂运算支持,放宽计算结果小数位数限制,并增强了错误处理能力。
Project description
Calculator MCP Server 🧮
基于 Python AST 的安全表达式计算器 MCP 服务,支持基础数学运算和错误防护机制。
功能特性
- ✅ 支持加减乘除四则运算(
+,-,*,/) - ✅ 支持幂运算(
**) - ✅ 支持正负号处理(
+5,-3) - ✅ 智能格式化计算结果:
- 整数结果直接显示整数
- 小数结果保留最多15位有效数字
- 自动去除末尾多余的零
- ⚠️ 安全防护:
- 拦截代码注入风险(通过 AST 解析而非
eval) - 除零错误防护
- 语法错误提示
- 类型安全检查
- 幂运算安全检查
- 拦截代码注入风险(通过 AST 解析而非
安装方式
pip install mcp-calculate-server
快速使用
from mcp.server.fastmcp import FastMCP
# 启动计算器服务
calculator = FastMCP("CalculatorServer")
calculator.run(transport='stdio')
API 接口
perform_calculation(expression: str) -> str
接收数学表达式字符串,返回格式化结果或错误信息
>>> perform_calculation("5+3")
"🧮 计算结果:5+3 = 8"
>>> perform_calculation("10/3")
"🧮 计算结果:10/3 = 3.333333333333333"
>>> perform_calculation("10**20")
"🧮 计算结果:10**20 = 100000000000000000000"
>>> perform_calculation("2**3")
"🧮 计算结果:2**3 = 8"
>>> perform_calculation("10/0")
"❌ 错误:除数不能为零"
>>> perform_calculation("0**-1")
"❌ 错误:零的负数次幂未定义"
>>> perform_calculation("(-1)**0.5")
"❌ 错误:负数的非整数次幂未定义"
支持的运算符
| 操作符 | 说明 | 示例 |
|---|---|---|
| + | 加法 | 3+4 |
| - | 减法 | 5-2 |
| * | 乘法 | 6*7 |
| / | 除法 | 8/2 |
| ** | 幂运算 | 2**3 |
| -x | 取负 | -5 |
| +x | 取正 | +3 |
错误码说明
| 错误类型 | 示例输出 |
|---|---|
| 语法错误 | ❌ 表达式语法错误:unexpected EOF |
| 不支持的运算符 | ❌ 不支持的运算符:Mod |
| 除零错误 | ❌ 错误:除数不能为零 |
| 非法操作数类型 | TypeError: 不支持的常量类型 |
| 零的负数次幂 | ❌ 错误:零的负数次幂未定义 |
| 负数的非整数次幂 | ❌ 错误:负数的非整数次幂未定义 |
依赖环境
- Python 3.13+
mcp-server库(自动安装)
开源协议
MIT License,详情见 LICENSE
源码地址
GitHub: https://github.com/xiaoyingv/mcp_calculate_server
贡献指南
欢迎提交 PR 和报告 issue,提交前请阅读 CONTRIBUTING.md
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mcp_calculate_server-1.0.0.tar.gz.
File metadata
- Download URL: mcp_calculate_server-1.0.0.tar.gz
- Upload date:
- Size: 4.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4b7e9cb3e92caef1c0397379b075ea7cfe46161cd3118e1843ca3bd6806fd4a1
|
|
| MD5 |
87211aa56aeb0677c180450d13c26f9a
|
|
| BLAKE2b-256 |
e1efc6a116fe1755a1d61ff39e19f400ecce85463ba3c4b6d7cf537bee7236ee
|
File details
Details for the file mcp_calculate_server-1.0.0-py3-none-any.whl.
File metadata
- Download URL: mcp_calculate_server-1.0.0-py3-none-any.whl
- Upload date:
- Size: 5.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f42fd742eb3d43010fb039cd4ddb2b5383ff40b2cb37540d41fcbf125d350720
|
|
| MD5 |
6fdf17981b3b4834cf2abfb06ef9c6f9
|
|
| BLAKE2b-256 |
d5168fb7b49a0a40d831052d079663f906976e03c15cad22ca529fe887aa9223
|