1
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

MCPの公式チュートリアルで新時代の開発を見た

Posted at

着手する前に、最後まで読んだ。
新時代のチュートリアルを見た感覚だ。
コード片は一切なく、LLMに与えるべき情報と守るべきルールが記載されている。
ページ全体をコピーするボタンすらついている。
image.png

ChatGPTと相談

何のMCPサーバーを作ろうか相談しようとした。
MCPサーバーを作ろうと思うんだって言ったら、いいね!MCPサーバー(Minecraft Plugin Server)のチュートリアル用なら、...って返されたので慌てて止めた。
その後、ちょっと噛み合っていない感じなので、チュートリアルに従ってコンテキストを与えることにした。
以下のページのURLを与えて、見てもらう:

要約してくれて、私が認識している情報との確認もできた。

チュートリアルでまずやってみたいのが、外部APIから情報を取得してAIに接続するためのMCPサーバー。なので、定番の天気APIでMCPサーバーを作成することにした。

CursorでMCPサーバー作成にトライ

まず、GitHub上でリポジトリを作り、Cursorでそのリポジトリを開いた。
image.png
これ使ってみる。
Webを選んで、上記2ページのURLを与えて...あ、そのままSendしてしまった。
(後記:多分ここでWebを選んだのは間違いだった。Web検索をしてしまったようだ)
特に何が作りたいとか言っていないのに、MCPを使用して天気予報データを取得・表示するWebアプリケーションを作り始めてしまった。
改めて、

Open-Meteo APIに接続して、ある地点の天気情報を取得するMCPサーバーを作りたい

image.png

あっという間にできた。

でも、一発では動かなかった。
importの情報が古かったり、dependencyの書き方が間違っていたりした。一つ一つ修正して行って、動き始めた🎉
image.png

image.png

次はクライアントか👀
修正を何周かした。

image.png

怖い。

途中でクライアントが動いたのに、Popout Terminalをクリックしたら失敗したと認識されて明後日の方向に修正し始めたので途中で止めた。巻き戻し過程でファイルが一つ消えた...
image.png
image.png
image.png
image.png

動いた🎉
image.png

PRを作ってもらった

PRを作る時、ブランチの切り方とか間違えまくりでクレジットを多く消費してくださるので、Git操作は人間がやったほうが良いかも。。。概要欄を丁寧に書いてくれるのはとてもありがたいので、その部分では今後もあやかりたい。

コードを見る

ディレクトリ構造

app/
├─ __init__.py
├─ client.py
├─ main.py
├─ mcp_server.py
├─ server.py
└─ weather_service.py

クライアント起動のコマンドから辿る

python -m app.client

client.pyはこちら:
https://github.com/kaisumi/weather-mcp/pull/1/files#diff-fad8b102fa5b64491ee38b22f7558f96b7bed87f1fa549463a52a07eb9403058

if __name__ == "__main__":
    asyncio.run(main()) 
  • その.pyファイルが「直接実行」されたときだけ asyncio.run(main()) を動かす
  • モジュール(import)されたときは動かない
async def main():
    """メイン関数"""
    city = input("天気情報を取得する都市名を入力してください: ")
    await get_weather(city)
  1. input() でユーザーに都市名を入力させる(同期処理)
  2. 入力した都市名を get_weather(city) に渡して実行(非同期処理だから await)

以下はget_weatherの一部:

async def get_weather(city: str):
    """指定した都市の天気情報を取得する"""
    try:
        # HTTPクライアントを初期化
        base_url = os.getenv("MCP_BASE_URL", "http://localhost:8000")
        async with httpx.AsyncClient(base_url=base_url) as client:
            # 天気情報を取得
            response = await client.post(
                "/mcp",
                json={
                    "method": "call_tool",  # MCPの決まり文句
                    "params": {
                        "name": "get_weather",  # 実行したいTool名
                        "arguments": {"city": city}  # Toolに渡す引数
                    }
                }
            )
            response.raise_for_status()
            data = response.json()

server側を見る

app/server.py

from .mcp_server import mcp
...
@app.post("/mcp")
async def handle_mcp_request(request: Request):
    """MCPリクエストを処理するエンドポイント"""
    try:
        # リクエストボディを取得
        body = await request.json()

        # MCPリクエストを処理
        if body["method"] == "call_tool":
            tool_name = body["params"]["name"]
            arguments = body["params"]["arguments"]

            if tool_name == "get_weather":
                result = await mcp.call_tool(tool_name, arguments) # 核心
                return result
            else:
                return {
                    "success": False,
                    "error": f"未知のツール: {tool_name}"
                }
        else:
            return {
                "success": False,
                "error": f"未知のメソッド: {body['method']}"
            }

    except Exception as e:
        return {
            "success": False,
            "error": f"リクエストの処理中にエラーが発生しました: {str(e)}"
        }

app/mcp_server.py

...
from app.weather_service import WeatherService
...
@mcp.tool()
async def get_weather(city: str) -> dict:
    """指定した都市の天気情報を返すTool"""
    if not city:
        return {"error": "都市名が指定されていません"}

    try:
        result = await weather_service.get_weather_for_city(city) # ここでAPIを叩くのかな
        return result
    except Exception as e:
        return {
            "error": str(e)
        } 

