Skip to content

100+ AI Agents, Agent Skills and RAG Apps - Free and Open Source.

6.4 KB
Raw Blame
import asyncio
import os
import uuid
from textwrap import dedent
from agno.agent import Agent 
from agno.models.openai import OpenAIChat
from agno.tools.mcp import MultiMCPTools
from agno.db.sqlite import SqliteDb
from dotenv import load_dotenv

# Load environment variables
load_dotenv()

GITHUB_TOKEN = os.getenv("GITHUB_PERSONAL_ACCESS_TOKEN")
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
PERPLEXITY_API_KEY = os.getenv("PERPLEXITY_API_KEY")

async def main():
    print("\n" + "="*60)
    print("           πŸš€ Multi-MCP Intelligent Assistant πŸš€")
    print("="*60)
    print("πŸ”— Connected Services: GitHub β€’ Perplexity β€’ Calendar")
    print("πŸ’‘ Powered by OpenAI GPT-4o with Advanced Tool Integration")
    print("="*60 + "\n")
    
    # Validate required environment variables
    required_vars = {
        "GITHUB_PERSONAL_ACCESS_TOKEN": GITHUB_TOKEN,
        "OPENAI_API_KEY": OPENAI_API_KEY,
        "PERPLEXITY_API_KEY": PERPLEXITY_API_KEY,
    }
    
    missing_vars = [name for name, value in required_vars.items() if not value]
    if missing_vars:
        print("❌ ERROR: Missing required environment variables:")
        for var in missing_vars:
            print(f"   β€’ {var}")
        print("\nPlease check your .env file and ensure all required variables are set.")
        return
    
    # Generate unique user and session IDs for this terminal session
    user_id = f"user_{uuid.uuid4().hex[:8]}"
    session_id = f"session_{uuid.uuid4().hex[:8]}"
    print(f"πŸ‘€ User ID: {user_id}")
    print(f"πŸ”‘ Session ID: {session_id}")
    
    print("\nπŸ”Œ Initializing MCP server connections...\n")
    
    # Set up environment variables for MCP servers
    env = {
        **os.environ,
        "GITHUB_PERSONAL_ACCESS_TOKEN": GITHUB_TOKEN,
        "PERPLEXITY_API_KEY": PERPLEXITY_API_KEY
    }

    mcp_servers = [
        "npx -y @modelcontextprotocol/server-github",
        "npx -y @chatmcp/server-perplexity-ask",
        "npx @gongrzhe/server-calendar-autoauth-mcp",
        "npx @gongrzhe/server-gmail-autoauth-mcp"
    ]
    
    # Setup database for memory
    db = SqliteDb(db_file="tmp/multi_mcp_agent.db")
    
    # Start the MCP Tools session
    async with MultiMCPTools(mcp_servers, env=env) as mcp_tools:
        print("βœ… Successfully connected to all MCP servers!")
        
        # Create the agent with comprehensive instructions
        agent = Agent(
            name="MultiMCPAgent",
            model=OpenAIChat(id="gpt-4o", api_key=OPENAI_API_KEY),
            tools=[mcp_tools],
            description="Advanced AI assistant with GitHub, Perplexity, and Calendar integration",
            instructions=dedent(f"""
                You are an elite AI assistant with powerful integrations across multiple platforms. Your mission is to help users be incredibly productive across their digital workspace.

                🎯 CORE CAPABILITIES & INSTRUCTIONS:

                1. πŸ”§ TOOL MASTERY
                   β€’ You have DIRECT access to GitHub, Notion, Perplexity, and Calendar through MCP tools
                   β€’ ALWAYS use the appropriate MCP tool calls for any requests related to these platforms
                   β€’ Be proactive in suggesting powerful workflows and automations
                   β€’ Chain multiple tool calls together for complex tasks

                2. πŸ“‹ GITHUB EXCELLENCE
                   β€’ Repository management: create, clone, fork, search repositories
                   β€’ Issue & PR workflow: create, update, review, merge, comment
                   β€’ Code analysis: search code, review diffs, suggest improvements
                   β€’ Branch management: create, switch, merge branches
                   β€’ Collaboration: manage teams, reviews, and project workflows

                4. πŸ” PERPLEXITY RESEARCH
                   β€’ Real-time web search and research
                   β€’ Current events and trending information
                   β€’ Technical documentation and learning resources
                   β€’ Fact-checking and verification

                5. πŸ“… CALENDAR INTEGRATION
                   β€’ Event scheduling and management
                   β€’ Meeting coordination and availability
                   β€’ Deadline tracking and reminders

                6. 🎨 INTERACTION PRINCIPLES
                   β€’ Be conversational, helpful, and proactive
                   β€’ Explain what you're doing and why
                   β€’ Suggest follow-up actions and optimizations
                   β€’ Handle errors gracefully with alternative solutions
                   β€’ Ask clarifying questions when needed
                   β€’ Provide rich, formatted responses using markdown

                7. πŸš€ ADVANCED WORKFLOWS
                   β€’ Cross-platform automation (e.g., GitHub issues β†’ Notion tasks)
                   β€’ Research-driven development (Perplexity β†’ GitHub)
                   β€’ Project management integration
                   β€’ Documentation and knowledge sharing

                SESSION INFO:
                β€’ User ID: {user_id}
                β€’ Session ID: {session_id}
                β€’ Active Services: GitHub, Notion, Perplexity, Calendar

                REMEMBER: You're not just answering questions - you're a productivity multiplier. Think big, suggest workflows, and help users achieve more than they imagined possible!
            """),
            markdown=True,
            debug_mode=True,
            retries=3,
            db=db,
            enable_user_memories=True,
            add_history_to_context=True,
            num_history_runs=10,  # Increased for better context retention
        )
        
        print("\n" + "πŸŽ‰ " + "="*54 + " πŸŽ‰")
        print("   Multi-MCP Assistant is READY! Let's get productive!")
        print("πŸŽ‰ " + "="*54 + " πŸŽ‰\n")
        
        print("πŸ’‘ Try these example commands:")
        print("   β€’ 'Show my recent GitHub repositories'")
        print("   β€’ 'Search for the latest AI developments'")
        print("   β€’ 'Schedule a meeting for next week'")
        
        print("⚑ Type 'exit', 'quit', or 'bye' to end the session\n")
        
        # Start interactive CLI session
        await agent.acli_app(
            user_id=user_id,
            session_id=session_id,
            user="You",
            emoji="πŸ€–",
            stream=True,
            markdown=True,
            exit_on=["exit", "quit", "bye", "goodbye"]
        )

if __name__ == "__main__":
    asyncio.run(main())