🗺️

Agentic-AI の MCP について調べて実践してみた

に公開

目的

よく聞くMCPについて、自分なりに調べてまとめてみる。
また、簡単なハンズオン手順を試してみる。

MCPとは

Model Context Protocolの略で、GeminiやClaudeなどのLLMが外部のツールやアプリケーション、データと連携するためのオープンな標準規格。
サーバーとクライアントの構成で成り立っており、MCPサーバーがどのようにツールやアプリケーション、データを公開するか、またそれらをMCPクライアントがどのように利用するかが定義されている。

利用パターン概要

  1. MCPクライアントとして、公開されているMCPサーバーを利用する

  2. MCPサーバーを立てて、ツールなどをMCPクライアント向けに公開する

ハンズオンにトライ

  • 手順(GoogleMapAPIの箇所):https://google.github.io/adk-docs/tools/mcp-tools/
  • 前提:
    • Set up ADK: Follow the standard ADK setup instructions in the quickstart.
      Install/update Python/Java: MCP requires Python version of 3.9 or higher for Python or Java 17 or higher.
    • Setup Node.js and npx: (Python only) Many community MCP servers are distributed as Node.js packages and run using npx.
    • Install Node.js (which includes npx) if you haven't already. For details, see https://nodejs.org/en.
    • Verify Installations: (Python only) Confirm adk and npx are in your PATH within the activated virtual environment:
    • 作業環境の作成は、こちらも参照して必要な作業を実施:https://google.github.io/adk-docs/get-started/quickstart/
  1. GoogleMapAPIキーを発行する。
    下記リンク先より発行可能。
    https://developers.google.com/maps/documentation/javascript/get-api-key#create-api-keys

  2. GoogleMapAPIを環境変数に設定する。
    ※YOUR_GOOGLE_MAPS_API_KEYはAPIキーに置き換える。

$env:GOOGLE_MAPS_API_KEY="YOUR_GOOGLE_MAPS_API_KEY"
  1. UIを起動
adk web
  1. メッセージを送ってみる。
    →エラーで失敗してしまう。

    調べたところによると、Windowsとstdioの組み合わせは不具合が多いとのこと。
    今回もこれが原因かは不明だが、別の方法でリトライ。
    参考:https://github.com/google/adk-python/issues/2979?utm_source=chatgpt.com

MCPを別で起動してリトライ

MCPサーバーの起動を別ターミナルで実施し、HTTPでMCPクライアントから接続をトライ

  1. 手順のスクリプトに下記を追加
from google.adk.tools.mcp_tool.mcp_session_manager import StreamableHTTPConnectionParams
  1. 手順のスクリプトの一部を修正
    修正前:
    tools=[
        MCPToolset(
            connection_params=StdioConnectionParams(
                server_params = StdioServerParameters(
                    command='npx',
                    args=[
                        "-y",
                        "@modelcontextprotocol/server-google-maps",
                    ],
                    # Pass the API key as an environment variable to the npx process
                    # This is how the MCP server for Google Maps expects the key.
                    env={
                        "GOOGLE_MAPS_API_KEY": google_maps_api_key
                    }
                ),
            ),
            # You can filter for specific Maps tools if needed:
            # tool_filter=['get_directions', 'find_place_by_id']
        )
    ],

修正後:

    tools=[
        MCPToolset(
            connection_params=StreamableHTTPConnectionParams(
            url="http://127.0.0.1:3000/mcp",
            )
        )
  1. 別ターミナルでMCPサーバーを実行
    ※YOUR_GOOGLE_MAPS_API_KEYはAPIキーに置き換える。
cmd /c npx @cablate/mcp-google-map --port 3000 --apikey "YOUR_GOOGLE_MAPS_API_KEY"

4.再度UIを起動

adk web
  1. メッセージにうまくレスポンスが返ってきた
    また、指定したツールが利用されていることがわかる。

Discussion