Skip to main content
Glama
leesdream

Calculator MCP Server

by leesdream

Calculator MCP 服务器

一个极简的 MCP 服务器,提供四个工具 —— add、subtract、multiply、divide —— 有两个版本:

  • calculator_server.py —— 使用 Python mcp SDK(FastMCP)构建。 @mcp.tool() 从类型提示/文档字符串中推导 JSON schema,并 为你处理 JSON-RPC 协议。

  • calculator_server_raw.py —— 同一个服务器,仅用标准库(json + sys)从零实现,不依赖 mcp。它 手工实现了 JSON-RPC 2.0 消息处理,以展示 FastMCP 在底层所做的工作: 从 stdin 读取换行分隔的 JSON 请求,根据 method(initialize、tools/list、tools/call、ping)分发,向 stdout 写入 JSON-RPC 响应。有关帧格式的细节,请参阅文件顶部的文档字符串 (没有 LSP 风格的头部,每行只有一个 JSON 对象)。

设置

python -m venv .venv
.venv\Scripts\pip install -r requirements.txt

Related MCP server: Math MCP Server

直接运行(stdio)

MCP 服务器通过 stdio(或 HTTP)通信,而不是普通的命令行 —— 你不能"运行并 看到输出",而是由客户端连接它。有两种尝试方式:

1. MCP Inspector(可视化、交互式) —— 需要 Node.js/npx:

.venv\Scripts\mcp dev calculator_server.py

会打开一个浏览器界面,你可以在其中手动调用 add、multiply 等。

如果页面加载了但显示"Connect to an MCP server to start inspecting", 并且没有 Tools 标签页,说明侧边栏没有自动获取命令 (如果缓存了过时/之前的会话配置,可能会发生这种情况)。请手动填写 侧边栏并点击 Connect:

  • Transport Type:STDIO

  • Command:C:\Users\simon\mcpserversample\.venv\Scripts\python.exe

  • Arguments:C:\Users\simon\mcpserversample\calculator_server.py

连接后,顶部导航中会出现一个 Tools 标签页,你可以点击 "List Tools" 并运行每个工具。

2. test_client.py —— 本仓库中包含的一个脚本化客户端,用于 连接、列出工具并调用每个工具。默认使用 calculator_server.py, 也可以传入路径来测试另一个版本:

.venv\Scripts\python test_client.py
.venv\Scripts\python test_client.py calculator_server_raw.py

接入 Claude Desktop

添加到 claude_desktop_config.json:

{
  "mcpServers": {
    "calculator": {
      "command": "C:\\Users\\simon\\mcpserversample\\.venv\\Scripts\\python.exe",
      "args": ["C:\\Users\\simon\\mcpserversample\\calculator_server.py"]
    }
  }
}

工作原理

  • calculator_server.py 创建一个 FastMCP 服务器,并通过 @mcp.tool() 装饰器将每个 Python 函数注册为工具。函数的类型 提示和文档字符串成为工具的 JSON schema 和描述,也就是 调用客户端(Claude、Inspector 或 test_client.py)所看到的内容。

  • mcp.run() 使用 MCP stdio 传输启动服务器,在 stdin/stdout 上监听 —— 这是客户端启动本地 MCP 服务器作为子进程并与之通信的标准方式。

  • 客户端连接后,调用 list_tools() 来发现可用工具,然后 调用 call_tool(name, args) 来调用某个工具并获取结果。

Related MCP Connectors

Related MCP Servers

  • F
    license
    Not graded
    quality
    D
    maintenance
    A demonstration MCP server that provides calculator functionality through both stdio and FastAPI implementations. Enables users to perform mathematical calculations via MCP tools using the FastMCP framework.
    -
  • F
    license
    A
    quality
    C
    maintenance
    Provides four fundamental arithmetic operations (add, subtract, multiply, divide) as MCP tools with zero-division protection.
    4
    -
  • F
    license
    Not graded
    quality
    C
    maintenance
    Enables arithmetic operations such as addition and calculating averages, exposing tools via MCP with stdio and Streamable HTTP transports.
    -
  • F
    license
    A
    quality
    C
    maintenance
    Enables AI agents to perform arithmetic operations — addition, subtraction, multiplication, and division with divide-by-zero validation — through MCP tool calls. It can be run locally over stdio or as an HTTP microservice via streamable-http transport.
    4
    -