LlamaIndex has officially showcased their integration with Klavis AI in this LinkedIn post, demonstrating how to build AI agents that connect to MCP Servers in just a few lines of code.
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}")
from llama_index.core.agent.workflow import FunctionAgent# Get tools from MCP serveryoutube_tools = await aget_tools_from_mcp_url( youtube_mcp_server_url, client=BasicMCPClient(youtube_mcp_server_url))# Create agent with MCP-based toolsyoutube_agent = FunctionAgent( name="youtube_agent", description="Agent using MCP-based tools", tools=youtube_tools, llm=llm, system_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=MmiveeGxfX0" # pick a video you like!response = await youtube_agent.run(f"Summarize this video: {YOUTUBE_VIDEO_URL}")print("✅ Video Summary:", response)
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 llama_index.core.agent.workflow import FunctionAgent, AgentWorkflow# Get MCP server URLsyoutube_mcp_server_url = youtube_mcp_instance.server_urlgmail_mcp_server_url = gmail_mcp_instance.server_url# Get tools from both MCP serversyoutube_tools = await aget_tools_from_mcp_url( youtube_mcp_server_url, client=BasicMCPClient(youtube_mcp_server_url))gmail_tools = await aget_tools_from_mcp_url( gmail_mcp_server_url, client=BasicMCPClient(gmail_mcp_server_url))# Create specialized agentsyoutube_agent = FunctionAgent( name="youtube_agent", description="Agent that can summarize YouTube videos", tools=youtube_tools, llm=llm, system_prompt="You are a YouTube video summarization expert. Use MCP tools to analyze and summarize videos.", can_handoff_to=["gmail_agent"],)gmail_agent = FunctionAgent( name="gmail_agent", description="Agent that can send emails via Gmail", tools=gmail_tools, llm=llm, system_prompt="You are an email assistant. Use MCP tools to send emails via Gmail.")# Create multi-agent workflowworkflow = AgentWorkflow( agents=[youtube_agent, gmail_agent], root_agent="youtube_agent",)print("🤖 Multi-agent workflow created with YouTube and Gmail agents!")
YOUTUBE_VIDEO_URL = "https://www.youtube.com/watch?v=MmiveeGxfX0" # pick a video you like!EMAIL_RECIPIENT = "example@email.com" # Replace with your emailresp = await workflow.run( user_msg=f"Summarize this video {YOUTUBE_VIDEO_URL} and send it to {EMAIL_RECIPIENT}")print("\n✅ Workflow Result:", resp.response.content)