Closed8

MCPクライアント、サーバー間の通信内容を見てみる

syuparnsyuparn
  • 「サーバー」と銘打っているがHTTPサーバーである必要はなく、公式チュートリアルもstdioを使用している
  • stdioを使用する場合のやり取りの流れ
    • MCPクライアントがMCPサーバーのプロセスを起動
    • MCPサーバーへのリクエスト = プロセスへのstdin
    • MCPサーバーからのレスポンス = プロセスからのstdout
syuparnsyuparn

検証

syuparnsyuparn
  • サーバー本実装index.js への入出力をログファイル出力するラッパーを作成
  • VSCodeにはこのラッパーをMCPサーバーとして登録

ラッパーのシェルスクリプト

build/wrapper.sh
#!/bin/bash

# teeコマンドでstdin, stdoutと同じ内容をログファイルにも保存
tee /home/syuparn/weather/build/logs/req_$(date +"%Y-%m-%d").log | node /home/syuparn/weather/build/index.js | tee /home/syuparn/weather/build/logs/res_$(date +"%Y-%m-%d").log

VSCodeの設定

settings.json
{
  // ...
  "mcp": {
    "inputs": [],
    "servers": {
      "mcp-server-weather": {
        "command": "bash",
        "args": [
          "/home/syuparn/weather/build/wrapper.sh"
        ],
        "env": {}
      }
    }
  }
}
syuparnsyuparn
  • Copilot Agentのチャットでアメリカの天気について質問をし、AgentにMCPサーバーを利用してもらう

Copilot Agentにニューヨークの天気について質問。MCPサーバーのツールget-forecastが利用され、1週間の天気予報が返ってくる

syuparnsyuparn

結果

リクエスト

build/logs/req_2025-04-15.log
{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{"roots":{"listChanged":true}},"clientInfo":{"name":"Visual Studio Code","version":"1.99.2"}}}
{"method":"notifications/initialized","jsonrpc":"2.0"}
{"jsonrpc":"2.0","id":2,"method":"tools/list","params":{}}
{"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"get-forecast","arguments":{"latitude":40.7128,"longitude":-74.006}}}

レスポンス

build/logs/res_2025-04-15.log
{"result":{"protocolVersion":"2024-11-05","capabilities":{"tools":{}},"serverInfo":{"name":"weather","version":"1.0.0","capabilities":{"resources":{},"tools":{}}}},"jsonrpc":"2.0","id":1}
{"result":{"tools":[{"name":"get-alerts","description":"Get weather alerts for a state","inputSchema":{"type":"object","properties":{"state":{"type":"string","minLength":2,"maxLength":2,"description":"Two-letter state code (e.g. CA, NY)"}},"required":["state"],"additionalProperties":false,"$schema":"http://json-schema.org/draft-07/schema#"}},{"name":"get-forecast","description":"Get weather forecast for a location","inputSchema":{"type":"object","properties":{"latitude":{"type":"number","minimum":-90,"maximum":90,"description":"Latitude of the location"},"longitude":{"type":"number","minimum":-180,"maximum":180,"description":"Longitude of the location"}},"required":["latitude","longitude"],"additionalProperties":false,"$schema":"http://json-schema.org/draft-07/schema#"}}]},"jsonrpc":"2.0","id":2}
{"result":{"content":[{"type":"text","text":"Forecast for 40.7128, -74.006:\n\nTonight:\nTemperature: 52°F\nWind: 2 to 6 mph S\nRain Showers Likely\n---\nTuesday:\nTemperature: 63°F\nWind: 2 to 22 mph W\nPartly Sunny then Chance Showers And Thunderstorms\n---\nTuesday Night:\nTemperature: 44°F\nWind: 16 to 21 mph W\nChance Showers And Thunderstorms then Partly Cloudy\n---\nWednesday:\nTemperature: 51°F\nWind: 16 to 21 mph W\nPartly Sunny\n---\nWednesday Night:\nTemperature: 41°F\nWind: 12 to 20 mph NW\nPartly Cloudy\n---\nThursday:\nTemperature: 56°F\nWind: 14 mph NW\nSunny\n---\nThursday Night:\nTemperature: 43°F\nWind: 3 to 13 mph NW\nClear\n---\nFriday:\nTemperature: 60°F\nWind: 3 to 15 mph SW\nMostly Sunny\n---\nFriday Night:\nTemperature: 55°F\nWind: 15 mph S\nSlight Chance Rain Showers\n---\nSaturday:\nTemperature: 71°F\nWind: 15 mph SW\nChance Rain Showers\n---\nSaturday Night:\nTemperature: 53°F\nWind: 14 mph W\nChance Rain Showers\n---\nSunday:\nTemperature: 62°F\nWind: 13 mph NW\nMostly Sunny\n---\nSunday Night:\nTemperature: 49°F\nWind: 8 to 13 mph NW\nPartly Cloudy\n---\nMonday:\nTemperature: 59°F\nWind: 8 to 12 mph W\nPartly Sunny then Slight Chance Rain Showers\n---"}]},"jsonrpc":"2.0","id":3}
syuparnsyuparn
  • initialize request/responseは initialize というメソッドで行われている
  • クライアントはツールを利用する前に、どんなツールがあるか/どんなリクエスト形式で送ればよいかを tools/list メソッドを利用して解釈している
  • そのあと実際に利用したい天気の取得を tools/call で行っている
このスクラップは5ヶ月前にクローズされました