MCP数学服务器🧮
模型上下文协议(MCP)服务器的两个完整实现,在两者中都提供数学运算(加法和减法) JavaScript 和 python.
  
🚀 快速开始
Python实现(推荐)🐍
cd python
python3 calc.py add 25 17 # → 25.0 + 17.0 = 42.0
python3 calc.py subtract 100 30 # → 100.0 - 30.0 = 70.0JavaScript实现🟨
cd javascript
npm install
node calc.js add 25 17 # → 25 + 17 = 42
node calc.js subtract 100 30 # → 100 - 30 = 70📁 项目结构
mcp-math-servers/
├── 📖 README.md # This file
├── 🙈 .gitignore # Git ignore rules
├── 🐍 python/ # Python implementation
│ ├── server.py # MCP server (no dependencies!)
│ ├── calc.py # CLI calculator
│ ├── test_client.py # Automated tests
│ └── README.md # Python-specific docs
└── 🟨 javascript/ # JavaScript implementation
├── index.js # MCP server (uses MCP SDK)
├── calc.js # CLI calculator
├── test-client.js # Automated tests
├── package.json # Dependencies
└── README.md # JavaScript-specific docs🎯 可用工具
这两种实现都提供了相同的MCP工具:
add
将两个数字相加。
- 参数:
a(数字),b(编号) - 例子:
add(5, 3)→"5 + 3 = 8"
subtract
从第一个数字中减去第二个数字。
- 参数:
a(数字),b(编号) - 例子:
subtract(10, 4)→"10 - 4 = 6"
🔧 使用方法
1.命令行界面(最快)
# Python
python3 python/calc.py add 15 27
# JavaScript
node javascript/calc.js add 15 272.自动化测试
# Python
python3 python/test_client.py
# JavaScript
node javascript/test-client.js3.直接MCP集成
这两台服务器都实现了完整的MCP协议,可以与Claude Desktop或其他MCP客户端集成。
🆚 实施比较
| 特性 | Python🐍 | JavaScript🟨 |
|---|---|---|
| 依赖项 | ✅ 无 (仅限stdlib) | ❌ 需要Node.js+MCP SDK |
| 设置时间 | ✅ 瞬间 | ❌ npm install 必填项 |
| 可读性 | ✅ 非常干净 | ✅ 现代ES6+ |
| 学习曲线 | ✅ 初学者友好 | ❌ 更复杂 |
| 演出 | ✅ 快速 | ✅ I/O速度稍快 |
| JSON处理 | ✅ 内置 | ✅ 原住民 |
| 错误信息 | ✅ 很清晰 | ✅ 很好 |
🛠️ 安装和设置
Python版本(推荐)
cd python
# No installation needed! Uses Python standard library only
python3 calc.py add 5 3JavaScript版本
cd javascript
npm install # Install MCP SDK
node calc.js add 5 3🧪 测试
这两种实现都包括全面的测试套件:
# Test Python version
cd python && python3 test_client.py
# Test JavaScript version
cd javascript && node test-client.js🔌 与Claude Desktop集成
将任一服务器添加到您的Claude Desktop MCP配置中:
Python服务器
{
"mcpServers": {
"math-python": {
"command": "python3",
"args": ["/path/to/mcp-math-servers/python/server.py"]
}
}
}JavaScript服务器
{
"mcpServers": {
"math-js": {
"command": "node",
"args": ["/path/to/mcp-math-servers/javascript/index.js"]
}
}
}📚 学习资源
此存储库非常适合:
- 学习MCP协议 -见两种不同的实施方法
- 了解JSON-RPC -请求/响应处理的清晰示例
- 比较语言 -Python与JavaScript的功能相同
- 构建自己的工具 -用作自定义MCP服务器的模板
🤝 贡献
请随意:
- 添加更多数学运算(乘法、除法等)
- 用其他语言实现(Go、Rust等)
- 改进错误处理
- 添加更全面的测试
- 加强文件编制
📄 许可证
MIT许可证-您可以自由使用此代码学习或构建自己的MCP服务器!
🎉 快速示例
# Python examples
python3 python/calc.py add 123.45 67.89 # → 191.34
python3 python/calc.py subtract -10 5 # → -15.0
# JavaScript examples
node javascript/calc.js add 123.45 67.89 # → 191.34
node javascript/calc.js subtract -10 5 # → -15
# Run all tests
python3 python/test_client.py # Python tests
node javascript/test-client.js # JavaScript tests______________________________________________________________________
为了简单起见选择Python,为了生态系统兼容性选择JavaScript。两者都完美地工作! 🚀
