Fireworks AI + Klavis AI Integration

In this tutorial, we’ll explore how to build an AI agent that integrates Fireworks AI’s LLMs with Klavis MCP Servers, enabling seamless interaction with external services and APIs.

This integration combines:

  • Fireworks AI: Fast and efficient LLMs with function calling capabilities
  • Klavis AI: MCP servers for connecting to external tools and services

Prerequisites

Before we begin, you’ll need:

Make sure to keep these API keys secure and never commit them to version control!

Installation

First, install the required packages:

pip install fireworks-ai requests

Setup Environment Variables

import os

# Set environment variables
os.environ["FIREWORKS_API_KEY"] = "your-fireworks-api-key-here"  # Replace with your actual Fireworks API key
os.environ["KLAVIS_API_KEY"] = "your-klavis-api-key-here"       # Replace with your actual Klavis API key

Klavis API Client

Set up the Klavis API client to interact with MCP servers:

import requests
import urllib.parse
from typing import Dict, Any, Optional, List
import webbrowser

class KlavisAPI:
    """API Client for Klavis API."""
    
    def __init__(self, api_key: str, base_url: str = "https://api.klavis.ai"):
        self.api_key = api_key
        self.base_url = base_url
        self.headers = {
            "Authorization": f"Bearer {api_key}",
            "Content-Type": "application/json"
        }
    
    def _make_request(self, method: str, endpoint: str, **kwargs) -> Dict[str, Any]:
        """Make HTTP request with error handling."""
        url = f"{self.base_url}{endpoint}"
        response = requests.request(method, url, headers=self.headers, **kwargs)
        response.raise_for_status()
        return response.json()
    
    def create_mcp_instance(self, server_name: str, user_id: str, platform_name: str) -> Dict[str, str]:
        """Create MCP server instance."""
        data = {
            "serverName": server_name,
            "userId": user_id,
            "platformName": platform_name,
            "connectionType": "StreamableHttp"
        }
        result = self._make_request("POST", "/mcp-server/instance/create", json=data)
        print(f"✅ Created {server_name} MCP instance")
        return {
            'serverUrl': result['serverUrl'],
            'instanceId': result['instanceId']
        }
    
    def list_tools(self, server_url: str) -> Dict[str, Any]:
        """List all available tools for an MCP server."""
        params = {"connection_type": "StreamableHttp"}
        encoded_url = urllib.parse.quote(server_url, safe='')
        return self._make_request("GET", f"/mcp-server/list-tools/{encoded_url}", params=params)
    
    def _convert_mcp_tools_to_openai_format(self, mcp_tools: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
        """Convert MCP tools format to OpenAI function calling format."""
        openai_tools = []
        
        for tool in mcp_tools:
            openai_tool = {
                "type": "function",
                "function": {
                    "name": tool.get("name", ""),
                    "description": tool.get("description", ""),
                    "parameters": tool.get("inputSchema", {})
                }
            }
            openai_tools.append(openai_tool)
        
        return openai_tools
    
    def call_tool(self, server_url: str, tool_name: str, 
                  tool_args: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
        """Call tool on MCP server."""
        data = {
            "serverUrl": server_url,
            "toolName": tool_name,
            "toolArgs": tool_args or {},
            "connectionType": "StreamableHttp"
        }
        return self._make_request("POST", "/mcp-server/call-tool", json=data)
    
    def redirect_to_oauth(self, instance_id: str, server_name: str) -> None:
        """Open OAuth authorization URL in browser."""
        oauth_url = f"{self.base_url}/oauth/{server_name.lower()}/authorize?instance_id={instance_id}"
        print(f"🔐 Opening OAuth authorization for {server_name}, if you are not redirected, please open the following URL in your browser: {oauth_url}")
        webbrowser.open(oauth_url)

AI Agent with MCP Integration

Now we’ll create an intelligent agent that can use MCP servers through Klavis API. This agent will:

  1. Discover Tools: Automatically find available tools from MCP servers
  2. Function Calling: Use Fireworks AI’s function calling capabilities
  3. Tool Execution: Execute tools through Klavis API
  4. Smart Responses: Generate intelligent responses based on tool results
import json

class Agent:
    def __init__(self, fireworks_client, klavis_api_client, mcp_server_url):
        self.fireworks = fireworks_client
        self.klavis = klavis_api_client
        self.mcp_server_url = mcp_server_url
        self.model = "accounts/fireworks/models/qwen2p5-72b-instruct"
        print(f"🤖 Agent initialized with model: {self.model}")
    
    def process_request(self, user_message):
        """Process a user request using Fireworks AI + Klavis integration."""
        # 1. Get available tools
        mcp_tools = self.klavis.list_tools(self.mcp_server_url)
        tools = self.klavis._convert_mcp_tools_to_openai_format(mcp_tools.get('tools', []))
        
        # 2. Call LLM with tools
        messages = [
            {"role": "system", "content": "You are a helpful assistant."},
            {"role": "user", "content": user_message}
        ]
        
        response = self.fireworks.chat.completions.create(
            model=self.model,
            messages=messages,
            tools=tools
        )
        
        assistant_message = response.choices[0].message
        messages.append(assistant_message)
        
        # 3. If LLM wants to use tools
        if assistant_message.tool_calls:
            
            # Execute each tool call
            for tool_call in assistant_message.tool_calls:
                tool_name = tool_call.function.name
                tool_args = json.loads(tool_call.function.arguments)
                
                print(f"🛠️ Calling tool: {tool_name} with args: {tool_args}")
                # Call tool via Klavis API
                tool_result = self.klavis.call_tool(
                    server_url=self.mcp_server_url,
                    tool_name=tool_name,
                    tool_args=tool_args
                )
                
                messages.append({
                    "role": "tool",
                    "tool_call_id": tool_call.id,
                    "content": json.dumps(tool_result)
                })
            
            # 4. Get final response from LLM
            final_response = self.fireworks.chat.completions.create(
                model=self.model,
                messages=messages
            )
            return final_response.choices[0].message.content
        
        # If no tools needed, return the assistant message directly
        return assistant_message.content

Use Case Examples

Example 1: Summarize YouTube Video

1

Initialize Clients

Set up Fireworks AI and Klavis API clients

2

Create MCP Instance

Create a YouTube MCP server instance

3

Process Request

Use the agent to analyze and summarize a YouTube video

import os
from fireworks.client import Fireworks

YOUTUBE_VIDEO_URL = "https://www.youtube.com/watch?v=kPXvf2-C_Hs"  # Pick a video you like!

# 1. Initialize Fireworks AI client and Klavis API client
fireworks_client = Fireworks(api_key=os.getenv("FIREWORKS_API_KEY"))
klavis_api_client = KlavisAPI(api_key=os.getenv("KLAVIS_API_KEY"))

# 2. Create a YouTube MCP server using Klavis API
youtube_mcp_instance = klavis_api_client.create_mcp_instance(
    server_name="YouTube",
    user_id="1234",
    platform_name="Klavis",
)

# 3. Create an agent with YouTube MCP server
agent = Agent(fireworks_client, klavis_api_client, youtube_mcp_instance["serverUrl"])

# 4. Process the request
response = agent.process_request(
    f"Summarize this YouTube video with timestamps: {YOUTUBE_VIDEO_URL}"
)

print(response)

Example 2: Send Email via Gmail

Gmail integration requires OAuth authentication, so you’ll need to authorize the application in your browser.

1

Create Gmail Instance

Create a Gmail MCP server instance

2

OAuth Authorization

Complete OAuth flow for Gmail access

3

Send Email

Use the agent to send an email

import os
from fireworks.client import Fireworks

EMAIL_SUBJECT = "Hello, World!"
EMAIL_BODY = "This is a test email sent using Fireworks AI and Klavis integration."
EMAIL_RECIPIENT = "recipient@example.com"  # Replace with your email

# 1. Initialize Fireworks AI client and Klavis API client
fireworks_client = Fireworks(api_key=os.getenv("FIREWORKS_API_KEY"))
klavis_api_client = KlavisAPI(api_key=os.getenv("KLAVIS_API_KEY"))

# 2. Create a Gmail MCP server using Klavis API
gmail_mcp_instance = klavis_api_client.create_mcp_instance(
    server_name="Gmail",
    user_id="1234",
    platform_name="Klavis",
)

# 3. Redirect to Gmail OAuth page
klavis_api_client.redirect_to_oauth(gmail_mcp_instance["instanceId"], "Gmail")

# 4. After OAuth authorization, create an agent with Gmail MCP server
agent = Agent(fireworks_client, klavis_api_client, gmail_mcp_instance["serverUrl"])

# 5. Send the email
response = agent.process_request(
    f"Send an email to {EMAIL_RECIPIENT} with subject {EMAIL_SUBJECT} and body {EMAIL_BODY}"
)

print(response)

Available Models

Fireworks AI offers various high-performance models you can use:

Qwen 2.5 72B

accounts/fireworks/models/qwen2p5-72b-instruct

Llama 3.1 405B

accounts/fireworks/models/llama-v3p1-405b-instruct

Mixtral 8x7B

accounts/fireworks/models/mixtral-8x7b-instruct

DeepSeek-V2.5

accounts/fireworks/models/deepseek-v2p5

Next Steps

Explore More MCP Servers

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

Try Different Fireworks Models

Experiment with various models like Llama, Mixtral, or Deepseek for different use cases

Build Multi-Server Workflows

Create sophisticated agents that combine Gmail + Slack + Notion for complete business automation

Production Deployment

Scale these patterns for production applications

Useful Resources

Happy building with Fireworks AI and Klavis! 🚀