Back to MCP directory
publicPublicdnsLocal runtime

simple-calculator-mcp-server

本项目是一个基于MCP协议的自定义计算器服务器教程,通过Python实现加减乘除功能,并与Claude桌面客户端集成,扩展AI助手的能力。

article

README

🚀 创建自定义服务器指南

本指南将指导你构建自己的服务器,用于Claude桌面版和其他客户端。通过本教程,你将学会构建一个简单的MCP计算器服务器,并将其连接到Claude桌面版主机。

✨ 主要特性

  • MCP协议扩展:利用模型上下文协议(MCP)扩展Claude等AI助手的功能。
  • 自定义工具创建:可以构建各种工具,如天气服务器、计算器、财务管理助手等。
  • 多系统支持:支持在MacOS、Linux和Windows系统上创建和配置服务器。

📦 安装指南

先决条件

  • 具备基础的Python编程经验。
  • 对大型语言模型(LLM)有基本了解。
  • 系统要求:

创建项目并初始化环境

MacOS/Linux

# 安装uv CLI 工具(如果尚未安装)
brew install model-context-protocol/protobuf/protobuf-cpp

# 初始化新项目
mkdir calculator && cd calculator

Windows

# 安装uv CLI 工具(如果尚未安装)
# 需要从MCP官方网站下载并安装Windows版本的uv工具。

# 初始化新项目
mkdir calculator && cd calculator

💻 使用示例

基础用法

实现代码

以下是实现计算器服务器的完整代码:

# 导入库和模块
from fastapi import FastAPI
from typing import Optional, Union
import uvicorn

# 定义工具
app = FastAPI()

@app.get("/calculate")
async def calculate(
    expression: str,
    error_handling: Optional[bool] = True,
    simplify: Optional[bool] = True
) -> Union[str, dict]:
    """Calculate a mathematical expression."""
    
    try:
        result = eval(expression)
        if simplify:
            return f"{expression} 的结果是 {result}"
        return str(result)
    except Exception as e:
        if error_handling:
            return {"error": str(e)}
        raise

# 启动服务器
if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=8000)

配置Claude桌面版

MacOS/Linux

编辑配置文件:

code ~/Library/Application\ Support/Claude/claude_desktop_config.json

将以下内容添加到 mcpServers 键下:

{
    "mcpServers": {
        "calculator": {
            "command": "uv",
            "args": [
                "--directory",
                "/ABSOLUTE/PATH/TO/PARENT/FOLDER/calculator",
                "run",
                "calculator.py"
            ]
        }
    }
}

Windows

编辑配置文件:

code $env:AppData/Claude/claude_desktop_config.json

将以下内容添加到 mcpServers 键下:

{
    "mcpServers": {
        "calculator": {
            "command": "uv",
            "args": [
                "--directory",
                "C:\\ABSOLUTE\\PATH\\TO\\PARENT\\FOLDER\\calculator",
                "run",
                "calculator.py"
            ]
        }
    }
}

⚠️ 重要提示

请确保将 /ABSOLUTE/PATH/TO/PARENT/FOLDER 替换为你的实际路径。

保存文件后,重启Claude桌面版,即可在Claude桌面版中使用计算器服务器进行基本的算术运算。

📚 详细文档

什么是MCP?

MCP模型上下文协议,它是一种允许开发者扩展AI助手(如Claude)功能的协议。可以理解为给AI助手 增加额外能力,这些服务器公开工具或功能,助手可以在对话期间使用。例如,你可以构建天气服务器、计算器、财务管理助手,或者任何你能想象的功能。

我们将要构建的内容

我们将构建一个提供四个工具的服务器:addsubtractmultiplydivide,然后将该服务器连接到MCP主机(在此示例中为Claude桌面版)。

核心概念

  • 资源:允许助手访问外部数据或服务。
  • 工具:扩展助手的功能。
  • 提示:自定义助手的交互方式。

此次重点放在 工具 上,因为我们将创建一个计算器工具。

help

Runtime guide

cloud

Hosted runtime

Hosted servers run from a provider-managed environment. You usually connect the MCP client to the hosted endpoint or follow the provider's authorization flow, without keeping a local process alive

  1. Open provider connection page
  2. Authorize or copy endpoint
  3. Connect from your MCP client
terminal

Local runtime / other methods

Local servers run on your own machine or infrastructure. You normally copy the server_config into your MCP client, install the required package, and provide env variables from env_schema when needed

  1. Copy server_config
  2. Install required package
  3. Fill env variables and restart client