Skip to main content
Glama
Robbo-lab

Unit Converter MCP Server

by Robbo-lab

单位转换 API + MCP 教程

  • 构建 FastAPI 应用,使用 FastMCP 进行封装,挂载 MCP HTTP/SSE 端点,注册资源和提示词,并启动 uvicorn。

  • requirements.txt – Python 依赖项。

前置要求

  • Python 3.10+(已在 3.12 上测试)。

  • 虚拟环境。

  • 下方的 npm 检查器。

⸻

Related MCP server: Local Utilities MCP Server

从此文件夹设置

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

你将看到:

由 FastMCP 提供的 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 示例)

  1. 按上述方式启动服务器。

  2. 将你的 MCP 客户端指向该进程。

// Example VS Code .vscode/mcp.json entry:
{
  "servers": {
    "UnitConverter": {
      "command": "python",
      "args": ["converter_api_tutorial.py"]
    }
  }
}
  1. 从 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.py
  • UI 默认在 localhost:5173 上运行。

  • 如需更改 UI 端口:CLIENT_PORT=8080 npx @modelcontextprotocol/inspector --url http://localhost:8003/mcp --transport http

  • 如有需要,添加标头:--header "Authorization: Bearer TOKEN"。

提示词与资源的 JSON-RPC 示例

  1. 列出所有提示词

curl -s -X POST <SERVER_URL> \
-H "Content-Type: application/json" \
-d '{"jsonrpc":"2.0","method":"prompts/list","params":{},"id":1}'

⸻

  1. 获取特定提示词

curl -s -X POST <SERVER_URL> \
-H "Content-Type: application/json" \
-d '{"jsonrpc":"2.0","method":"prompts/get","params":{"name":"summarize"},"id":2}'

⸻

  1. 使用变量渲染/执行提示词

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}'
  1. 列出可用资源

curl -s -X POST <SERVER_URL> \
-H "Content-Type: application/json" \
-d '{"jsonrpc":"2.0","method":"resources/list","params":{},"id":4}'
  1. 通过 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}'
  1. 搜索资源(如果支持)

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}"

Related MCP Connectors

Related MCP Servers

  • F
    license
    Not graded
    quality
    D
    maintenance
    Enables temperature conversion from Fahrenheit to Celsius through HTTP requests. Provides a simple MCP tool for temperature unit conversion with AWS Lambda deployment support.
    19 npm
    -
  • A
    license
    Not graded
    quality
    D
    maintenance
    Provides essential utility tools for text processing, file operations, hashing, temperature conversion, password generation, and date/time operations through a comprehensive MCP interface.
    MIT
  • A
    license
    A
    quality
    D
    maintenance
    MCP server for converting cooking measurements (volume, weight, temperature) between common units like ml, cup, g, oz, and Celsius/Fahrenheit.
    3
    1
    MIT
  • A
    license
    A
    quality
    D
    maintenance
    An MCP server that provides deterministic unit conversions backed by Pint, enabling exact unit conversions, compatibility checks, and parsing of quantities for LLMs.
    5
    MIT