import osimport jsonfrom openai import OpenAIfrom klavis import Klavisfrom klavis.types import McpServerName, ToolFormat# Set environment variablesos.environ["OPENAI_API_KEY"] = "YOUR_OPENAI_API_KEY" # Replace with your actual OpenAI API keyos.environ["KLAVIS_API_KEY"] = "YOUR_KLAVIS_API_KEY" # Replace with your actual Klavis API key
klavis_client = Klavis(api_key=os.getenv("KLAVIS_API_KEY"))youtube_mcp_instance = klavis_client.mcp_server.create_server_instance( server_name=McpServerName.YOUTUBE, user_id="1234", platform_name="Klavis",)# print(f"🔗 YouTube MCP server created at: {youtube_mcp_instance.server_url}, and the instance id is {youtube_mcp_instance.instance_id}")
YOUTUBE_VIDEO_URL = "https://www.youtube.com/watch?v=LCEmiRjPEtQ" # pick a video you like!result = openai_with_mcp_server( mcp_server_url=youtube_mcp_instance.server_url, user_query=f"Summarize this YouTube video with timestamps: {YOUTUBE_VIDEO_URL}")print(result)
✅ Great! You’ve successfully created an AI agent that uses OpenAI function calling with Klavis MCP servers to summarize YouTube videos!
Case Study 2: OpenAI + Gmail MCP Server (OAuth needed)
Copy
Ask AI
import webbrowsergmail_mcp_server = klavis_client.mcp_server.create_server_instance( server_name=McpServerName.GMAIL, user_id="1234", platform_name="Klavis",)webbrowser.open(gmail_mcp_server.oauth_url)print(f"🔐 Opening OAuth authorization for Gmail, if you are not redirected, please open the following URL in your browser: {gmail_mcp_server.oauth_url}")
After completing the OAuth authorization, you can send emails using the agent.
Copy
Ask AI
EMAIL_RECIPIENT = "zihaolin@klavis.ai" # Replace with your emailEMAIL_SUBJECT = "Test OpenAI + Gmail MCP Server"EMAIL_BODY = "Hello World"result = openai_with_mcp_server( mcp_server_url=gmail_mcp_server.server_url, user_query=f"Please send an email to {EMAIL_RECIPIENT} with subject {EMAIL_SUBJECT} and body {EMAIL_BODY}")print(result)
This tutorial demonstrated how to integrate OpenAI’s function calling capabilities with Klavis MCP servers to create powerful AI applications. We covered two practical examples:
🎥 YouTube Integration: Built an AI assistant that can automatically summarize YouTube videos by extracting transcripts and providing detailed, timestamped summaries.
📧 Gmail Integration: Created an AI-powered email assistant that can send emails through Gmail with OAuth authentication.