import os# Set environment variablesos.environ["OPENAI_API_KEY"] = "your-openai-api-key-here" # Replace with your actual OpenAI API keyos.environ["KLAVIS_API_KEY"] = "your-klavis-api-key-here" # Replace with your actual Klavis API key
# Create a YouTube MCP server and get the server URLyoutube_mcp_instance = klavis_client.mcp_server.create_server_instance( server_name=McpServerName.YOUTUBE, user_id="1234", platform_name="klavis",)youtube_mcp_server_url = youtube_mcp_instance.server_urlprint(f"🔗 YouTube MCP server created at: {youtube_mcp_server_url}")
# Create MCP client with YouTube servermcp_client = MultiServerMCPClient({ "youtube": { "transport": "streamable_http", "url": youtube_mcp_server_url }})# Get tools from MCP servertools = asyncio.run(mcp_client.get_tools())# Create agent with MCP-based toolsyoutube_agent = create_react_agent( model=llm, tools=tools, prompt="You are an AI assistant that uses MCP tools to analyze YouTube videos.")print("🤖 YouTube AI agent created successfully!")
YOUTUBE_VIDEO_URL = "https://www.youtube.com/watch?v=LCEmiRjPEtQ" # pick a video you like!response = asyncio.run(youtube_agent.ainvoke({ "messages": [{"role": "user", "content": f"Summarize this video: {YOUTUBE_VIDEO_URL}"}]}))print("✅ Video Summary:", response["messages"][-1].content)
import webbrowser# Create YouTube MCP serveryoutube_mcp_instance = klavis_client.mcp_server.create_server_instance( server_name=McpServerName.YOUTUBE, user_id="1234", platform_name="klavis",)# Create Gmail MCP server with OAuth authorizationgmail_mcp_instance = klavis_client.mcp_server.create_server_instance( server_name=McpServerName.GMAIL, user_id="1234", platform_name="klavis",)print("✅ Created YouTube and Gmail MCP instances")# Open Gmail OAuth authorizationwebbrowser.open(gmail_mcp_instance.oauth_url)print(f"🔐 Opening OAuth authorization for Gmail, if you are not redirected, please open the following URL in your browser: {gmail_mcp_instance.oauth_url}")
from langgraph.graph import StateGraph, MessagesStatefrom typing import Annotated, Literalfrom langchain_core.messages import BaseMessage# Get MCP server URLsyoutube_mcp_server_url = youtube_mcp_instance.server_urlgmail_mcp_server_url = gmail_mcp_instance.server_url# Create a single MCP client with both serversmcp_client = MultiServerMCPClient({ "youtube": { "transport": "streamable_http", "url": youtube_mcp_server_url }, "gmail": { "transport": "streamable_http", "url": gmail_mcp_server_url }})# Get tools from all MCP serversall_tools = asyncio.run(mcp_client.get_tools())# Create agents with access to all toolsyoutube_agent = create_react_agent( model=llm, tools=all_tools, prompt="You are a YouTube video summarization expert. Use MCP tools to analyze and summarize videos. After summarizing, pass the summary to the gmail agent.")gmail_agent = create_react_agent( model=llm, tools=all_tools, prompt="You are an email assistant. Use MCP tools to send emails via Gmail.")print("🤖 Multi-agent workflow created with YouTube and Gmail agents!")
YOUTUBE_VIDEO_URL = "https://www.youtube.com/watch?v=LCEmiRjPEtQ" # pick a video you like!EMAIL_RECIPIENT = "example@email.com" # Replace with your emailinitial_state = { "messages": [{ "role": "user", "content": f"Summarize this video {YOUTUBE_VIDEO_URL} and send it to {EMAIL_RECIPIENT}" }]}# Run the workflowresult = asyncio.run(app.ainvoke(initial_state))print("\n✅ Workflow Result:", result)