Closed6

MCPサーバーとJSON-RPCで会話してみる

ojapiojapi

tmuxの新しいセッションを perplexity という名前でデタッチモード(バックグラウンド)で作成。
-i オプションで対話モードをONにする。

tmux new-session -d -s perplexity 'docker run -i --rm -e PERPLEXITY_API_KEY=your_actual_api_key 
mcp/perplexity-ask'
  • PERPLEXITY_API_KEY はperplexityの管理画面から発行したSonarのAPIキー
ojapiojapi

イニシャライズのリクエストを送信

tmux send-keys -t perplexity \
  '{"method": "initialize", "params": {"protocolVersion": "2024-11-05", "capabilities": {"roots": {"listChanged": true}}, "clientInfo": {"name": "mcp", "version": "0.1.0"}}, "jsonrpc": "2.0", "id": 0}' \
  C-m

以下のコマンドで出力を確認

tmux capture-pane -t perplexity -p

以下出力されていることを確認

{
    "result": {
        "protocolVersion": "2024-11-05",
        "capabilities": {
            "tools": {}
        },
        "serverInfo": {
            "name": "example-servers/perplexity-ask",
            "version": "0.1.0"
        }
    },
    "jsonrpc": "2.0",
    "id": 0
}
ojapiojapi

"method": "tools/list" でMCPサーバーが提供するツールの内容や入力スキーマを確認

tmux send-keys -t perplexity \
  '{"jsonrpc": "2.0", "id": 1, "method": "tools/list"}' \
  C-m
{
    "result": {
        "tools": [
            {
                "name": "perplexity_ask",
                "description": "Engages in a conversation using the Sonar API. Accepts an array of messages (each with a role and content) and returns a ask completion response from the Perplexity model.",
                "inputSchema": {
                    "type": "object",
                    "properties": {
                        "messages": {
                            "type": "array",
                            "items": {
                                "type": "object",
                                "properties": {
                                    "role": {
                                        "type": "string",
                                        "description": "Role of the message (e.g., system, user, assistant)"
                                    },
                                    "content": {
                                        "type": "string",
                                        "description": "The content of the message"
                                    }
                                },
                                "required": [
                                    "role",
                                    "content"
                                ]
                            },
                            "description": "Array of conversation messages"
                        }
                    },
                    "required": [
                        "messages"
                    ]
                }
            }
        ]
    },
    "jsonrpc": "2.0",
    "id": 1
}
ojapiojapi

"method": "tools/call"perplexity_ask を実行する

tmux send-keys -t perplexity \
  '{ "method": "tools/call", "params": { "name": "perplexity_ask", "arguments": { "messages": [{"role": "user", "content": "今日の代々木の天気は?"}] } }, "jsonrpc": "2.0", "id": 3 }' \
  C-m
{
    "result": {
        "content": [
            {
                "type": "text",
                "text": "今日の代々木の天気は晴れです。最高気温は13℃、最低気温は4℃の予報となっています[1][5]。\n\n午前中は晴れ、午後も晴れの見込みで、降水確率は低くなっています。風は北西の風が2-3m/s程度吹く予報です[1][5]。\n\n全体的に穏やかな天気で、過ごしやすい一日になりそうです。\n\nCitations:\n[1] https://weathernews.jp/onebox/tenki/tokyo/13113/\n[2] https://weathernews.jp/onebox/35.664116/139.679375/q=%E4%BB%A3%E3%80%85%E6%9C%A8%E4%B8%8A%E5%8E%9F%EF%BC%88%E6%9D%B1%E4%BA%AC%E9%83%BD%EF%BC%89&v=e8d4aeadf237fc4f7e15a708dfd6908670dd82755d2c64da42a4b208dabf8e35&lang=ja\n[3] https://weather.yahoo.co.jp/weather/jp/13/4410/13113.html\n[4] https://tenki.jp/leisure/3/16/96/34/\n[5] https://tenki.jp/forecast/3/16/4410/13113/10days.html\n[6] https://tenki.jp/leisure/3/16/96/34/3hours.html\n[7] https://www.toshin.com/weather/detail?id=66287\n[8] https://www.toshin.com/weather/detail?id=56744\n[9] https://weathernews.jp/onebox/tenki/spot/park/03/5930876/\n[10] https://www.mapion.co.jp/weather/admi/13/1311331.html\n[11] https://www.toshin.com/weather/detail?id=56745\n[12] https://tenki.jp/past/2025/03/weather/3/16/\n[13] https://traininfo.jreast.co.jp/train_info/line.aspx?gid=1&lineid=yamanoteline\n[14] https://iseshima.keizai.biz/release/391995/\n[15] https://www.data.jma.go.jp/stats/etrn/index.php?prec_no=56&block_no=0560\n"
            }
        ],
        "isError": false
    },
    "jsonrpc": "2.0",
    "id": 3
}
このスクラップは5ヶ月前にクローズされました