Last updated

CrewAI - Frontegg AI for Python

The Frontegg AI Python SDK - frontegg-ai-sdk - connects your AI agent code to the Frontegg platform, allowing you to authenticate agents, set user context, and securely access tools and integrations like Slack, Google Workspace, or HubSpot.



Prerequisites

You’ll need:

  • Python 3.8+
  • A Frontegg account
  • Your Frontegg Client ID, Client Secret, and Agent ID

Python dependencies:

  • asyncio
  • httpx
  • anyio
  • nest_asyncio (for Jupyter or nested event loops)
  • pydantic
  • modelcontextprotocol
  • crewai (optional, for CrewAI integration)

Install the SDK

pip install frontegg-ai-sdk

Environment configuration

Create a .env file and populate it with the credentials from your Frontegg dashboard:

FRONTEGG_CLIENT_ID=your_client_id // Settings → Keys & domains
FRONTEGG_CLIENT_SECRET=your_client_secret // Settings → Domains tab (e.g., https://app-xxxx.frontegg.com)
FRONTEGG_AGENT_ID=your_agent_id // AI Agents → Agents → [Your Agent]

Supported environments

The SDK supports multiple Frontegg environments. Use the appropriate environment enum based on your data residency needs:

from frontegg_ai_python_sdk import Environment

# Available environments
Environment.EU  # European servers: '.frontegg.com'
Environment.US  # US servers: 'us.frontegg.com'
Environment.CA  # Canadian servers: 'ca.frontegg.com'
Environment.AU  # Australian servers: 'au.frontegg.com'
Environment.UK  # UK servers: 'uk.frontegg.com'

Project setup

Use your environment variables to set up the Frontegg AI client:

import asyncio
import os
from frontegg_ai_python_sdk import (
    Environment,
    FronteggAiClientConfig,
    FronteggAiClient
)

# Async usage
async def async_example():
    # Create client configuration
    config = FronteggAiClientConfig(
        environment=Environment.US,
        agent_id=os.environ.get("FRONTEGG_AGENT_ID"),
        client_id=os.environ.get("FRONTEGG_CLIENT_ID"),
        client_secret=os.environ.get("FRONTEGG_CLIENT_SECRET"),
    )

    # Create client
    client = FronteggAiClient(config)

    # Set Context manually
    tenant_id = os.getenv("FRONTEGG_TENANT_ID")
    user_id = os.getenv("FRONTEGG_USER_ID")
    client.set_context(tenant_id=tenant_id, user_id=user_id)

    # Or set the context using the user JWT
    user_jwt = "Bearer eyJ..."
    client.set_user_context_by_jwt(user_jwt)

    # List available tools
    tools = await client.list_tools()
    print(f"Available tools: {tools}")

    # Call a tool with arguments
    result = await client.call_tool(
        name="your_tool_name",
        arguments={"param1": "value1"},
    )
    print(f"Tool result: {result}")

if __name__ == "__main__":
    # Run async example
    asyncio.run(async_example())

CrewAI integration

You can integrate Frontegg AI tools directly into CrewAI agents for powerful multi-agent orchestration. Use the following example to get started:

import os
import asyncio
from crewai import Agent, Crew, Task
from frontegg_ai_python_sdk import (
    Environment,
    FronteggAiClientConfig,
    FronteggAiClient
)

async def get_crewai_tools():
    # Configure Frontegg client
    config = FronteggAiClientConfig(
        environment=Environment.US,
        agent_id=os.environ.get("FRONTEGG_AGENT_ID"),
        client_id=os.environ.get("FRONTEGG_CLIENT_ID"),
        client_secret=os.environ.get("FRONTEGG_CLIENT_SECRET"),
    )

    # Create client
    client = FronteggAiClient(config)

    # Set context manually
    tenant_id = os.getenv("FRONTEGG_TENANT_ID")
    user_id = os.getenv("FRONTEGG_USER_ID")
    client.set_context(tenant_id=tenant_id, user_id=user_id)

    # Or use a JWT token
    # user_jwt = "Bearer eyJ..."
    # client.set_user_context_by_jwt(user_jwt)

    # Get tools in CrewAI-compatible format
    tools = await client.list_tools_as_crewai_tools()
    return tools

# Fetch tools for the agent
tools = asyncio.run(get_crewai_tools())

# Create a CrewAI agent with Frontegg tools
agent = Agent(
    role="Research Assistant",
    goal="Find and analyze information",
    backstory="I am an AI research assistant with access to various tools.",
    tools=tools,
    verbose=True
)

# Define the task for the agent
task = Task(
    description="Research topic X and provide insights",
    expected_output="A comprehensive report",
    agent=agent
)

# Launch the crew
crew = Crew(agents=[agent], tasks=[task])
result = crew.kickoff()

Custom logging

To add detailed logging to your Frontegg client, use the built-in setup_logger utility:

from frontegg_ai_python_sdk import setup_logger
import logging

# Set up a custom logger
logger = setup_logger(
    name="my_custom_logger",
    level=logging.DEBUG,
    format_string="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
    file_handler="app.log"  # Optional: log to a file
)

# Initialize client with logger
client = FronteggAiClient(config, logger=logger)