计算器服务

一个基于MCP协议的计算器服务,提供基本的算术运算功能,支持多种通信模式(HTTP、WebSocket、stdio),适用于生产部署和Smithery集成。

  • by kumartheashwani
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 kumartheashwani/vault-python-mcp-server

使用说明

MCP计算器服务器

带有基本计算器工具的模型上下文协议(MCP)服务器实现。该服务器可以部署在Smithery中,并通过REST API提供算术运算。

特性

  • 基本算术运算(加、减、乘、除)
  • 符合MCP的API端点
  • JSON模式验证
  • 错误处理
  • 多种通信模式(HTTP、WebSocket、stdio)
  • 针对不同部署场景的专门入口点

安装

  1. 创建虚拟环境(推荐):
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate
  1. 安装依赖项:
pip install -r requirements.txt

运行服务器

HTTP模式(建议用于生产和容器)

直接使用uvicorn以纯HTTP模式运行服务器:

# Set environment variable to ensure only HTTP mode runs
export MCP_HTTP_MODE=1  # On Windows: set MCP_HTTP_MODE=1
uvicorn server:app --host 0.0.0.0 --port 8000

或者使用提供的脚本:

# On Unix/Linux/Mac
./start-container.sh

# On Windows
start-container.bat

建议将此模式用于:

  • 生产部署
  • 容器环境
  • 需要可靠HTTP端点的任何场景

Smithery模式(本地工具集成)

对于Smithery集成作为本地工具,请使用stdio模式:

# Set environment variable to ensure only stdio mode runs
export MCP_STDIO_MODE=1  # On Windows: set MCP_STDIO_MODE=1
python server.py

或者使用提供的便利脚本:

# On Unix/Linux/Mac
./start-smithery.sh

# On Windows
start-smithery.bat

此模式专为Smithery的本地工具集成而设计,并通过标准输入/输出进行通信。

重要:不要使用 python server.py 而无需设置环境变量,因为它同时启动HTTP和stdio模式,这可能会导致冲突或超时。

双模式(仅限开发)

同时开发和测试两个接口:

# No environment variables set - runs both modes
python server.py

这会同时启动HTTP服务器和stdio处理程序,但如果关闭stdin,可能会提前退出。不建议将此模式用于生产或Smithery集成。

API终点

标准终结点

  • GET /health:健康检查端点
  • GET /tools:列出可用工具及其模式
  • POST /:MCP协议的JSON-RPC端点
  • WebSocket位于 /:MCP协议的WebSocket端点

Smithery集成端点

  • POST /mcp:用于Smithery集成的专用MCP兼容JSON-RPC端点
  • WebSocket位于 /mcp:用于Smithery集成的专用MCP兼容WebSocket端点

这些专用的MCP端点是专门为Smithery集成设计的,可以自动处理初始化和工具列表,而不需要明确的初始化步骤。

使用计算器工具

REST API

示例请求 /tools 端点:

curl -X GET http://localhost:8000/tools

JSON-RPC(HTTP)

对JSON-RPC端点的请求示例:

curl -X POST http://localhost:8000/ \
  -H "Content-Type: application/json" \
  -d '{"jsonrpc": "2.0", "method": "execute", "params": {"function_calls": [{"name": "calculator", "parameters": {"operation": "add", "numbers": [1, 2, 3, 4]}}]}, "id": 1}'

可用操作:

  • add:将所有数字相加
  • subtract:从第一个数字中减去后续数字
  • multiply:将所有数字相乘
  • divide:将第一个数字除以所有后续数字

错误处理

服务器为以下对象提供明确的错误消息:

  • 无效操作
  • 除以零
  • 操作数不足
  • 参数类型无效
  • JSON-RPC协议错误

部署

Docker容器

构建并运行Docker容器:

docker build -t mcp-calculator-server .
docker run -p 8000:8000 mcp-calculator-server

容器使用 uvicorn 直接确保HTTP服务器通过正确的信号处理可靠启动,使API端点可以在http://localhost:8000.

Smithery集成

本地工具集成(stdio模式)

对于Smithery集成作为本地工具,您 必须 使用具有所需日志配置的stdio模式:

# Set environment variables for stdio mode and logging
export MCP_STDIO_MODE=1  # On Windows: set MCP_STDIO_MODE=1
export LOGGING_CONFIG=stdio  # On Windows: set LOGGING_CONFIG=stdio

# Run the server
python server.py

或者使用提供的便利脚本:

# On Unix/Linux/Mac
./start-smithery.sh

# On Windows
start-smithery.bat

用于Smithery集成的Docker容器

对于容器中的Smithery集成,请使用专用的Smithery Dockerfile:

# Build the Smithery-specific container
docker build -t mcp-calculator-smithery -f Dockerfile.smithery .

# Run the container with stdio mode
docker run -i -e MCP_STDIO_MODE=1 -e LOGGING_CONFIG=stdio mcp-calculator-smithery

或者使用提供的便利脚本:

# On Unix/Linux/Mac
./run-smithery-container.sh

# On Windows
run-smithery-container.bat

此容器是专门为Smithery集成配置的,并在stdio模式下运行,具有所需的日志配置。

Smithery配置

将Smithery配置为将服务器用作本地工具:

{
  "name": "calculator",
  "description": "A basic calculator that can perform arithmetic operations",
  "command": ["python", "server.py"],
  "env": {
    "MCP_STDIO_MODE": "1",
    "LOGGING_CONFIG": "stdio"
  },
  "type": "local"
}

此配置可确保服务器在Smithery作为本地工具运行时通过stdio进行通信,并正确配置日志系统。

重要:对于本地工具集成,您必须将stdio模式(MCP_stdio_mode=1)与LOGGING_CONFIG环境变量一起使用。HTTP模式不适用于本地工具集成。

远程工具集成(HTTP模式)

对于作为远程工具的Smithery集成(例如,在容器中),请使用HTTP模式和专用MCP端点:

{
  "name": "calculator",
  "description": "A basic calculator that can perform arithmetic operations",
  "url": "http://your-container-host:8000/mcp",
  "type": "remote"
}

此配置确保Smithery可以使用专用的MCP-兼容端点通过HTTP访问该工具的API端点。

为了在容器环境中实现最可靠的操作,请在部署配置中直接使用uvicorn:

{
  "name": "calculator",
  "description": "A basic calculator that can perform arithmetic operations",
  "command": ["uvicorn", "server:app", "--host", "0.0.0.0", "--port", "8000"],
  "env": {
    "MCP_HTTP_MODE": "1"
  },
  "type": "remote"
}

这确保了在容器环境中正确的信号处理和更可靠的启动。