🔄 单元转换器MCP服务器
这是习俗 模型上下文协议(MCP) 使用Python构建的服务器。它为AI代理(如Claude Desktop或Cursor)提供了精确的工具来转换测量单位,避免了数学计算中的幻觉。
______________________________________________________________________
🛠 特性
服务器公开了以下工具:
| 类别 | 工具 |
|---|---|
| 🌡 温度 | celsius_to_fahrenheit, fahrenheit_to_celsius |
| 📏 长度 | meters_to_feet, feet_to_meters |
| ⚖️ 重量 | kg_to_pounds, pounds_to_kg |
______________________________________________________________________
🚀 安装
1.克隆存储库
git clone
cd 2.设置环境
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
pip install -r requirements.txt______________________________________________________________________
💻 用法
选项1:本地测试
使用 MCP检查员 在没有AI客户端的情况下测试工具:
mcp dev converter.py选项2:连接到Cursor/Windsurf
将此添加到您的 mcp.json 配置文件:
{
"mcpServers": {
"unit_converter": {
"command": "/absolute/path/to/venv/bin/python",
"args": ["/absolute/path/to/converter.py"]
}
}
}⚠️ 注: 替换 /absolute/path/to/... 在您的计算机上显示真实路径。______________________________________________________________________
📄 代码概述
这是主要的 converter.py 实施:
from mcp.server.fastmcp import FastMCP
# Initialize the server
mcp = FastMCP("UnitConverter")
# --- Temperature Tools ---
@mcp.tool()
def celsius_to_fahrenheit(celsius: float) -> float:
"""Convert Celsius to Fahrenheit"""
return (celsius * 9/5) + 32
@mcp.tool()
def fahrenheit_to_celsius(fahrenheit: float) -> float:
"""Convert Fahrenheit to Celsius"""
return (fahrenheit - 32) * 5/9
# --- Length Tools ---
@mcp.tool()
def meters_to_feet(meters: float) -> float:
"""Convert Meters to Feet"""
return meters * 3.28084
@mcp.tool()
def feet_to_meters(feet: float) -> float:
"""Convert Feet to Meters"""
return feet / 3.28084
# --- Weight Tools ---
@mcp.tool()
def kg_to_pounds(kg: float) -> float:
"""Convert Kilograms to Pounds"""
return kg * 2.20462
@mcp.tool()
def pounds_to_kg(pounds: float) -> float:
"""Convert Pounds to Kilograms"""
return pounds / 2.20462
if __name__ == "__main__":
mcp.run(transport="stdio")______________________________________________________________________
📦 需求
mcp[cli]______________________________________________________________________
📝 许可证
MIT许可证-随意使用和修改!
