The Problem
AI agents often rely on isolated data or models, making it difficult to access dynamic content, like CRM, financial or sales data, and more, without bespoke code for each integration. This complexity leads to brittle pipelines, increased maintenance, and slowed development.
The Solution
LlamaIndex provides powerful agent frameworks and AgentWorkflow capabilities that make it easy to build sophisticated multi-agent systems.
Klavis AI is open source MCP integrations for AI Applications. The API provides hosted, secure MCP servers, eliminating auth management and client-side code.
With just a few lines of code, you can connect your AI agent to any high-quality remote MCP server, no extra client libraries, no authentication hassles.
Technical walkthrough: Integrating Klavis AI with LlamaIndex
Prerequisites
Before we begin, you'll need:
- OpenAI API key - Get at openai.com
- Klavis API key - Get at klavis.ai
๐ Installation
# Install the required packages
pip install -qU llama-index llama-index-tools-mcp klavis
Then set your environment variables:
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
# Set environment variables
os.environ["OPENAI_API_KEY"] = "YOUR_OPENAI_API_KEY" # Replace with your actual OpenAI API key
os.environ["KLAVIS_API_KEY"] = "YOUR_KLAVIS_API_KEY" # Replace with your actual Klavis API key
Case 1: YouTube AI Agent
Create an AI agent to summarize YouTube videos using LlamaIndex and Klavis MCP Server.
Step 1 - Using Klavis to create YouTube MCP Server
klavis_client = Klavis(api_key=os.getenv("KLAVIS_API_KEY"))
# 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 - Using LlamaIndex to create AI Agent with the MCP Server
from llama_index.core.agent.workflow import FunctionAgent
from llama_index.tools.mcp import (
get_tools_from_mcp_url,
aget_tools_from_mcp_url,
)
llm = OpenAI(model="gpt-4o-mini", api_key=os.getenv("OPENAI_API_KEY"))
youtube_tools = await aget_tools_from_mcp_url(youtube_mcp_server_url, client=BasicMCPClient(youtube_mcp_server_url))
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."
)
Step 3 - Run your AI Agent to summarize your favorite video!
YOUTUBE_VIDEO_URL = "https://www.youtube.com/watch?v=MmiveeGxfX0&t=528s" # pick a video you like!
response = await youtube_agent.run(f"Summarize this video: {YOUTUBE_VIDEO_URL}")
print(response)
Sample Output:
The video titled "Introducing AgentWorkflow, a way to easily create multi-agent systems in Llamaindex" presents a new system called AgentWorkflow designed for building and orchestrating AI agent systems. It emphasizes the ability to coordinate multiple AI agents while maintaining state and context, making it suitable for both single specialized agents and teams working together.
Case 2: Multi-Agent Workflow
Build a LlamaIndex AgentWorkflow that summarizes YouTube videos and sends the summary via email.
Step 1 - Using Klavis to create YouTube and Gmail MCP Servers
import webbrowser
klavis_client = Klavis(api_key=os.getenv("KLAVIS_API_KEY"))
# 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 - Using LlamaIndex to create Multi-Agent Workflow with the MCP Servers
from llama_index.llms.openai import OpenAI
from llama_index.core.agent.workflow import FunctionAgent, AgentWorkflow
from llama_index.tools.mcp import (
BasicMCPClient,
get_tools_from_mcp_url,
aget_tools_from_mcp_url,
)
llm = OpenAI(model="gpt-4o-mini", api_key=os.getenv("OPENAI_API_KEY"))
# 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 workflow!
YOUTUBE_VIDEO_URL = "https://www.youtube.com/watch?v=MmiveeGxfX0&t=528s" # pick a video you like!
EMAIL_RECIPIENT = "zihaolin@klavis.ai" # 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โ
Report:\n", resp.response.content)
Sample Output:
โ Report: The summary of the video "Introducing AgentWorkflow, a way to easily create multi-agent systems in Llamaindex" has been successfully sent to xxx@gmail.com. If you need anything else, feel free to ask!
Next Steps
Now that you've learned how to integrate LlamaIndex with Klavis AI, here are some ideas to take your AI agents to the next level:
- Explore More MCP Servers: Try other available servers like Slack, Notion, GitHub, and more. Each server opens up new possibilities for your AI agents to interact with different platforms and services.
- Build Advanced Workflows: Create more complex multi-agent systems with branching logic, conditional handoffs, and sophisticated decision-making capabilities.
- Create Custom Tools: Develop custom tools and integrate them with your workflows to handle specific business logic or domain-specific tasks.
- Scale for Production: Implement proper error handling, logging, monitoring, and deployment strategies to take these patterns into production applications.
Resources
- LlamaIndex Documentation - Complete guide to LlamaIndex framework
- LlamaIndex Agent Workflows - Deep dive into multi-agent systems
- Klavis AI Documentation - Learn more about Klavis MCP servers
- MCP Protocol Specification - Understand the Model Context Protocol
- Klavis MCP Servers - Browse available MCP servers
Happy building with LlamaIndex and Klavis! ๐