|
| 1 | +""" |
| 2 | +MCP Agent Example: YouTube + Gmail |
| 3 | +---------------------------------- |
| 4 | +
|
| 5 | +This script demonstrates how to: |
| 6 | +
|
| 7 | +1. Load API keys securely from environment variables. |
| 8 | +2. Initialize the Klavis client. |
| 9 | +3. Create MCP server instances for YouTube and Gmail. |
| 10 | +4. Redirect user to Gmail OAuth flow for authorization. |
| 11 | +5. Create specialized agents for YouTube and Gmail. |
| 12 | +6. Combine them into a root Gemini agent for orchestration. |
| 13 | +
|
| 14 | +Requirements: |
| 15 | +- A `.env` file containing KLAVIS_API_KEY |
| 16 | +- Valid Klavis account with MCP server support |
| 17 | +- Browser access for Gmail OAuth flow |
| 18 | +
|
| 19 | +Usage: |
| 20 | + Run the following command to launch the Dev UI: adk web |
| 21 | + Run the following command, to chat with your agent: adk run multi_tool_agent |
| 22 | + API Server: adk api_server |
| 23 | +
|
| 24 | +Doc: |
| 25 | + https://google.github.io/adk-docs/get-started/quickstart/#run-your-agent |
| 26 | + https://google.github.io/adk-docs/tools/mcp-tools/#further-resources |
| 27 | +""" |
| 28 | + |
| 29 | +import os |
| 30 | +import logging |
| 31 | +import webbrowser |
| 32 | +from dotenv import load_dotenv |
| 33 | + |
| 34 | +from google.adk.agents import Agent |
| 35 | +from google.adk.tools.mcp_tool import StreamableHTTPConnectionParams |
| 36 | +from google.adk.tools.mcp_tool.mcp_toolset import MCPToolset |
| 37 | +from klavis import Klavis |
| 38 | +from klavis.types import McpServerName |
| 39 | + |
| 40 | +# ---------------------------------------------------------------------- |
| 41 | +# Logging Configuration |
| 42 | +# ---------------------------------------------------------------------- |
| 43 | +logging.basicConfig( |
| 44 | + level=logging.INFO, # Use DEBUG for verbose logging |
| 45 | + format="%(asctime)s [%(levelname)s] %(name)s: %(message)s", |
| 46 | +) |
| 47 | + |
| 48 | +logger = logging.getLogger("MCP_Agent_Example") |
| 49 | + |
| 50 | +# ---------------------------------------------------------------------- |
| 51 | +# Environment Setup |
| 52 | +# ---------------------------------------------------------------------- |
| 53 | +load_dotenv() |
| 54 | + |
| 55 | +KLAVIS_API_KEY = os.getenv("KLAVIS_API_KEY") |
| 56 | +if not KLAVIS_API_KEY: |
| 57 | + logger.error("❌ Missing KLAVIS_API_KEY in environment variables.") |
| 58 | + raise EnvironmentError("KLAVIS_API_KEY not found in .env file") |
| 59 | + |
| 60 | +logger.info("✅ Loaded KLAVIS_API_KEY from environment.") |
| 61 | + |
| 62 | +# ---------------------------------------------------------------------- |
| 63 | +# Klavis Client Initialization |
| 64 | +# ---------------------------------------------------------------------- |
| 65 | +logger.info("Initializing Klavis client...") |
| 66 | +klavis_client = Klavis(api_key=KLAVIS_API_KEY) |
| 67 | +logger.info("✅ Klavis client initialized.") |
| 68 | + |
| 69 | +# ---------------------------------------------------------------------- |
| 70 | +# Create YouTube MCP Server |
| 71 | +# ---------------------------------------------------------------------- |
| 72 | +logger.info("Creating YouTube MCP server instance...") |
| 73 | +youtube_mcp_server = klavis_client.mcp_server.create_server_instance( |
| 74 | + server_name=McpServerName.YOUTUBE, |
| 75 | + user_id="1234", |
| 76 | + platform_name="Klavis", |
| 77 | +) |
| 78 | + |
| 79 | +youtube_mcp_server_url = youtube_mcp_server.server_url |
| 80 | +logger.info( |
| 81 | + "✅ YouTube MCP server created at %s (instance id: %s)", |
| 82 | + youtube_mcp_server_url, |
| 83 | + youtube_mcp_server.instance_id, |
| 84 | +) |
| 85 | + |
| 86 | +# ---------------------------------------------------------------------- |
| 87 | +# Create Gmail MCP Server with OAuth |
| 88 | +# ---------------------------------------------------------------------- |
| 89 | +logger.info("Creating Gmail MCP server instance...") |
| 90 | +gmail_mcp_server = klavis_client.mcp_server.create_server_instance( |
| 91 | + server_name=McpServerName.GMAIL, |
| 92 | + user_id="1234", |
| 93 | + platform_name="Klavis", |
| 94 | +) |
| 95 | + |
| 96 | +logger.info("✅ Gmail MCP server instance created.") |
| 97 | + |
| 98 | +# Redirect user to Gmail OAuth |
| 99 | +if gmail_mcp_server.oauth_url: |
| 100 | + logger.info("🔐 Redirecting to Gmail OAuth authorization page...") |
| 101 | + try: |
| 102 | + webbrowser.open(gmail_mcp_server.oauth_url) |
| 103 | + logger.info("✅ Gmail OAuth page opened in browser.") |
| 104 | + except Exception as e: |
| 105 | + logger.warning("⚠️ Could not open browser automatically. Please visit: %s", gmail_mcp_server.oauth_url) |
| 106 | +else: |
| 107 | + logger.error("❌ Gmail server did not return an OAuth URL.") |
| 108 | + |
| 109 | +gmail_mcp_server_url = gmail_mcp_server.server_url |
| 110 | +logger.info( |
| 111 | + "✅ Gmail MCP server created at %s (instance id: %s)", |
| 112 | + gmail_mcp_server_url, |
| 113 | + gmail_mcp_server.instance_id, |
| 114 | +) |
| 115 | + |
| 116 | +# ---------------------------------------------------------------------- |
| 117 | +# Agent Setup |
| 118 | +# ---------------------------------------------------------------------- |
| 119 | +logger.info("Setting up agents with MCP toolsets...") |
| 120 | + |
| 121 | +youtube_agent = Agent( |
| 122 | + name="YouTube_Agent", |
| 123 | + model="gemini-2.0-flash", |
| 124 | + description="Agent specialized in handling YouTube queries.", |
| 125 | + instruction="You are a helpful YouTube agent.", |
| 126 | + tools=[ |
| 127 | + MCPToolset( |
| 128 | + connection_params=StreamableHTTPConnectionParams( |
| 129 | + url=youtube_mcp_server_url, |
| 130 | + ), |
| 131 | + ) |
| 132 | + ], |
| 133 | +) |
| 134 | + |
| 135 | +gmail_agent = Agent( |
| 136 | + name="Gmail_Agent", |
| 137 | + model="gemini-2.0-flash", |
| 138 | + description="Agent specialized in handling Gmail queries.", |
| 139 | + instruction="You are a helpful Gmail agent.", |
| 140 | + tools=[ |
| 141 | + MCPToolset( |
| 142 | + connection_params=StreamableHTTPConnectionParams( |
| 143 | + url=gmail_mcp_server_url, |
| 144 | + ), |
| 145 | + ) |
| 146 | + ], |
| 147 | +) |
| 148 | + |
| 149 | +gemini_agent = Agent( |
| 150 | + name="Gemini", |
| 151 | + model="gemini-2.0-flash", |
| 152 | + description="Root Orchestrator Agent", |
| 153 | + instruction="You are a helpful orchestrator agent combining Gmail and YouTube capabilities.", |
| 154 | + sub_agents=[youtube_agent, gmail_agent], |
| 155 | +) |
| 156 | + |
| 157 | +root_agent = gemini_agent |
| 158 | +logger.info("✅ Root agent '%s' initialized and ready.", root_agent.name) |
0 commit comments