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