Unit Converter MCP Server
单位转换 API + MCP 教程
构建 FastAPI 应用,使用 FastMCP 进行封装,挂载 MCP HTTP/SSE 端点,注册资源和提示词,并启动 uvicorn。
requirements.txt – Python 依赖项。
前置要求
Python 3.10+(已在 3.12 上测试)。
虚拟环境。
下方的 npm 检查器。
⸻
Related MCP server: mcp-units
从此文件夹设置
python -m venv .venv
# Mac or Gitbash
source .venv/bin/activate
# Windows powershell:
.venv\Scripts\activate
python -m pip install -r requirements.txt⸻
运行 HTTP + MCP 服务器
# start the server
python converter_streamable_http_server.py
# or
python -m converter_streamable_http_server
你将看到:
Swagger UI: http://localhost:8003/docs
ReDoc: http://localhost:8003/redoc
由 FastMCP 提供的 MCP 端点:
streamable-http: http://localhost:8003/mcp
⸻
尝试 HTTP 端点 (curl)
curl -X POST "http://localhost:8003/miles-to-kilometers?miles=3.1" \
-H "Authorization: Bearer 143f4a46d74fee0d7918b2857577868cb3daf9e6e50ee91c2f7975ba26fdb8f7"
# If we use pydantic models
curl -X POST "http://localhost:8003/miles-to-kilometers" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer Y658139cf61948208ed76a4b36122b9552ec5c3f6da5e02f7c5d85d995dede17dE" \
-d "3.1"# Celsius → Fahrenheit
curl -X POST "http://localhost:8003/celsius-to-fahrenheit" \
-H "Content-Type: application/json" \
-d "25"# Fahrenheit → Celsius
curl -X POST "http://localhost:8003/fahrenheit-to-celsius" \
-H "Content-Type: application/json" \
-d "86"# Kilometers → Miles
curl -X POST "http://localhost:8003/kilometers-to-miles" \
-H "Content-Type: application/json" \
-d "5"# Miles → Kilometers (rejects negative values)
curl -X POST "http://localhost:8003/miles-to-kilometers" \
-H "Content-Type: application/json" \
-d "3.1"每个端点返回的 JSON 格式如下:
{ "result": , "operation": "..." } 或 { "error": "..." }(针对无效输入)。
标头与身份验证(所有端点通用)
添加 JSON 内容类型(以及可选的身份验证令牌)
-H "Content-Type: application/json"
-H "Authorization: Bearer "
我们的服务器目前不需要身份验证,可以省略 Authorization 标头。
与 MCP 一起使用(VS Code 示例)
按上述方式启动服务器。
将你的 MCP 客户端指向该进程。
// Example VS Code .vscode/mcp.json entry:
{
"servers": {
"UnitConverter": {
"command": "python",
"args": ["converter_api_tutorial.py"]
}
}
}从 MCP 客户端列出工件。你应该能看到:
工具:celsius_to_fahrenheit, fahrenheit_to_celsius, kilometers_to_miles, miles_to_kilometers
资源:resource://unit_reference, resource://troubleshooting_guide
提示词:explain_conversion, api_usage
⸻
使用 npm MCP 检查器进行检查
在浏览器中探索所有内容(工具、资源、提示词)。
服务器已在 http://localhost:8003 上运行
# If env error appears
npx @modelcontextprotocol/inspector@latest -e DUMMY=1 --url http://localhost:8003/mcp --transport streamable-http
# If you want to test the older HTTP:
npx @modelcontextprotocol/inspector@latest -e DUMMY=1 --url http://localhost:8003/mcp --transport http
# If you want to test the deprecated SSE:
npx @modelcontextprotocol/inspector@latest -e DUMMY=1 --url http://localhost:8003/sse --transport sse仅运行 STDIO 服务器
# If venv is ".venv", change to .\.venv\Scripts\python.exe
npx @modelcontextprotocol/inspector python converter_stdio_server.pyUI 默认在 localhost:5173 上运行。
如需更改 UI 端口:CLIENT_PORT=8080 npx @modelcontextprotocol/inspector --url http://localhost:8003/mcp --transport http
如有需要,添加标头:--header "Authorization: Bearer TOKEN"。
提示词与资源的 JSON-RPC 示例
列出所有提示词
curl -s -X POST <SERVER_URL> \
-H "Content-Type: application/json" \
-d '{"jsonrpc":"2.0","method":"prompts/list","params":{},"id":1}'⸻
获取特定提示词
curl -s -X POST <SERVER_URL> \
-H "Content-Type: application/json" \
-d '{"jsonrpc":"2.0","method":"prompts/get","params":{"name":"summarize"},"id":2}'⸻
使用变量渲染/执行提示词
curl -s -X POST <SERVER_URL> \
-H "Content-Type: application/json" \
-d '{"jsonrpc":"2.0","method":"prompts/render","params":{"name":"summarize","variables":{"text":"This is the content to summarize","tone":"neutral"}},"id":3}'列出可用资源
curl -s -X POST <SERVER_URL> \
-H "Content-Type: application/json" \
-d '{"jsonrpc":"2.0","method":"resources/list","params":{},"id":4}'通过 URI 读取资源
curl -s -X POST <SERVER_URL> \
-H "Content-Type: application/json" \
-d '{"jsonrpc":"2.0","method":"resources/read","params":{"uri":"file:///data/report.pdf"},"id":5}'搜索资源(如果支持)
curl -s -X POST <SERVER_URL> \
-H "Content-Type: application/json" \
-d '{"jsonrpc":"2.0","method":"resources/search","params":{"query":"error OR exception","limit":50},"id":6}'⸻
错误处理
解析错误 (-32700)
无效请求 (-32600)
方法未找到 (-32601)
无效参数 (-32602)
内部错误 (-32603)
注意事项
macOS/Linux (bash/zsh) • 上述示例可直接使用。
# Windows PowerShell
curl -Method POST <SERVER_URL> ` -Headers @{ "Content-Type"="application/json" }`
-Body '{"jsonrpc":"2.0","method":"prompts/list","params":{},"id":1}'Windows CMD
curl -s -X POST <SERVER_URL> -H "Content-Type: application/json" -d "{\"jsonrpc\":\"2.0\",\"method\":\"prompts/list\",\"params\":{},\"id\":1}"Tool Schema Changelog
Recent tool additions, removals, and schema changes observed during successful MCP inspections.
Loading tool schema changelog…
This server cannot be deployed
Maintenance
Related MCP Connectors
Unofficial OpenWeather MCP — wraps the OpenWeatherMap API (openweathermap.org)
Remote MCP for JSON repair, hashing, formatting, IDs, dates, units, and other small tools.
Baking unit conversion (cups/grams/temp/recipe scaling). convert_amount, convert_temperature
60+ units, live FX, timezones, and date arithmetic for AI agents.
Related MCP Servers
- AlicenseAqualityDmaintenanceMCP server for converting cooking measurements (volume, weight, temperature) between common units like ml, cup, g, oz, and Celsius/Fahrenheit.31MIT
- AlicenseAqualityDmaintenanceAn MCP server that provides deterministic unit conversions backed by Pint, enabling exact unit conversions, compatibility checks, and parsing of quantities for LLMs.5MIT
- FlicenseNot gradedqualityDmaintenanceProvides a set of everyday utility tools via MCP, including greetings, calculations, time, UUID generation, secure passwords, BMI/age calculation, and unit conversion.-
- AlicenseNot gradedqualityDmaintenanceA deterministic unit conversion MCP server with fixed tables, offering tools to convert length, mass, temperature, and volume, list units, and compare quantities. Designed for judge-friendly schema clarity and no network I/O.MIT