Calculator MCP Server
Calculator MCP 服务器
一个极简的 MCP 服务器,提供四个工具 ——
add、subtract、multiply、divide —— 有两个版本:
calculator_server.py—— 使用 PythonmcpSDK(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.txtRelated 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:
STDIOCommand:
C:\Users\simon\mcpserversample\.venv\Scripts\python.exeArguments:
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)来调用某个工具并获取结果。
This server cannot be deployed
Maintenance
Related MCP Connectors
Math.js MCP — wraps the mathjs.org API (free, no auth)
Math MCP server for primality, factorization, Java templates, and integer multiplication.
518.PLUS: MCP tool marketplace. Run tools on files, text or JSON, share results, publish your own.
Related MCP Servers
- FlicenseNot gradedqualityDmaintenanceA 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.-
- FlicenseAqualityCmaintenanceProvides four fundamental arithmetic operations (add, subtract, multiply, divide) as MCP tools with zero-division protection.4-
- FlicenseNot gradedqualityCmaintenanceEnables arithmetic operations such as addition and calculating averages, exposing tools via MCP with stdio and Streamable HTTP transports.-
- FlicenseAqualityCmaintenanceEnables 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-