※@mcp.tool() は MCP SDK の「この関数を Tool(=LLMが実行できるAPI)として登録するよ!」という宣言(デコレーター)。

app/weather_service.pyより、get_weather_for_cityの一部:

    async def get_weather_for_city(self, city: str) -> Dict[str, Any]:
        """都市名から天気情報を取得する"""
        try:
            # 都市名から緯度経度を取得
            location = await self.get_coordinates(city)

            # 緯度経度から天気情報を取得
            weather_data = await self.get_weather(location["latitude"], location["longitude"])

            # 現在の天気情報を整形
            current = weather_data["current"]
            current_condition = self.get_weather_condition(current["weather_code"])
            ...
            
            # 結果を整形
            result = {
                "location": {
                    "name": location["name"],
                    "country": location["country"],
                    "latitude": location["latitude"],
                    "longitude": location["longitude"],
                    "timezone": weather_data["timezone"]
                },
                "current": {
                    "temperature": current["temperature_2m"],
                    "feels_like": current["apparent_temperature"],
                    "humidity": current["relative_humidity_2m"],
                    "wind_speed": current["wind_speed_10m"],
                    "wind_direction": current["wind_direction_10m"],
                    "precipitation": current["precipitation"],
                    "condition": current_condition,
                    "weather_code": current["weather_code"]
                },
                "forecast": daily_forecast
            }

            return result

緯度経度取得部分:

    GEOCODING_URL = "https://geocoding-api.open-meteo.com/v1/search"
    WEATHER_URL = "https://api.open-meteo.com/v1/forecast"

    async def get_coordinates(self, city: str) -> Dict[str, Any]:
        """都市名から緯度経度を取得する"""
        async with aiohttp.ClientSession() as session:
            params = {
                "name": city,
                "count": 1,
                "language": "ja",
                "format": "json"
            }
            async with session.get(self.GEOCODING_URL, params=params) as response:
                if response.status != 200:
                    raise Exception(f"Geocoding API error: {response.status}")

                data = await response.json()
                if not data.get("results"):
                    raise Exception(f"City '{city}' not found")

                return data["results"][0]

geocoding-apiというAPIを叩いていたのね。それで都市名(しかも日本語)から取得できていたのか👀

緯度経度から天気を取得する部分:

    async def get_weather(self, latitude: float, longitude: float) -> Dict[str, Any]:
        """緯度経度から天気情報を取得する"""
        async with aiohttp.ClientSession() as session:
            params = {
                "latitude": latitude,
                "longitude": longitude,
                "current": ["temperature_2m", "relative_humidity_2m", "apparent_temperature", 
                           "precipitation", "weather_code", "wind_speed_10m", "wind_direction_10m"],
                "daily": ["temperature_2m_max", "temperature_2m_min", "precipitation_sum", 
                         "weather_code", "sunrise", "sunset"],
                "timezone": "auto"
            }
            async with session.get(self.WEATHER_URL, params=params) as response:
                if response.status != 200:
                    raise Exception(f"Weather API error: {response.status}")

                return await response.json()

コードを読み進めてみて思ったこと

APIを叩いて、JSONでクライアントに返している。決まった形のJSONで返すんだったら、APIと変わらない気がする。MCPの良さとは?

MCPの強み

ChatGPTに聞いてみた。

  • API仕様を自動的にLLMが取得できるようにしたこと

  • 外部実行を標準化したこと

  • Contextでユーザーごとの情報を自動注入できること

  • MCP対応クライアントは統一仕様で動けること

本当かな...

API仕様を自動的にLLMが取得できるようにしたこと

これは公式で明記されていた。

Resources are a core primitive in the Model Context Protocol (MCP) that allow servers to expose data and content that can be read by clients and used as context for LLM interactions.

つまり、API仕様をLLM側から取りに行く場所が決まっているので、LLMが使い方に困らないということ。
今回作ったMCPサーバーにはresourcesがなかったはず。
Cursorに直してもらっているけど、どつぼにハマったので自分で直したい。自分で直したいけどドキュメントがLLM前提に作られているような。

ここ見たらいいかな。

感想

Cursorで頑張ってみたけど、実装ミスに振り回されて開発体験があんまり良くない...他のエージェントを試してみようかな。
image.png

今日MCPを頑張ってみてわかったこと

LLMは、そのままではAPIを叩けない。コンテキストとしてAPI仕様を入れる必要がある。だからMCPとして公開すれば、MCP Clientを搭載したLLMがそれらを使える利点がある。Resourcesを使えば統一した方法で仕様を入手できるのが目玉なのかと思ったけど、サポート表を見るとこの機能を使っているツールは少ない様子。つまり、ResourcesがなくてもLLMに使わせられるから別にいらないってことなのかな🤔でも、Resourcesがいらないなら、既存のAPIの仕様を事前に学習させておけばいい、またはリストを用意して見に行かせればいいよね。

image.png

1
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?