知识库 · AI 智能体
10分钟!搭建一个本地MCP
使用Cursor和Python SDK,10分钟快速搭建本地MCP服务器。通过添加官方文档作为知识库,让Claude生成实时汇率货币转换工具,包含API调用、错误处理和Pydantic模型。
⏰ 时间:2025-04-06 23:50:20 (UTC+8)
作者 AZane AZane的AGI笔记
今天,我们来使用cursor帮助我们搭建一个简单的本地mcp服务器,并根据自己的喜好进行配置
话不多说,开始吧
首先 打开mcp官方文档 : https://modelcontextprotocol.io/introduction
在这里我们可以看到各种语言的软件开发工具包,你可以选择一个你熟悉的,这里我选择了 Python
点击 跳转到Github界面后,往下滑,找到 Quickstart(快速开始)
复制 下面的代码示例
打开cursor ,在你的项目文件夹下 新建 一个 .py 文件,我这里命名为 first-mcp.py
将刚才复制的代码 粘贴 进去
点击cursor右上角的⚙ 打开设置 界面
将下面两个链接 添加 到cursor的setting/Features/Docs中
分别输入 以下链接(这一步相当于给LLM做知识库)
https://github.com/modelcontextprotocol/python-sdk/blob/main/README.md
https://modelcontextprotocol.io/llms-full.txt
可以参考我的命名
打开命令行cmd ,进行以下操作
- 安装python
- 访问 Python 官网下载你电脑对应系统的 Python 3.8+
- 安装时勾选" Add Python to PATH "
- 验证安装:
python --version
-
安装MCP工具
-
使用pip安装MCP:
pip install mcp
- 或使用推荐的UV工具安装:
pip install uv
uv add "mcp[cli]"
现在可以开始让LLM根据你的需求搭建服务器啦!
点击cursor对话框左上角的@,选择Docs, 引用我们之前添加的两个知识库
根据你的需求 输入提示词 ,这里我想搭建一个根据实时汇率进行货币转换的MCP
修改当前代码,搭建一个货币转换工具:
- 从可靠的url获取中国人民币(CNY)和美元(USD)之间的最新汇率
- 允许用户根据实时汇率将指定金额从人民币转换为美元或反之亦然
- 提供转换后的金额以及当前汇率以供参
注意:
-
明确说明需要实现什么样的功能
-
从核心功能开始,跑通之后再叠加额外功能
Claude思考了一段时间后,写下了以下代码:
# currency-converter.py
from mcp.server.fastmcp import FastMCP
import requests
from typing import Dict, Literal, Optional
from pydantic import BaseModel
# 创建MCP服务器
mcp = FastMCP("Currency Converter")
# 定义转换方向类型
ConversionDirection = Literal["CNY_to_USD", "USD_to_CNY"]
# 定义请求和响应模型
class CurrencyConversionRequest(BaseModel):
amount: float
direction: ConversionDirection
class CurrencyConversionResponse(BaseModel):
original_amount: float
converted_amount: float
original_currency: str
target_currency: str
exchange_rate: float
# 获取最新汇率def get_exchange_rates() -> Dict[str, float]:
try:
# 使用Exchange Rate API获取最新汇率
response = requests.get("<https://api.exchangerate-api.com/v4/latest/USD>")
data = response.json()
# 获取USD到CNY的汇率
usd_to_cny = data["rates"]["CNY"]
# 计算CNY到USD的汇率
cny_to_usd = 1 / usd_to_cny
return {
"USD_to_CNY": usd_to_cny,
"CNY_to_USD": cny_to_usd
}
except Exception as e:
print(f"Error fetching exchange rates: {e}")
# 如果API调用失败,使用备用汇率
return {
"USD_to_CNY": 7.20, # 备用汇率
"CNY_to_USD": 0.139 # 备用汇率
}
# 添加货币转换工具
@mcp.tool()
def convert_currency(amount: float, direction: ConversionDirection) -> CurrencyConversionResponse:
"""
将指定金额在人民币(CNY)和美元(USD)之间相互转换
参数:
- amount: 要转换的金额
- direction: 转换方向,可以是 'CNY_to_USD' 或 'USD_to_CNY'
返回:
- 转换结果信息,包括原始金额、转换后金额、汇率等
"""
# 获取最新汇率
rates = get_exchange_rates()
# 根据转换方向执行转换
if direction == "CNY_to_USD":
original_currency = "CNY"
target_currency = "USD"
exchange_rate = rates["CNY_to_USD"]
converted_amount = amount * exchange_rate
else: # USD_to_CNY
original_currency = "USD"
target_currency = "CNY"
exchange_rate = rates["USD_to_CNY"]
converted_amount = amount * exchange_rate
# 返回结果
return CurrencyConversionResponse(
original_amount=amount,
converted_amount=round(converted_amount, 2),
original_currency=original_currency,
target_currency=target_currency,
exchange_rate=round(exchange_rate, 4)
)
# 启动服务器
if __name__ == "__main__":
mcp.run()
现在我们的服务器代码已经有了,到 命令行cmd 里 运行服务器python文件
转到项目文件目录下 (需要替换成你的项目路径)
cd D:\\AGI\\first-mcp-server
运行
py first-mcp.py
接下来要将服务器接入到cursor里
打开cursor右上角 设置/MCP, 点击 添加
这会 打开配置文件 mcp.json
输入代码以安装我们的mcp服务器
{
"mcpServers": {
"mcp-server-text": {
"command": "python",
"args": [
"D:\\\\AGI\\\\mcp-server-text\\\\first-mcp.py"
]
}
}
}
或以开发者模式安装
{
"mcpServers": {
"mcp-server-text": {
"command": "mcp",
"args": [
"run",
"D:\\\\AGI\\\\mcp-server-text\\\\first-mcp.py"
]
}
}
}
如果你安装了别的mcp服务器,可以看到应该是这个格式,输入时要注意
配置好后,记得按ctrl+s保存
回到设置界面,可以看到配置成功了,并且前面有绿色小点,说明连接上了
如果没有连接上点一下后面刷新
现在提问一下,可以看到成功调用了我们的mcp工具
成功了!!!🚀 🚀 🚀