PR #7 ([Doc] Use registered callable names in examples/README.md) を手で再現するための cURL 手順メモ。学習用ノート。
前提
0. サーバー起動
別ターミナルで:
$ git checkout fix_tool_name_in_http_server_example # PR #7 を試すとき
# or
$ git checkout main # 修正前の挙動を見るとき (README は変わらない、登録名は同じ)
$ bundle exec ruby examples/http_server.rb
Listening on http://127.0.0.1:9292 が出れば OK。
1. session 初期化 → SID を取得
$ ACCEPT='Accept: application/json, text/event-stream'
$ SID=$(curl -sS -i http://localhost:9292 \
-H "$ACCEPT" \
--json '{"jsonrpc":"2.0","method":"initialize","id":1,"params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"test","version":"1.0"}}}' \
| grep -i '^mcp-session-id:' | sed -E 's/^[^:]+: *//' | tr -d '\r')
$ echo "SID=$SID"
2. tools/list で登録名を確認 (PR の根拠)
$ curl -sS http://localhost:9292 \
-H "$ACCEPT" \
-H "Mcp-Session-Id: $SID" \
--json '{"jsonrpc":"2.0","method":"tools/list","id":2}'
期待:
data: {"jsonrpc":"2.0","id":2,"result":{"tools":[{"name":"example_tool",...},{"name":"echo",...}]}}
→ 登録されている tool 名は example_tool (クラス名 ExampleTool ではない)。
3. 旧 README の payload を再現 → Tool not found
$ curl -sS http://localhost:9292 \
-H "$ACCEPT" \
-H "Mcp-Session-Id: $SID" \
--json '{"jsonrpc":"2.0","method":"tools/call","id":3,"params":{"name":"ExampleTool","arguments":{"a":5,"b":3}}}'
期待:
data: {"jsonrpc":"2.0","id":3,"error":{"code":-32602,"message":"Invalid params","data":"Tool not found: ExampleTool"}}
4. 修正後の payload → 成功
$ curl -sS http://localhost:9292 \
-H "$ACCEPT" \
-H "Mcp-Session-Id: $SID" \
--json '{"jsonrpc":"2.0","method":"tools/call","id":4,"params":{"name":"example_tool","arguments":{"a":5,"b":3}}}'
期待:
data: {"jsonrpc":"2.0","id":4,"result":{"content":[{"type":"text","text":"The sum of 5 and 3 is 8"}],"isError":false}}
5. prompt 側の確認
$ curl -sS http://localhost:9292 \
-H "$ACCEPT" \
-H "Mcp-Session-Id: $SID" \
--json '{"jsonrpc":"2.0","method":"prompts/list","id":5}'
期待: "name":"example_prompt" (クラス名 ExamplePrompt ではない)。
ハマりどころメモ
- Accept header 必須: 上記のとおり。README にも書かれていない別件 doc バグ。
- SID は 1 セッション 1 個: 上で取った SID を 2〜5 で使い回す。新シェルに移ったら再取得。
- レスポンスは SSE 形式:
data: prefix を剥がしてから jq / python -m json.tool に渡す。
set -e + パイプ注意: SSE 出力を json parser にそのまま渡すと parse 失敗で全部止まる。raw のまま見るのが楽。
参考
PR #7 (
[Doc] Use registered callable names in examples/README.md) を手で再現するための cURL 手順メモ。学習用ノート。前提
fix_tool_name_in_http_server_example(PR [Doc] Use registered callable names in examples/README.md #7) を試すか、mainを試すかは適宜切り替え--jsonだけだとAccept: application/jsonしか送られず、サーバーがAccept: application/json, text/event-streamを要求するため HTTP 406 で弾かれる (lib/mcp/server/transports/streamable_http_transport.rb:533)。これは PR [Doc] Use registered callable names in examples/README.md #7 と別件の doc バグdata: {...}prefix 付き)0. サーバー起動
別ターミナルで:
Listening on http://127.0.0.1:9292が出れば OK。1. session 初期化 → SID を取得
2.
tools/listで登録名を確認 (PR の根拠)期待:
→ 登録されている tool 名は
example_tool(クラス名ExampleToolではない)。3. 旧 README の payload を再現 →
Tool not found期待:
4. 修正後の payload → 成功
期待:
5. prompt 側の確認
期待:
"name":"example_prompt"(クラス名ExamplePromptではない)。ハマりどころメモ
data:prefix を剥がしてからjq/python -m json.toolに渡す。set -e+ パイプ注意: SSE 出力を json parser にそのまま渡すと parse 失敗で全部止まる。raw のまま見るのが楽。参考
lib/mcp/tool.rb#name_value/lib/mcp/prompt.rb#name_value/lib/mcp/string_utils.rb#handle_from_class_name