高性能数学计算服务器

一个基于Rust构建的高性能数学计算服务器,支持Model Context Protocol (MCP)和HTTP API,适用于Claude Desktop和Llamastack等AI框架的集成。

  • by cvicens
  • Rust
  • 本地部署
  • 更新于
在 GitHub 查看 cvicens/calculator-mcp-server

使用说明

🧮 计算器MCP服务器

Rust内置的高性能数学计算器服务器,支持两者 模型上下文协议(MCP) 克劳德桌面和 HTTP API 用于与Llamastack和其他AI框架集成。

✨ 特性

🔢 数学运算

  • 基本算术:带多个操作数的加法、减法、乘法、除法
  • 高等数学:幂、平方根、n次根
  • 三角学:Sin、cos、tan及其逆(度/弧度)
  • 对数:自然原木、10基、2基和自定义基
  • 组合数学:阶乘、排列、组合
  • 统计:平均值、中位数、众数、方差、标准差
  • 编号系统:二进制、八进制、十进制、十六进制转换(基数2-36)

🚀 双接口支持

  • MCP服务器:与Claude Desktop直接集成
  • HTTP API:Llamastack和web服务的RESTful端点
  • 共享核心:两个接口的高性能Rust逻辑相同

⚡ 演出

  • 原生锈蚀速度:零开销抽象
  • 任意精度:使用BigInt支持大阶乘
  • 亚毫秒计算:闪电般快速的数学运算
  • 内存效率高:资源使用最少

🏗️ 建筑

┌─────────────────┐    ┌─────────────────┐
│   Claude Desktop │    │   Llamastack   │
│                 │    │   / Web Apps    │
└─────────┬───────┘    └─────────┬───────┘
          │                      │
          │ JSON-RPC             │ HTTP/REST
          │ (stdin/stdout)       │ (TCP:8080)
          │                      │
          ▼                      ▼
┌─────────────────────────────────────────┐
│           Calculator Core               │
│    (Shared Rust Implementation)        │
└─────────────────────────────────────────┘

🚀 快速开始

先决条件

  • Rust 1.70+已安装
  • 克劳德桌面(供MCP使用)

构建两台服务器

# Clone the repository
git clone <your-repo-url>
cd calculator-mcp-server

# Build both MCP and HTTP servers
make all

适用于克劳德桌面(MCP)

# Package for Claude Desktop
make pack-mcp

# Install the generated calculator-mcp-server.dxt file in Claude Desktop
# Settings > Extensions > Install Extension

对于调用堆栈/HTTP(API)

# Start HTTP server
make test-http

# Server runs on http://localhost:8080

📋 使用示例

Claude桌面集成

一旦安装为MCP扩展,只需询问Claude:

"Calculate the factorial of 15"
"Convert binary 1101 to hexadecimal" 
"What's the combination C(10,3)?"
"Calculate statistics for: 23, 45, 12, 67, 34, 89, 56"

HTTP API使用情况

健康检查

curl http://localhost:8080/health

计算因子

curl -X POST http://localhost:8080/factorial \
  -H 'Content-Type: application/json' \
  -d '{"n": 20}'

数字基数转换

curl -X POST http://localhost:8080/convert \
  -H 'Content-Type: application/json' \
  -d '{"value": "FF", "from_base": 16, "to_base": 2}'

基本算术

curl -X POST http://localhost:8080/arithmetic \
  -H 'Content-Type: application/json' \
  -d '{"operation": "multiply", "operands": [12, 15, 8]}'

🛠️ 可用工具/端点

工具/端点 描述 参数
basic_arithmetic 加法、减法、乘法、除法 operation, operands[]
power_and_roots 幂、平方根、n次根 operation, base, exponent?
trigonometry 三角函数 function, value, angle_unit?
logarithms 对数计算 value, base?
factorial_combinatorics 阶乘、排列、组合 operation, n, r?
statistics 描述性统计 values[]
number_conversion 基础转换(2-36) value, from_base, to_base

🔧 发展

项目结构

calculator-mcp-server/
├── src/
│   ├── lib.rs           # Shared calculator logic
│   ├── mcp_server.rs    # MCP JSON-RPC server
│   └── http_server.rs   # HTTP API server (Axum)
├── Makefile            # Build automation
├── manifest.json       # MCP extension manifest
└── README.md          # This file

构建命令

make build-mcp      # Build MCP server only
make build-http     # Build HTTP server only  
make pack-mcp       # Package for Claude Desktop
make test-mcp       # Test MCP server
make test-http      # Start HTTP server for testing
make clean          # Clean build artifacts
make help           # Show all commands

测试

# Test MCP server
echo '{"jsonrpc":"2.0","method":"tools/list","id":1}' | ./target/release/mcp-server

# Test HTTP endpoints
curl http://localhost:8080/tools
curl -X POST http://localhost:8080/factorial -H 'Content-Type: application/json' -d '{"n": 12}'

📊 性能基准

操作 输入 时间 结果
阶乘 170! <1ms 7.257415615…e+306
基数转换 32位数字 <1ms 任意基数2-36
统计 1000个数字 <5ms 完整分析
三角测量 任意角度 <1ms 高精度

🌐 HTTP API参考

基本URL

http://localhost:8080

端点

GET /health

健康检查端点

{
  "status": "healthy",
  "version": "1.0.0"
}

GET /tools

列出可用的数学工具

{
  "tools": [
    {
      "name": "basic_arithmetic",
      "description": "Basic arithmetic operations",
      "parameters": {...}
    }
  ]
}

POST /calculate

通用计算端点

{
  "tool": "factorial_combinatorics",
  "arguments": {
    "operation": "factorial",
    "n": 15
  }
}

响应格式

{
  "success": true,
  "result": "1307674368000",
  "calculation_details": {
    "operation": "15!",
    "result": "1307674368000",
    "decimal_approximation": 1307674368000.0,
    "calculation_time_ms": 0
  }
}

🔌 集成示例

Python客户端

import requests

class CalculatorClient:
    def __init__(self, base_url="http://localhost:8080"):
        self.base_url = base_url
    
    def factorial(self, n):
        response = requests.post(f"{self.base_url}/factorial", 
                               json={"n": n})
        return response.json()
    
    def convert_base(self, value, from_base, to_base):
        response = requests.post(f"{self.base_url}/convert", 
                               json={
                                   "value": value, 
                                   "from_base": from_base, 
                                   "to_base": to_base
                               })
        return response.json()

# Usage
calc = CalculatorClient()
result = calc.factorial(20)
print(f"20! = {result['result']}")

JavaScript/Node.js客户端

class CalculatorClient {
    constructor(baseUrl = 'http://localhost:8080') {
        this.baseUrl = baseUrl;
    }
    
    async factorial(n) {
        const response = await fetch(`${this.baseUrl}/factorial`, {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({ n })
        });
        return await response.json();
    }
    
    async convertBase(value, fromBase, toBase) {
        const response = await fetch(`${this.baseUrl}/convert`, {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({ 
                value, 
                from_base: fromBase, 
                to_base: toBase 
            })
        });
        return await response.json();
    }
}

// Usage
const calc = new CalculatorClient();
const result = await calc.factorial(15);
console.log(`15! = ${result.result}`);

🎯 用例

Claude桌面用户

  • 数学研究:具有详细结果的复杂计算
  • 编程:基础转换和组合计算
  • 统计:数据分析和描述性统计
  • 教育:逐步的数学运算

Llamastack集成

  • AI模型增强:为LLM提供精确的数学能力
  • 科学计算:用于研究的高性能计算
  • Web应用程序:用于web应用程序的数学微服务
  • API集成:RESTful数学运算

🚦 状态和兼容性

✅ 支持的平台

  • 克劳德桌面:macOS、Windows、Linux
  • HTTP API:任何支持HTTP客户端的平台
  • MCP版本:2024-11-05协议

❌ 不支持

  • 克劳德·艾 (web客户端)-不支持MCP扩展
  • 克劳德移动应用 -不支持MCP扩展

🤝 贡献

  1. 分叉存储库
  2. 创建要素分支(git checkout -b feature/amazing-feature)
  3. 提交您的更改(git commit -m 'Add amazing feature')
  4. 推到分支(git push origin feature/amazing-feature)
  5. 打开拉取请求

开发设置

# Install Rust
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

# Clone and build
git clone <repo-url>
cd calculator-mcp-server
make all

📄 许可证

此项目根据MIT许可证获得许可-请参阅 许可证 文件以获取详细信息。

🙏 致谢

📞 支持


内置于❤️ 在Rust | 由MCP供电 | 为AI集成做好准备