-
-
Save kuc-arc-f/f1e5d7bd8636a0512b8d2ad60a1d9a6c to your computer and use it in GitHub Desktop.
Google ADK , MCP 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="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 | |
| # 例として、JSONPlaceholderというテスト用のAPIからToDoリストを取得するツールを定義します。 | |
| # 実際のAPIを使用する場合は、エンドポイントや認証方法に応じて変更してください。 | |
| def get_todo_list(limit: int = 5) -> dict: | |
| """ | |
| 外部APIを呼び出してToDoリストを取得します。 | |
| Args: | |
| limit (int): 取得するToDoの最大数。デフォルトは5。 | |
| Returns: | |
| dict: ステータスとToDoリストの結果。 | |
| 例: {"status": "success", "todos": [{"id": 1, "title": "...", "completed": False}, ...]} | |
| """ | |
| try: | |
| # JSONPlaceholderのToDo APIを呼び出し | |
| response = requests.get(f"https://jsonplaceholder.typicode.com/todos?_limit={limit}", timeout=10) | |
| response.raise_for_status() # ステータスコードが200でない場合に例外を発生 | |
| todos = response.json() | |
| # 必要な情報のみ抽出してリスト化 | |
| todo_list = [ | |
| { | |
| "id": todo["id"], | |
| "title": todo["title"], | |
| "completed": todo["completed"] | |
| } | |
| for todo in todos | |
| ] | |
| return { | |
| "status": "success", | |
| "todos": todo_list | |
| } | |
| except requests.exceptions.RequestException as e: | |
| # エラー発生時の処理 | |
| return { | |
| "status": "error", | |
| "message": f"API呼び出し中にエラーが発生しました: {str(e)}" | |
| } | |
| except Exception as e: | |
| return { | |
| "status": "error", | |
| "message": f"予期しないエラーが発生しました: {str(e)}" | |
| } | |
| # 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}" | |
| root_agent = Agent( | |
| name="weather_time_agent", | |
| model="gemini-2.0-flash", | |
| description=( | |
| "Agent to answer questions about the current date and fetch a list of TODOs from an external API." | |
| ), | |
| instruction=( | |
| "You are a helpful agent. You can tell the user the current date and fetch a list of TODO items from an external API. " | |
| "When the user asks for TODOs, use the `get_todo_list` tool. " | |
| "Present the TODO list clearly, for example, as a numbered list showing the title and completion status. " | |
| "If the tool returns an error, inform the user politely." | |
| ), | |
| tools=[get_now_date, get_todo_list], | |
| ) | |
| # | |
| # "When the user asks for TODOs, use the `get_todo_list` tool. " | |
| # "Present the TODO list clearly, for example, as a numbered list showing the title and completion status. " | |
| # "If the tool returns an error, inform the user politely." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment