Skip to content

sobhan2204/Agentic_AI

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

33 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿค– Agentic AI Multi-Tool Assistant

Python LangChain FastAPI

An intelligent multi-agent AI system powered by LangChain + LangGraph + MCP (Model Context Protocol) with ReAct-style reasoning. Integrates 5 specialized tool servers (Math, Weather, Translation, Web Search, Gmail) with persistent FAISS memory and FastAPI web interface.


โœจ Key Features

๐Ÿง  Intelligent Agent Architecture

  • ReAct Loop (Reasoning + Acting): LLM autonomously decides when to use tools through Thinkโ†’Actโ†’Observe cycles
  • Native Tool Calling: Zero hardcoded routing - agent interprets tool schemas and chooses appropriate tools
  • Multi-Step Task Handling: Automatically chains tool calls for complex queries (e.g., "search AI news and email results")
  • Duplicate Call Prevention: Built-in loop detection prevents stuck tool calls

๐Ÿ› ๏ธ 5 MCP Tool Servers

Tool Capabilities Transport
๐Ÿงฎ Math Server Symbolic math (derivatives, integrals, equation solving), calculator stdio
๐ŸŒฆ๏ธ Weather Current weather, air quality via Open-Meteo API stdio
๐ŸŒ Translate Multi-language translation via Google Translate API stdio
๐Ÿ” Web Search Real-time web search powered by Tavily API stdio
๐Ÿ“ง Gmail Send emails, read inbox via Gmail API stdio

๐Ÿ’พ Persistent Memory System

  • FAISS Vector Database: Stores conversation history as semantic embeddings
  • Context Retrieval: Automatically fetches top 3 relevant past interactions
  • Conversation Summarization: Summarizes long conversations every 6+ exchanges
  • Cross-Session Memory: Maintains context between chat sessions

๐ŸŒ Web Interface

  • FastAPI Backend: RESTful API with CORS support
  • Chat Endpoint: /chat - Process user messages with full agent capabilities
  • Clear History: /clear - Reset conversation memory
  • Health Check: /health - Monitor system status

๐Ÿ”ง Production Features

  • API Key Rotation: Supports 3 Groq API keys for rate limit handling
  • Retry Logic: Automatic retry with exponential backoff for rate limits
  • Error Handling: Graceful degradation with timeout protection (60s per tool)
  • Truncation: Smart result truncation to prevent token overflow

๐Ÿ—๏ธ System Architecture

graph TB
    User[๐Ÿ‘ค User Input] --> Client[๐Ÿ’ฌ Client Interface<br/>Terminal or Web API]
    
    Client --> AgentLLM{๐Ÿง  ReAct Agent<br/>LLaMA-3.1-8B}
    
    AgentLLM -->|"Think: Need tool?"| Decision{Decision}
    Decision -->|No tool needed| DirectReply[๐Ÿ“ Direct Response]
    Decision -->|Tool needed| ToolCall[๐Ÿ”ง Tool Execution]
    
    ToolCall --> MCPServers[๐Ÿ› ๏ธ MCP Tool Servers]
    
    MCPServers --> MathTool[๐Ÿงฎ Math Server<br/>sympy + scipy]
    MCPServers --> WeatherTool[๐ŸŒฆ๏ธ Weather<br/>Open-Meteo API]
    MCPServers --> TranslateTool[๐ŸŒ Translate<br/>Google Translate]
    MCPServers --> SearchTool[๐Ÿ” Web Search<br/>Tavily API]
    MCPServers --> GmailTool[๐Ÿ“ง Gmail<br/>Gmail API]
    
    ToolCall -->|Result| Observe[๐Ÿ‘๏ธ Observe Result]
    Observe -->|Add to context| AgentLLM
    
    AgentLLM -->|Final answer| Response[โœ… Final Response]
    
    Response --> Memory[(๐Ÿง  FAISS Memory<br/>HuggingFace Embeddings)]
    DirectReply --> Memory
    
    Memory -->|Retrieve context| AgentLLM
    
    Response --> User
    DirectReply --> User
    
    subgraph "ReAct Loop (Max 6 iterations)"
        AgentLLM
        Decision
        ToolCall
        Observe
    end
    
    subgraph "FastAPI Server (Optional)"
        API[๐ŸŒ Web Interface<br/>Port 8080]
    end
    
    User -.->|HTTP POST| API
    API -.-> Client
    
    style AgentLLM fill:#ff9999
    style Memory fill:#ffcc99
    style MCPServers fill:#e6b3ff
    style Decision fill:#99ff99
Loading

How It Works

  1. Input Processing: User sends query via CLI or API
  2. ReAct Loop: Agent enters Thinkโ†’Actโ†’Observe cycle:
    • Think: Decides if a tool is needed based on query
    • Act: Calls appropriate tool(s) with generated arguments
    • Observe: Processes tool output and decides next action
  3. Memory Integration: FAISS retrieves relevant past context automatically
  4. Response Generation: Agent synthesizes final answer after โ‰ค6 tool calls
  5. Memory Update: Conversation stored as embeddings for future retrieval

๐Ÿ“‚ Project Structure

agentic-ai-mcp/
โ”œโ”€โ”€ client.py                 # Main CLI chat interface (ReAct agent)
โ”œโ”€โ”€ server.py                 # FastAPI web server
โ”œโ”€โ”€ intent_router.py          # LLM-based query understanding
โ”œโ”€โ”€ executioner.py            # Tool execution pipeline
โ”œโ”€โ”€ rule_based_verifier.py    # Response validation logic
โ”‚
โ”œโ”€โ”€ Tool Servers (MCP)
โ”‚   โ”œโ”€โ”€ mathserver.py         # Math calculations (sympy, scipy)
โ”‚   โ”œโ”€โ”€ weather.py            # Weather & air quality
โ”‚   โ”œโ”€โ”€ translate.py          # Language translation
โ”‚   โ”œโ”€โ”€ websearch.py          # Tavily web search
โ”‚   โ””โ”€โ”€ gmail.py              # Gmail send/read
โ”‚
โ”œโ”€โ”€ Utilities
โ”‚   โ”œโ”€โ”€ debug_script.py       # Test MCP server connectivity
โ”‚   โ”œโ”€โ”€ test_servers.py       # Validate individual servers
โ”‚   โ””โ”€โ”€ personalized_task.py  # Custom workflow placeholder
โ”‚
โ”œโ”€โ”€ Configuration
โ”‚   โ”œโ”€โ”€ .env                  # API keys (not in repo)
โ”‚   โ”œโ”€โ”€ requirment.txt        # Python dependencies
โ”‚   โ”œโ”€โ”€ pyproject.toml        # Project metadata
โ”‚   โ””โ”€โ”€ .gitignore
โ”‚
โ”œโ”€โ”€ Memory Storage
โ”‚   โ””โ”€โ”€ faiss_index/
โ”‚       โ””โ”€โ”€ index.faiss       # Vector embeddings
โ”‚
โ”œโ”€โ”€ Auth (not in repo)
โ”‚   โ”œโ”€โ”€ credentials.json      # Google OAuth credentials
โ”‚   โ””โ”€โ”€ token.json            # Gmail access token
โ”‚
โ””โ”€โ”€ README.md

โš™๏ธ Installation & Setup

1๏ธโƒฃ Prerequisites

  • Python 3.13+
  • Git
  • API Keys:
    • Groq API (for LLaMA models)
    • Tavily API (for web search)
    • Google Cloud (for Gmail - optional)

2๏ธโƒฃ Clone Repository

git clone https://github.com/sobhan2204/agentic-ai-mcp.git
cd agentic-ai-mcp

3๏ธโƒฃ Install Dependencies

pip install -r requirment.txt

4๏ธโƒฃ Configure Environment

Create a .env file:

# Groq API Keys (get from https://console.groq.com)
GROQ_API_KEY_1=gsk_xxxxxxxxxxxxxxxxxxxxx
GROQ_API_KEY_2=gsk_xxxxxxxxxxxxxxxxxxxxx  # Optional for rotation
GROQ_API_KEY_3=gsk_xxxxxxxxxxxxxxxxxxxxx  # Optional

# Tavily API (get from https://tavily.com)
TAVILY_API_KEY=tvly-xxxxxxxxxxxxxxxx

5๏ธโƒฃ Gmail Setup (Optional)

If using Gmail features:

  1. Create OAuth credentials at Google Cloud Console
  2. Download as credentials.json
  3. First run will open browser for authentication
  4. Token saved as token.json for future use

๐Ÿš€ Usage

Option 1: Terminal Chat (Recommended)

python client.py

Example Interaction:

You: what is 5 + 5
  [Tool: solve_math({"query": "5 + 5"})]
Assistant: The result is 10.

You: translate "good morning" to french
  [Tool: translate({"sentence": "good morning", "target_language": "french"})]
Assistant: "Good morning" in French is "bonjour".

You: search latest AI news and email to bob@test.com
  [Tool: search_web({"query": "latest AI news"})]
  [Tool: send_email({"recipient": "bob@test.com", "subject": "AI Update", "body": "..."})]
Assistant: I found recent AI news and sent it to bob@test.com.

You: clear                     # Reset conversation memory
Conversation cleared.

You: exit
Bye bye!

Option 2: Web API

Start the server:

python server.py

API Endpoints:

# Send chat message
curl -X POST http://localhost:8080/chat \
  -H "Content-Type: application/json" \
  -d '{"message": "what is the weather in London?"}'

# Clear conversation
curl -X POST http://localhost:8080/clear

# Health check
curl http://localhost:8080/health

๐Ÿงช Test MCP Servers

Before running the main client, verify all tools work:

python test_servers.py

Expected output:

Testing mathserver.py... โœ… WORKING
Testing weather.py... โœ… WORKING
Testing translate.py... โœ… WORKING
Testing websearch.py... โœ… WORKING
Testing gmail.py... โœ… WORKING

๐ŸŽฏ Example Use Cases

Query Tools Used Result
"solve x^2 - 4 = 0" solve_math Solutions: x = -2, 2
"weather in Tokyo" get_weather Current weather with temperature
"translate 'hello' to spanish" translate "hola"
"latest news on AI" search_web Top 3 news articles with sources
"send email to alice@test.com" send_email Email sent confirmation
"search AI news and email it to bob@test.com" search_web โ†’ send_email Multi-step: search then email

๐Ÿ› ๏ธ Configuration Options

Memory Settings

Edit in client.py:

SUMMARIZE_AFTER = 6   # Summarize every N user-assistant exchanges
MAX_REACT_STEPS = 6   # Max tool calls per turn
TOOL_TIMEOUT    = 60  # Seconds before tool call times out

Model Selection

Edit in client.py:

agent_llm = ChatGroq(
    model="llama-3.1-8b-instant",  # Fast for tool calling
    max_tokens=1024,
    temperature=0.0,                # Deterministic for reliability
)

API Key Rotation

The system automatically cycles through 3 API keys to handle rate limits. Configure in .env.


๐Ÿงน Commands

Command Action
clear Reset FAISS memory and conversation history
exit / quit / q Exit the chat
Any other text Process as user query

๐Ÿค Contributing

Contributions welcome! To add a new tool:

  1. Create newtool.py following MCP FastMCP format
  2. Register in client.py MultiServerMCPClient config
  3. Test with test_servers.py
  4. Update this README

๐Ÿ“ Tech Stack

Component Technology
Agent Framework LangChain + LangGraph
LLM Groq (LLaMA-3.1-8B-Instant)
Tool Protocol MCP (FastMCP)
Vector DB FAISS
Embeddings HuggingFace (all-MiniLM-L6-v2)
Web Framework FastAPI
Math Engine SymPy + SciPy
Translation deep-translator (Google Translate)
Search Tavily API
Email Gmail API (OAuth2)

Acknowledgments


Made with โค๏ธ by Sobhan

About

Agentic_AI is a multi-agent orchestration framework that combines different AI tools and services for rich, document- and context-aware assistance. It integrates LangChain + LangGraph + MCP, powered by a large model (LLaMA-3-70B via Groq), along with specialized agents for mathematical tasks, translation, Gmail interactions, web search, and persist

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors