Skip to main content
Open In Colab

Gemini + Klavis AI Integration

This tutorial demonstrates how to use Google’s Gemini with function calling with Klavis MCP (Model Context Protocol) servers.

Prerequisites

Before we begin, you’ll need:

Installation

First, install the required packages:
pip install google-genai klavis

Full Code Examples

For complete working examples, check out the source code:

Setup Environment Variables

import os

# Set environment variables
os.environ["GEMINI_API_KEY"] = "YOUR_GEMINI_API_KEY"  # Replace with your actual Gemini API key
os.environ["KLAVIS_API_KEY"] = "YOUR_KLAVIS_API_KEY"  # Replace with your actual Klavis API key

Step 1 - Create Strata MCP Server with Gmail and Slack

from klavis import Klavis
from klavis.types import McpServerName, ToolFormat
import webbrowser

klavis_client = Klavis(api_key=os.getenv("KLAVIS_API_KEY"))

response = klavis_client.mcp_server.create_strata_server(
    servers=[McpServerName.GMAIL, McpServerName.SLACK], 
    user_id="1234"
)

# Handle OAuth authorization for each services
if response.oauth_urls:
    for server_name, oauth_url in response.oauth_urls.items():
        webbrowser.open(oauth_url)
        print(f"Or please open this URL to complete {server_name} OAuth authorization: {oauth_url}")
OAuth Authorization Required: The code above will open browser windows for each service. Click through the OAuth flow to authorize access to your accounts.

Step 2 - Create method to use MCP Server with Claude

This method handles multiple rounds of tool calls until a final response is ready, allowing the AI to chain tool executions for complex tasks.
import json
from google import genai
from google.genai import types

def gemini_with_mcp_server(mcp_server_url: str, user_query: str):
    gemini_client = genai.Client(api_key=os.getenv("GEMINI_API_KEY"))

    contents = []
    contents.append(types.Content(role="user", parts=[types.Part(text=user_query)]))

    mcp_server_tools = klavis_client.mcp_server.list_tools(
        server_url=mcp_server_url,
        format=ToolFormat.GEMINI
    )

    max_iterations = 10
    iteration = 0

    while iteration < max_iterations:
        iteration += 1

        response = gemini_client.models.generate_content(
            model='gemini-2.5-flash',
            contents=contents,
            config=types.GenerateContentConfig(tools=mcp_server_tools.tools)
        )

        if response.candidates and response.candidates[0].content.parts:
            contents.append(response.candidates[0].content)

            # Check if there are function calls to execute
            has_function_calls = False
            for part in response.candidates[0].content.parts:
                if hasattr(part, 'function_call') and part.function_call:
                    has_function_calls = True
                    function_name = part.function_call.name
                    function_args = dict(part.function_call.args)

                    print(f"Calling: {function_name}, with args: {function_args}")

                    result = klavis_client.mcp_server.call_tools(
                        server_url=mcp_server_url,
                        tool_name=function_name,
                        tool_args=function_args
                    )

                    function_response_part = types.Part.from_function_response(
                        name=function_name,
                        response={'result': result.result}
                    )
                    function_response_content = types.Content(
                        role='tool',
                        parts=[function_response_part]
                    )
                    contents.append(function_response_content)

            if has_function_calls:
                continue
            else:
                return response.text
        else:
            return "No response generated."

    return "Max iterations reached without final response"

Step 3 - Run!

result = gemini_with_mcp_server(
    mcp_server_url=response.strata_server_url,
    user_query="Check my latest 5 gmails and summarize them in a Slack message to #engineering"
)

print(f"\n🤖 Final Response: {result}")
Perfect! You’ve integrated Gemini with Klavis MCP servers.

Next Steps

Explore More MCP Servers

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

Multimodal Workflows

Build workflows that combine text, images, and other media

Production Deployment

Scale these patterns for production applications

Custom Integrations

Build custom MCP servers for your specific needs

Useful Resources

Happy building! 🚀
I