Skip to main content

Prerequisites

Before we begin, you’ll need:

Fireworks AI API Key

Get your API key from Fireworks AI

Klavis AI API Key

Get your API key from Klavis AI

Installation

First, install the required packages:
pip install fireworks-ai klavis
npm install fireworks-ai klavis

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
// Set environment variables in your .env file
process.env.FIREWORKS_API_KEY = "your-fireworks-api-key-here";  // Replace with your actual Fireworks API key
process.env.KLAVIS_API_KEY = "your-klavis-api-key-here";       // Replace with your actual Klavis API key

Basic Setup

import os
import json
from fireworks.client import Fireworks
from klavis import Klavis
from klavis.types import McpServerName, ToolFormat

# Initialize clients
fireworks_client = Fireworks(api_key=os.getenv("FIREWORKS_API_KEY"))
klavis_client = Klavis(api_key=os.getenv("KLAVIS_API_KEY"))
import Fireworks from 'fireworks-ai';
import { KlavisClient, Klavis } from 'klavis';

// Initialize clients
const fireworksClient = new Fireworks({ apiKey: process.env.FIREWORKS_API_KEY });
const klavisClient = new KlavisClient({ apiKey: process.env.KLAVIS_API_KEY });

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
class Agent:
    def __init__(self, fireworks_client, klavis_client, mcp_server_url):
        self.fireworks = fireworks_client
        self.klavis = klavis_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):
        # 1. Get available tools
        mcp_tools = self.klavis.mcp_server.list_tools(
            server_url=self.mcp_server_url,
            format=ToolFormat.OPENAI
        )
        
        # 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=mcp_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 SDK
                tool_result = self.klavis.mcp_server.call_tools(
                    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": str(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
class Agent {
    private fireworks: Fireworks;
    private klavis: KlavisClient;
    private mcpServerUrl: string;
    private model: string;

    constructor(fireworksClient: Fireworks, klavisClient: KlavisClient, mcpServerUrl: string) {
        this.fireworks = fireworksClient;
        this.klavis = klavisClient;
        this.mcpServerUrl = mcpServerUrl;
        this.model = "accounts/fireworks/models/qwen2p5-72b-instruct";
        console.log(`🤖 Agent initialized with model: ${this.model}`);
    }

    async processRequest(userMessage: string) {
        // 1. Get available tools
        const mcpTools = await this.klavis.mcpServer.listTools({
            serverUrl: this.mcpServerUrl,
            format: Klavis.ToolFormat.Openai
        });

        // 2. Call LLM with tools
        const messages = [
            { role: "system", content: "You are a helpful assistant." },
            { role: "user", content: userMessage }
        ];

        const response = await this.fireworks.chat.completions.create({
            model: this.model,
            messages: messages,
            tools: mcpTools.tools
        });

        const assistantMessage = response.choices[0].message;
        messages.push(assistantMessage);

        // 3. If LLM wants to use tools
        if (assistantMessage.tool_calls) {
            // Execute each tool call
            for (const toolCall of assistantMessage.tool_calls) {
                const toolName = toolCall.function.name;
                const toolArgs = JSON.parse(toolCall.function.arguments);

                console.log(`🛠️ Calling tool: ${toolName} with args:`, toolArgs);
                // Call tool via Klavis SDK
                const toolResult = await this.klavis.mcpServer.callTools({
                    serverUrl: this.mcpServerUrl,
                    toolName: toolName,
                    toolArgs: toolArgs
                });

                messages.push({
                    role: "tool",
                    tool_call_id: toolCall.id,
                    content: JSON.stringify(toolResult)
                });
            }

            // 4. Get final response from LLM
            const finalResponse = await this.fireworks.chat.completions.create({
                model: this.model,
                messages: messages
            });
            return finalResponse.choices[0].message.content;
        }

        // If no tools needed, return the assistant message directly
        return assistantMessage.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
YOUTUBE_VIDEO_URL = "https://www.youtube.com/watch?v=kPXvf2-C_Hs"  # Pick a video you like!

# 1. Create YouTube MCP server instance
youtube_mcp_instance = klavis_client.mcp_server.create_server_instance(
    server_name=McpServerName.YOUTUBE,
    user_id="1234"
)

# 2. Create an agent with YouTube MCP server
agent = Agent(fireworks_client, klavis_client, youtube_mcp_instance.server_url)

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

print(response)
const YOUTUBE_VIDEO_URL = "https://www.youtube.com/watch?v=kPXvf2-C_Hs";  // Pick a video you like!

// 1. Create YouTube MCP server instance
const youtubeMcpInstance = await klavisClient.mcpServer.createServerInstance({
    serverName: Klavis.McpServerName.Youtube,
    userId: "1234"
});

// 2. Create an agent with YouTube MCP server
const agent = new Agent(fireworksClient, klavisClient, youtubeMcpInstance.serverUrl);

// 3. Process the request
const response = await agent.processRequest(
    `Summarize this YouTube video with timestamps: ${YOUTUBE_VIDEO_URL}`
);

console.log(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 webbrowser

# Create Gmail MCP server instance
gmail_mcp_instance = klavis_client.mcp_server.create_server_instance(
    server_name=McpServerName.GMAIL,
    user_id="1234"
)

# Redirect to Gmail OAuth page
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}")

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

# After OAuth authorization, create an agent with Gmail MCP server
agent = Agent(fireworks_client, klavis_client, gmail_mcp_instance.server_url)

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

print(response)
// Create Gmail MCP server instance
const gmailMcpInstance = await klavisClient.mcpServer.createServerInstance({
    serverName: Klavis.McpServerName.Gmail,
    userId: "1234"
});

// Redirect to Gmail OAuth page
console.log("🔐 Opening OAuth authorization for Gmail");
console.log(`If you are not redirected, please open the following URL in your browser: ${gmailMcpInstance.oauthUrl}`);
// In a web environment, you might redirect the user
window.open(gmailMcpInstance.oauthUrl);

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

// After OAuth authorization, create an agent with Gmail MCP server
const agent = new Agent(fireworksClient, klavisClient, gmailMcpInstance.serverUrl);

// Send the email
const response = await agent.processRequest(
    `Send an email to ${EMAIL_RECIPIENT} with subject ${EMAIL_SUBJECT} and body ${EMAIL_BODY}`
);

console.log(response);

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! 🚀