-
-
Save kuc-arc-f/ab37495a7128ce7c010843512bd3d8f7 to your computer and use it in GitHub Desktop.
Google-ADK, MCP discord example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| GOOGLE_GENAI_USE_VERTEXAI=FALSE | |
| GOOGLE_API_KEY= | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from . import agent |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import datetime | |
| import requests | |
| import json | |
| from zoneinfo import ZoneInfo | |
| from google.adk.agents import Agent | |
| WEBHOOK_URL = "https://discord.com/api/webhooks/111/222" | |
| # -------------------------------------------------------------------------- | |
| # Toolの定義 | |
| # -------------------------------------------------------------------------- | |
| def discord_send_message(message: str) -> str: | |
| """ | |
| discordにメッセージを送信する実処理。 | |
| LLMはこのdocstringを読んで、'message'という引数が必要だと理解します。 | |
| 指定された メッセージ discordに送信します。 | |
| 自然言語から「メッセージ」を自動で抽出して、この関数の引数に渡します。 | |
| Args: | |
| message: discordに送信するテキストメッセージ。 | |
| Returns: | |
| str: API実行結果を要約した、ユーザーへの返答メッセージ。 | |
| """ | |
| print("--- Tool: discord_send_message が呼び出されました ---") | |
| print(f" [抽出された引数] message='{message}'") | |
| # --- ここからが外部API実行のシミュレーションです --- | |
| # TODO: あなたの実際のAPIエンドポイントURLに置き換えてください | |
| # 送信するメッセージ | |
| payload = { | |
| "content": message, | |
| "username": "Webhook-user-2", # 送信者の名前(任意) | |
| } | |
| print(f" [Webhook送信] URL: {WEBHOOK_URL}") | |
| print(f" [Webhook送信] データ: {payload}") | |
| try: | |
| # 実際のAPI呼び出しを行う場合は、以下のコメントを解除します。 | |
| # HTTP POSTリクエストを送信 | |
| response = requests.post( | |
| WEBHOOK_URL, | |
| data=json.dumps(payload), | |
| headers={"Content-Type": "application/json"} | |
| ) | |
| print("status_code=" + str(response.status_code)) | |
| # このサンプルでは、API呼び出しが成功したと仮定します。 | |
| print(" [Webhook結果] 成功したと仮定します。") | |
| # メッセージを送信する実処理 | |
| return f"承知いたしました。「{message}」をメッセージを送信しました。" | |
| except requests.exceptions.RequestException as e: | |
| # API呼び出しでネットワークエラーなどが発生した場合の処理 | |
| print(f" [API結果] エラーが発生しました: {e}") | |
| return f"申し訳ありません。システムの通信エラーにより、登録に失敗しました。" | |
| except Exception as e: | |
| # その他の予期せぬエラーが発生した場合の処理 | |
| print(f" [API結果] 予期せぬエラーが発生しました: {e}") | |
| return f"申し訳ありません。予期せぬエラーにより、登録に失敗しました。" | |
| finally: | |
| print("--- Tool: 処理を終了します ---") | |
| # --- ここまでが外部API実行のシミュレーションです --- | |
| # get_now_date | |
| # Returns the current date in YYYY-MM-DD format | |
| def get_now_date() -> str: | |
| """Returns the current date in YYYY-MM-DD format. | |
| Returns: | |
| dict: status and result with current date. | |
| """ | |
| current_date = datetime.datetime.now().strftime("%Y-%m-%d") | |
| return f"今日の日付: {current_date}" | |
| # Agent | |
| root_agent = Agent( | |
| name="tool_agent_5", | |
| model="gemini-2.0-flash", | |
| description=( | |
| "Agent の回答は。 本日の日時、 ユーザーのメッセージ を、discordに送信して欲しい。" | |
| ), | |
| instruction=( | |
| "You are a helpful agent. 本日の日時 を返却します。" | |
| "ユーザーの入力から「こんにちは」などのメッセージを抽出し、discordに送信してください。" | |
| "例: 「こんにちは」 のメッセージを discordに送信して欲しい。" | |
| ), | |
| tools=[get_now_date, discord_send_message], | |
| ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment