Skip to main content

计算器MCP服务,输入字符串的计算式,返回计算结果

Project description

Calculator MCP Server 🧮

PyPI version License Python Version

基于 Python AST 的安全表达式计算器 MCP 服务,支持基础数学运算和错误防护机制。

功能特性

  • ✅ 支持加减乘除四则运算(+, -, *, /
  • ✅ 支持正负号处理(+5, -3
  • ✅ 自动格式化计算结果(保留两位小数)
  • ⚠️ 安全防护:
    • 拦截代码注入风险(通过 AST 解析而非 eval
    • 除零错误防护
    • 语法错误提示
    • 类型安全检查

安装方式

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.00"

>>> perform_calculation("10/0")
"❌ 错误:除数不能为零"

>>> perform_calculation("2**3")
"❌ 不支持的运算符:Power"

支持的运算符

操作符 说明 示例
+ 加法 3+4
- 减法 5-2
* 乘法 6*7
/ 除法 8/2
-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]

请根据实际情况修改以下内容:
1. 替换 GitHub 仓库地址和 PyPI 的 badge 链接
2. 添加实际版本号(当前使用占位符)
3. 补充测试说明或更多使用示例(如有)
4. 如果需要,可添加 CI/CD 状态徽章

这个 README 包含:

- 清晰的功能列表和安全特性
- 使用示例和错误处理说明
- API 接口文档
- 支持的操作符表格
- 错误码说明
- 安装和开发信息
- 开源协议和贡献指引 

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mcp_calculate_server-0.1.1.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mcp_calculate_server-0.1.1-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file mcp_calculate_server-0.1.1.tar.gz.

File metadata

  • Download URL: mcp_calculate_server-0.1.1.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for mcp_calculate_server-0.1.1.tar.gz
Algorithm Hash digest
SHA256 f6215bf8ac7e1b6498df3facafc18dc44493695a056d003419c6772a2d644aa5
MD5 64b7ce51a665252b8b3554b0a44850ab
BLAKE2b-256 55ed24504905d97b6ac20ea019a97763377aaa7fa4a6b357175abd2fd036651b

See more details on using hashes here.

File details

Details for the file mcp_calculate_server-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for mcp_calculate_server-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ea157fad8cd3d12157c719f59f488da0fadb7f9a0854b49d498dff838a574cf9
MD5 756c5b3490e106eac23bcd3fd853ccfe
BLAKE2b-256 0adb94f677b621b14d36b5a2aac71c9584ddacd36c3ef613f974d8ac104b7d13

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page