-
-
Save kuc-arc-f/bbffd879953be926782a0b630eb03592 to your computer and use it in GitHub Desktop.
Google-ADK, MCP slack 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 | |
| from zoneinfo import ZoneInfo | |
| from google.adk.agents import Agent | |
| Webhook_URL="https://hooks.slack.com/services/param1/param2" | |
| # -------------------------------------------------------------------------- | |
| # Toolの定義 | |
| # -------------------------------------------------------------------------- | |
| def slack_send_message(message: str) -> str: | |
| """ | |
| Slackにメッセージを送信する実処理。 | |
| LLMはこのdocstringを読んで、'message'という引数が必要だと理解します。 | |
| 指定された メッセージ Slackに送信します。 | |
| 自然言語から「メッセージ」を自動で抽出して、この関数の引数に渡します。 | |
| Args: | |
| message: Slackに送信するテキストメッセージ。 | |
| Returns: | |
| str: API実行結果を要約した、ユーザーへの返答メッセージ。 | |
| """ | |
| print("--- Tool: slack_send_message が呼び出されました ---") | |
| print(f" [抽出された引数] message='{message}'") | |
| # --- ここからが外部API実行のシミュレーションです --- | |
| # TODO: あなたの実際のAPIエンドポイントURLに置き換えてください | |
| payload = {"text": message} | |
| print(f" [Webhook送信] URL: {Webhook_URL}") | |
| print(f" [Webhook送信] データ: {payload}") | |
| try: | |
| # 実際のAPI呼び出しを行う場合は、以下のコメントを解除します。 | |
| response = requests.post(Webhook_URL, json=payload) | |
| print("status_code=" + str(response.status_code)) | |
| # このサンプルでは、API呼び出しが成功したと仮定します。 | |
| print(" [Webhook結果] 成功したと仮定します。") | |
| # Slackにメッセージを送信する実処理 | |
| return f"承知いたしました。「{message}」をSlackにメッセージを送信しました。" | |
| 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_4", | |
| model="gemini-2.0-flash", | |
| description=( | |
| "Agent の回答は。 本日の日時、 ユーザーのメッセージ を、Slackに送信して欲しい。" | |
| ), | |
| instruction=( | |
| "You are a helpful agent. 本日の日時 を返却します。" | |
| "ユーザーの入力から「こんにちは」などのメッセージを抽出し、Slackに送信してください。" | |
| "例: 「こんにちは」 のメッセージを Slackに送信して欲しい。" | |
| ), | |
| tools=[get_now_date, slack_send_message], | |
| ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment