CrewAI has officially showcased their integration with Klavis AI in this LinkedIn post, demonstrating how to build powerful AI agent crews that can automate complex workflows across multiple platforms.
import os# Set environment variablesos.environ["OPENAI_API_KEY"] = "your-openai-api-key-here" # Replace with your actual OpenAI API keyos.environ["KLAVIS_API_KEY"] = "your-klavis-api-key-here" # Replace with your actual Klavis API key
CrewAI allows you to create specialized AI agent crews where each agent can have access to different MCP tools. This enables sophisticated multi-agent workflows that can:
Create MCP Instances: Set up connections to external services
Specialized Agents: Each agent focuses on specific tasks with relevant tools
Collaborative Workflows: Agents work together in sequential or parallel processes
Tool Discovery: Automatically discover available tools from MCP servers
Smart Coordination: CrewAI manages task dependencies and agent collaboration
Step 1 - Create Strata MCP Server with Gmail and Slack
Copy
Ask AI
from klavis import Klavisfrom klavis.types import McpServerName, ToolFormatimport webbrowserklavis_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 servicesif 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 Crew AI
This method handles multiple rounds of tool calls until a final response is ready, allowing the AI to chain tool executions for complex tasks.
Copy
Ask AI
import jsonfrom crewai import Agent, Task, Crew, Processfrom crewai_tools import MCPServerAdapterdef crew_with_mcp_server(mcp_server_url: str, user_query: str): klavis_server_params = [ { "url": mcp_server_url, "transport": "streamable-http" } ] with MCPServerAdapter(klavis_server_params) as all_mcp_tools: print(f"Available tools: {[tool.name for tool in all_mcp_tools]}") klavis_agent = Agent( role="Klavis Query Assistant", goal="Assist the user with their query using available tools", backstory="Expert at assisting users with their queries using available tools", tools=all_mcp_tools, verbose=False, llm="gpt-4o" ) klavis_task = Task( description=f"Answer the user's query: {user_query}", expected_output="Provide a detailed response to the user's query", agent=klavis_agent ) crew = Crew( agents = [klavis_agent], tasks = [klavis_task], process=Process.sequential, verbose=True ) result = crew.kickoff() return result
result = crew_with_mcp_server( mcp_server_url=response.strata_server_url, user_query="Check my latest 5 emails and summarize them in a Slack message to #general")print(f"\nFinal Response: {result}")
Perfect! You’ve integrated Crew with Strata MCP servers.
When using CrewAI with Klavis MCP servers, follow these security guidelines:
Copy
Ask AI
def create_secure_crew(): """Demonstrates secure MCP server integration with CrewAI""" # 1. Use environment variables for sensitive data api_key = os.getenv("KLAVIS_API_KEY") if not api_key: raise ValueError("KLAVIS_API_KEY environment variable is required") # 2. Validate server URLs (use HTTPS in production) server_params = [{ "url": server_instance.server_url, "transport": "streamable-http" }] # 3. Always use context managers for proper resource cleanup try: with MCPServerAdapter(server_params) as mcp_tools: # 4. Validate available tools before use if not mcp_tools: raise ValueError("No tools available from MCP server") print(f"✅ Securely connected with {len(mcp_tools)} tools") # 5. Create agents with limited scope agent = Agent( role="Data Analyst", goal="Analyze data within defined parameters", backstory="You operate within strict security guidelines.", tools=mcp_tools, reasoning=False, # Disable for production verbose=False # Disable verbose logging in production ) return agent except Exception as e: print(f"🔒 Security check failed: {e}") return None# Example usagesecure_agent = create_secure_crew()if secure_agent: print("✅ Secure crew created successfully")