Connect Any LLM to Klavis MCP Servers

ยท5 min read

Cover Image for Connect Any LLM to Klavis MCP Servers

Connect your favorite LLM to external services like GSuite, CRMs, Slack, and GitHub in minutes with Klavis MCP servers. No complex integrations needed โ€“ just plug and play.

Quick Start

Prerequisites

  • Klavis AI API Key - Get at klavis.ai
  • API key for your LLM provider (OpenAI, Google AI, Anthropic, etc.)

Basic Setup

All LLM integrations follow the same pattern:

from klavis import Klavis
from klavis.types import McpServerName

# Initialize Klavis client
klavis_client = Klavis(api_key="YOUR_KLAVIS_API_KEY")

# Create an MCP server instance (example: YouTube)
server = klavis_client.mcp_server.create_server_instance(
    server_name=McpServerName.YOUTUBE,
    user_id="user123",
)

print(f"๐Ÿ”— MCP server ready: {server.server_url}")

Complete Example: OpenAI + YouTube

Here's a working example that lets ChatGPT analyze YouTube videos:

import json
from openai import OpenAI
from klavis import Klavis
from klavis.types import McpServerName, ToolFormat

# Initialize clients
openai_client = OpenAI(api_key="YOUR_OPENAI_API_KEY")
klavis_client = Klavis(api_key="YOUR_KLAVIS_API_KEY")

# Create YouTube MCP server
youtube_server = klavis_client.mcp_server.create_server_instance(
    server_name=McpServerName.YOUTUBE,
    user_id="user123",
)

def analyze_youtube_video(video_url: str):
    """Analyze a YouTube video using OpenAI + Klavis"""
    
    # Get YouTube tools in OpenAI format
    tools = klavis_client.mcp_server.list_tools(
        server_url=youtube_server.server_url,
        format=ToolFormat.OPENAI,
    )
    
    # Ask OpenAI to analyze the video
    response = openai_client.chat.completions.create(
        model="gpt-4o-mini",
        messages=[
            {"role": "system", "content": "You are a helpful assistant. Use available tools to analyze content."},
            {"role": "user", "content": f"Analyze this YouTube video and summarize key points: {video_url}"}
        ],
        tools=tools.tools
    )
    
    # Handle tool calls automatically
    if response.choices[0].message.tool_calls:
        for tool_call in response.choices[0].message.tool_calls:
            function_name = tool_call.function.name
            function_args = json.loads(tool_call.function.arguments)
            
            # Execute tool via Klavis
            result = klavis_client.mcp_server.call_tools(
                server_url=youtube_server.server_url,
                tool_name=function_name,
                tool_args=function_args,
            )
            
            # Return the analysis
            return f"Video Analysis: {result.result.content[0]['text']}"
    
    return response.choices[0].message.content

# Test it
video_url = "https://www.youtube.com/watch?v=dQw4w9WgXcQ"
analysis = analyze_youtube_video(video_url)
print(analysis)

Works with Any LLM

The same pattern works with any LLM provider. Just change the tool format:

Google Gemini

# Get tools in Gemini format
tools = klavis_client.mcp_server.list_tools(
    server_url=server.server_url,
    format=ToolFormat.GEMINI,
)

Anthropic Claude

# Get tools in Claude format
tools = klavis_client.mcp_server.list_tools(
    server_url=server.server_url,
    format=ToolFormat.ANTHROPIC,
)

Custom LLMs

# Get tools in generic format for custom implementations
tools = klavis_client.mcp_server.list_tools(
    server_url=server.server_url,
    format=ToolFormat.GENERIC,
)

OAuth Services (Gmail, Slack, etc.)

For services requiring OAuth authentication:

import webbrowser

# Create Gmail MCP server
gmail_server = klavis_client.mcp_server.create_server_instance(
    server_name=McpServerName.GMAIL,
    user_id="user123",
)

# Open OAuth URL for user authorization
webbrowser.open(gmail_server.oauth_url)
print(f"๐Ÿ” Complete authorization: {gmail_server.oauth_url}")

# After OAuth completion, use with any LLM