使用说明
创建自定义服务器
开始构建自己的服务器,以便在Claude中用于Desktop和其他客户端。
在本教程中,我们将构建一个简单的MCP计算器服务器,并将其连接到主机Claude for Desktop。
什么是MCP?
主控程序 代表 模型上下文协议 --它是一种协议,允许开发人员使用自定义工具和服务器扩展人工智能助手(如Claude)。
- 把它想象成给你的人工智能助手 额外功率.
- 这些服务器公开了助手在对话中可以使用的工具或功能。
- 例如:你可以构建天气服务器、计算器、财务助理或任何你想象中的东西。
我们将要建造什么
我们将构建一个公开四个工具的服务器: add, subtract, multiply,以及 divide。然后我们将服务器连接到MCP主机(在本例中,Claude for Desktop):
备注:服务器可以连接到任何客户端。出于演示目的,我们在这里选择了Claude作为桌面。
Why Claude for Desktop and not Claude.ai?
Because servers are locally run, MCP currently only supports desktop hosts.MCP核心概念
MCP服务器可以提供三种主要功能:
- 资源:可以由客户端读取的类文件数据(如API响应或文件内容)。
- 工具:LLM可以调用的函数(经用户批准)。
- 鼓励:帮助用户完成特定任务的预先编写的模板。
本教程将主要关注工具。
让我们开始构建我们的计算器服务器! 你可以在这里找到我们将要构建的完整代码。
预备知识
本快速入门假定您熟悉:
- python
- 像克劳德这样的法学硕士
系统要求
- 已安装Python 3.10或更高版本。
- 您必须使用Python MCP SDK 1.2.0或更高版本。
设置您的环境
首先,让我们安装 uv 并设置我们的Python项目和环境:
MacOS/Linux
curl -LsSf https://astral.sh/uv/install.sh | sh
视窗
powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"
确保之后重新启动终端,以确保 uv 命令被接收。
现在,让我们创建并设置我们的项目:
MacOS/Linux
# Create a new directory for our project
uv init calculator
cd calculator
# Create virtual environment and activate it
uv venv
source .venv/bin/activate
# Install dependencies
uv add "mcp[cli]"
# Create our server file
touch calculator.py
视窗
# Create a new directory for our project
uv init calculator
cd calculator
# Create virtual environment and activate it
uv venv
.venv\Scripts\activate
# Install dependencies
uv add "mcp[cli]"
# Create our server file
new-item calculator.py
现在,让我们开始构建您的服务器。
构建您的服务器
导入包并设置实例
将这些添加到您的顶部 calculator.py:
from mcp.server.fastmcp import FastMCP
# Initialize FastMCP server
mcp = FastMCP("calculator")
实施工具执行
工具执行处理程序负责实际执行每个工具的逻辑。让我们添加它:
@mcp.tool()
def add(a: float, b: float) -> float:
"""Add two numbers."""
return a + b
@mcp.tool()
def subtract(a: float, b: float) -> float:
"""Subtract two numbers."""
return a - b
@mcp.tool()
def multiply(a: float, b: float) -> float:
"""Multiply two numbers."""
return a * b
@mcp.tool()
def divide(a: float, b: float) -> float:
"""Divide two numbers."""
if b == 0:
raise ValueError("Division by zero is not allowed.")
return a / b
运行服务器
最后,让我们初始化并运行服务器:
if __name__ == "__main__":
# Initialize and run the server
mcp.run(transport='stdio')
您的服务器已完成!跑 uv run calculator.py 确认一切正常。
使用Claude for Desktop测试您的服务器
备注:Claude for Desktop尚未在Linux上可用。
我们需要为您想要使用的任何MCP服务器配置Claude for Desktop。为此,请在以下位置打开Claude for Desktop App配置 ~/Library/Application Support/Claude/claude_desktop_config.json 在文本编辑器中。如果文件不存在,请确保创建该文件。
MacOS/Linux
code ~/Library/Application\ Support/Claude/claude_desktop_config.json
视窗
code $env:AppData\Claude\claude_desktop_config.json
然后,您将在 mcpServers 钥匙。如果至少有一台服务器配置正确,MCP UI元素将仅显示在Claude for Desktop中。
在这种情况下,我们将添加单个计算器服务器,如下所示:
MacOS/Linux
{
"mcpServers": {
"calculator": {
"command": "uv",
"args": [
"--directory",
"/ABSOLUTE/PATH/TO/PARENT/FOLDER/calculator",
"run",
"calculator.py"
]
}
}
}
视窗
{
"mcpServers": {
"calculator": {
"command": "uv",
"args": [
"--directory",
"C:\\ABSOLUTE\\PATH\\TO\\PARENT\\FOLDER\\calculator",
"run",
"calculator.py"
]
}
}
}
警告:您可能需要填写到的完整路径
uv可执行于command现场。你可以通过跑步来获得这个which uv在MacOS/Linux或where uv在Windows上。
确保传入服务器的绝对路径。
此消息克劳德桌面:
- 有一个名为“计算器”的MCP服务器。
- 通过运行来启动它
uv --directory /ABSOLUTE/PATH/TO/PARENT/FOLDER/calculator run calculator.py.
保存文件,然后重新启动 Claude桌面版.
由于此计算器支持基本算术运算,因此可用于加法、减法、乘法和除法。
更多信息: MCP官方文件
