Skip to main content
Glama
Unit Converter MCP Server

Unit Converter MCP Server

by Robbo-lab

Tutorial de API + MCP de Conversor de Unidades

  • construye la aplicación FastAPI, la envuelve con FastMCP, monta los endpoints HTTP/SSE de MCP, registra recursos y prompts, e inicia uvicorn.

  • requirements.txt – Dependencias de Python.

Requisitos previos

  • Python 3.10+ (probado con 3.12).

  • Entorno virtual.

  • Inspector de npm a continuación.

⸻

Related MCP server: mcp-units

Configuración desde esta carpeta

python -m venv .venv

# Mac or Gitbash
source .venv/bin/activate

# Windows powershell:
.venv\Scripts\activate
python -m pip install -r requirements.txt

⸻

Ejecutar el servidor HTTP + MCP

# start the server
python converter_streamable_http_server.py

# or
python -m converter_streamable_http_server

Verás:

Endpoints MCP servidos por FastMCP:

⸻

Probar los endpoints 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"

Cada endpoint devuelve un JSON como:

  • { "result": <número>, "operation": "..." } o { "error": "..." } para entradas no válidas.

Cabeceras y Autenticación (común a todos)

Añadir tipo de contenido JSON (y opcionalmente tu token de autenticación)

-H "Content-Type: application/json"
-H "Authorization: Bearer "

Nuestro servidor aún no requiere autenticación, podemos omitir la cabecera Authorization.

Uso con MCP (Ejemplo de VS Code)

  1. Inicia el servidor como se indicó anteriormente.

  2. Apunta tu cliente MCP al proceso.

// Example VS Code .vscode/mcp.json entry:
{
  "servers": {
    "UnitConverter": {
      "command": "python",
      "args": ["converter_api_tutorial.py"]
    }
  }
}
  1. Desde el cliente MCP, lista los artefactos. Deberías ver:

    • Herramientas: celsius_to_fahrenheit, fahrenheit_to_celsius, kilometers_to_miles, miles_to_kilometers

    • Recursos: resource://unit_reference, resource://troubleshooting_guide

    • Prompts: explain_conversion, api_usage

⸻

Inspeccionar con el Inspector MCP de npm

  • explora todo (herramientas, recursos, prompts) en un navegador.

  • con el servidor ya ejecutándose en 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

Para ejecutar solo el servidor STDIO

# If venv is ".venv", change to .\.venv\Scripts\python.exe
npx @modelcontextprotocol/inspector python converter_stdio_server.py
  • La interfaz de usuario se ejecuta en localhost:5173 por defecto.

  • Cambia el puerto de la interfaz si es necesario: CLIENT_PORT=8080 npx @modelcontextprotocol/inspector --url http://localhost:8003/mcp --transport http

  • Añade cabeceras si es necesario: --header "Authorization: Bearer TOKEN".

Ejemplos de JSON-RPC para Prompts y Recursos

  1. Listar todos los prompts

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

⸻

  1. Obtener un prompt específico

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

⸻

  1. Renderizar/ejecutar un prompt con variables

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. Listar recursos disponibles

curl -s -X POST <SERVER_URL> \
-H "Content-Type: application/json" \
-d '{"jsonrpc":"2.0","method":"resources/list","params":{},"id":4}'
  1. Leer un recurso por 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. Buscar recursos (si es compatible)

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

⸻

Manejo de errores

  • Error de análisis (-32700)

  • Solicitud no válida (-32600)

  • Método no encontrado (-32601)

  • Parámetros no válidos (-32602)

  • Error interno (-32603)

Notas

macOS/Linux (bash/zsh) • Los ejemplos anteriores funcionarán tal cual.

# 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…

    Related MCP Connectors

    Related MCP Servers

    • 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
    • F
      license
      Not graded
      quality
      D
      maintenance
      Provides a set of everyday utility tools via MCP, including greetings, calculations, time, UUID generation, secure passwords, BMI/age calculation, and unit conversion.
      -
    • A
      license
      Not graded
      quality
      D
      maintenance
      A 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