MCP:计算器
MCP 服务配置
复制以下 JSON 到 OPClaw 或其他 MCP 客户端的配置文件中即可使用
{
"mcpServers": {
"calculator": {
"args": [
"string"
],
"command": "python mcp_server.py",
"env": {
"PYTHONPATH": "/app"
}
}
}
}
服务介绍
MCP Mathematical Expression Calculator Server
A mathematical expression calculator server based on FastMCP framework implementing the Model Context Protocol (MCP) standard.
Project Introduction
This project implements a secure mathematical expression calculator that can be called as an MCP tool by other applications. It uses Abstract Syntax Tree (AST) to parse expressions, avoiding code injection risks, while supporting various basic mathematical operations.
Features
- Secure Calculation - Uses AST to parse expressions, avoiding code injection risks
- Basic Operations Support - Supports basic mathematical operations such as addition, subtraction, multiplication, division, modulo, and power operations
- Error Handling - Comprehensive exception handling mechanism, including division by zero and illegal expression detection
- MCP Tool Integration - Can be called as an MCP tool by other applications
Requirements
- Python 3.7+
- fastmcp>=0.1.0
- Flask==2.3.2
- requests==2.31.0
Installation
pip install -r requirements.txt
Usage
Start the server:
python mcp_server.py
Testing
Run the test script:
python test_calculator.py
Supported Operators
- Addition:
+ - Subtraction:
- - Multiplication:
* - Division:
/ - Floor division:
// - Modulo:
% - Power:
** - Parentheses:
()
Example Expressions
2 + 3 * 4(2 + 3) * 43.14 * 2.52 ** 310 // 310 % 3
Project Structure
.
├── mcp_server.py # Main server file
├── test_calculator.py # Test script
├── requirements.txt # Dependencies file
├── README.md # English documentation
└── README_zh.md # Chinese documentation
Contributing
Issues and Pull Requests are welcome for contributions.