Unit Converter MCP Server
Unit Converter API + MCP Tutorial
baut die FastAPI-App, umschließt sie mit FastMCP, bindet MCP HTTP/SSE-Endpunkte ein, registriert Ressourcen und Prompts und startet uvicorn.
requirements.txt – Python-Abhängigkeiten.
Voraussetzungen
Python 3.10+ (getestet mit 3.12).
Virtuelle Umgebung.
npm-Inspector unten.
⸻
Related MCP server: Local Utilities MCP Server
Einrichtung aus diesem Ordner
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-Server ausführen
# start the server
python converter_streamable_http_server.py
# or
python -m converter_streamable_http_server
Sie sehen:
Swagger UI: http://localhost:8003/docs
ReDoc: http://localhost:8003/redoc
MCP-Endpunkte, die von FastMCP bereitgestellt werden:
streamable-http: http://localhost:8003/mcp
⸻
HTTP-Endpunkte testen (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"Jeder Endpunkt gibt JSON zurück wie:
{ "result": , "operation": "..." } oder { "error": "..." } bei ungültiger Eingabe.
Header & Authentifizierung (für alle gleich)
JSON-Content-Type hinzufügen (und optional Ihr Auth-Token)
-H "Content-Type: application/json"
-H "Authorization: Bearer "
Unser Server erfordert noch keine Authentifizierung, wir können den Authorization-Header weglassen.
Verwendung mit MCP (VS Code-Beispiel)
Starten Sie den Server wie oben beschrieben.
Verweisen Sie Ihren MCP-Client auf den Prozess.
// Example VS Code .vscode/mcp.json entry:
{
"servers": {
"UnitConverter": {
"command": "python",
"args": ["converter_api_tutorial.py"]
}
}
}Listen Sie vom MCP-Client aus die Artefakte auf. Sie sollten sehen:
Tools: celsius_to_fahrenheit, fahrenheit_to_celsius, kilometers_to_miles, miles_to_kilometers
Ressourcen: resource://unit_reference, resource://troubleshooting_guide
Prompts: explain_conversion, api_usage
⸻
Mit dem npm MCP Inspector untersuchen
Erkunden Sie alles (Tools, Ressourcen, Prompts) in einem Browser.
während der Server bereits auf http://localhost:8003 läuft
# 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 sseNur den STDIO-Server ausführen
# If venv is ".venv", change to .\.venv\Scripts\python.exe
npx @modelcontextprotocol/inspector python converter_stdio_server.pyDie UI läuft standardmäßig auf localhost:5173.
Ändern Sie bei Bedarf den UI-Port: CLIENT_PORT=8080 npx @modelcontextprotocol/inspector --url http://localhost:8003/mcp --transport http
Fügen Sie bei Bedarf Header hinzu: --header "Authorization: Bearer TOKEN".
JSON-RPC-Beispiele für Prompts & Ressourcen
Alle Prompts auflisten
curl -s -X POST <SERVER_URL> \
-H "Content-Type: application/json" \
-d '{"jsonrpc":"2.0","method":"prompts/list","params":{},"id":1}'⸻
Einen bestimmten Prompt abrufen
curl -s -X POST <SERVER_URL> \
-H "Content-Type: application/json" \
-d '{"jsonrpc":"2.0","method":"prompts/get","params":{"name":"summarize"},"id":2}'⸻
Einen Prompt mit Variablen rendern/ausführen
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}'Verfügbare Ressourcen auflisten
curl -s -X POST <SERVER_URL> \
-H "Content-Type: application/json" \
-d '{"jsonrpc":"2.0","method":"resources/list","params":{},"id":4}'Eine Ressource per URI lesen
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}'Ressourcen durchsuchen (falls unterstützt)
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}'⸻
Fehlerbehandlung
Parse-Fehler (-32700)
Ungültige Anfrage (-32600)
Methode nicht gefunden (-32601)
Ungültige Parameter (-32602)
Interner Fehler (-32603)
Hinweise
macOS/Linux (bash/zsh) • Die obigen Beispiele funktionieren unverändert.
# 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}"This server cannot be deployed
Maintenance
Related MCP Connectors
OpenWeather MCP — wraps the OpenWeatherMap API (openweathermap.org)
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
- FlicenseNot gradedqualityDmaintenanceEnables 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-
- AlicenseNot gradedqualityDmaintenanceProvides essential utility tools for text processing, file operations, hashing, temperature conversion, password generation, and date/time operations through a comprehensive MCP interface.MIT
- 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