Prerequisites

Before we begin, you’ll need:

Installation

First, install the required packages:

pip install llama-index llama-index-tools-mcp klavis

Setup Environment Variables

import os

# Set environment variables
os.environ["OPENAI_API_KEY"] = "your-openai-api-key-here"    # Replace with your actual OpenAI API key
os.environ["KLAVIS_API_KEY"] = "your-klavis-api-key-here"   # Replace with your actual Klavis API key

Basic Setup

import os
from klavis import Klavis
from klavis.types import McpServerName, ConnectionType
from llama_index.llms.openai import OpenAI
from llama_index.tools.mcp import (
    BasicMCPClient,
    get_tools_from_mcp_url,
    aget_tools_from_mcp_url,
)

# Initialize clients
klavis_client = Klavis(api_key=os.getenv("KLAVIS_API_KEY"))
llm = OpenAI(model="gpt-4o-mini", api_key=os.getenv("OPENAI_API_KEY"))

Single Agent Integration

Let’s start with creating a simple AI agent that can summarize YouTube videos using LlamaIndex and Klavis MCP Server.

Step 1: Create MCP Server Instance

# Create a YouTube MCP server and get the server URL
youtube_mcp_instance = klavis_client.mcp_server.create_server_instance(
    server_name=McpServerName.YOUTUBE,
    user_id="1234",
    platform_name="Klavis",
    connection_type=ConnectionType.STREAMABLE_HTTP,
)

youtube_mcp_server_url = youtube_mcp_instance.server_url
print(f"🔗 YouTube MCP server created at: {youtube_mcp_server_url}")

Step 2: Create LlamaIndex Agent with MCP Tools

from llama_index.core.agent.workflow import FunctionAgent

# Get tools from MCP server
youtube_tools = await aget_tools_from_mcp_url(
    youtube_mcp_server_url, 
    client=BasicMCPClient(youtube_mcp_server_url)
)

# Create agent with MCP-based tools
youtube_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!")

Step 3: Use the Agent

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)

Multi-Agent Workflow

Now let’s build a more sophisticated multi-agent workflow that summarizes YouTube videos and sends the summary via email.

Step 1: Create Multiple MCP Server Instances

import webbrowser

# Create YouTube MCP server
youtube_mcp_instance = klavis_client.mcp_server.create_server_instance(
    server_name=McpServerName.YOUTUBE,
    user_id="1234",
    platform_name="Klavis",
    connection_type=ConnectionType.STREAMABLE_HTTP,
)

# Create Gmail MCP server with OAuth authorization
gmail_mcp_instance = klavis_client.mcp_server.create_server_instance(
    server_name=McpServerName.GMAIL,
    user_id="1234",
    platform_name="Klavis",
    connection_type=ConnectionType.STREAMABLE_HTTP,
)

print("✅ Created YouTube and Gmail MCP instances")

# Open Gmail OAuth authorization
webbrowser.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}")

Step 2: Create Multi-Agent Workflow

from llama_index.core.agent.workflow import FunctionAgent, AgentWorkflow

# Get MCP server URLs
youtube_mcp_server_url = youtube_mcp_instance.server_url
gmail_mcp_server_url = gmail_mcp_instance.server_url

# Get tools from both MCP servers
youtube_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 agents
youtube_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 workflow
workflow = AgentWorkflow(
    agents=[youtube_agent, gmail_agent],
    root_agent="youtube_agent",
)

print("🤖 Multi-agent workflow created with YouTube and Gmail agents!")

Step 3: Run the Multi-Agent Workflow

YOUTUBE_VIDEO_URL = "https://www.youtube.com/watch?v=MmiveeGxfX0"  # pick a video you like!
EMAIL_RECIPIENT = "example@email.com"  # Replace with your email

resp = 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)

Next Steps

Explore More MCP Servers

Try other available servers like Slack, Notion, GitHub, etc.

Advanced Workflows

Build more complex multi-agent systems with branching logic

Custom Tools

Create custom tools and integrate them with your workflows

Production Deployment

Scale these patterns for production applications

Useful Resources

Happy building with LlamaIndex and Klavis! 🚀