Skip to content

PG1204/AI-Agent-Communication-Framework

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

9 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

AI Agent Communication Framework

A comprehensive framework for building, deploying, and orchestrating intelligent AI agents that can communicate with each other in real-time. Combines gRPC for high-performance agent-to-agent communication with a REST API layer and a modern Next.js dashboard for monitoring and interaction.

⚠️ Early Development Stage
This project is currently in active development and is being run and tested locally. All services default to localhost with environment-specific configurations. Production deployment documentation and guides (Docker, cloud platforms, remote hosts) will be added in upcoming releases. For now, follow the local development setup below.


πŸ“‹ Overview

This framework enables:

  • Multi-agent systems: Deploy multiple AI agents that communicate autonomously
  • Real-time messaging: Bi-directional gRPC streaming for low-latency message exchange
  • AI-powered agents: Built-in support for LLM integration (Groq API)
  • Agent orchestration: Coordinator agent that routes tasks between specialist agents
  • Web dashboard: Next.js UI for monitoring agents and sending messages
  • Persistent storage: PostgreSQL backend for message history and state
  • JWT authentication: Secure agent-to-agent and client-to-server communication

πŸ—οΈ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                      Agent Dashboard (Web UI)                    β”‚
β”‚                    Next.js + React + TypeScript                  β”‚
β”‚  - Chat interface                                                β”‚
β”‚  - Agent monitoring                                              β”‚
β”‚  - Real-time message updates (SSE)                               β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                           β”‚ REST API (axios)
                           β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    FastAPI REST Server                           β”‚
β”‚  - JWT token generation & verification                          β”‚
β”‚  - Message routing & persistence                                β”‚
β”‚  - Agent registry queries                                        β”‚
β”‚  - WebSocket/SSE endpoints                                       β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                           β”‚
        β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
        β”‚                  β”‚                  β”‚
        β–Ό                  β–Ό                  β–Ό
    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
    β”‚        gRPC Server (Port 50051)          β”‚
    β”‚  - AgentComm (bi-directional streaming) β”‚
    β”‚  - AgentRegistry (agent registration)   β”‚
    β”‚  - Database polling for message routing β”‚
    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
        β–²                  β”‚                  β–²
        β”‚                  β”‚                  β”‚
        β”‚ gRPC            β”‚ Postgres         β”‚ gRPC
        β”‚ Streaming        β”‚                  β”‚
        β”‚                  β–Ό                  β”‚
    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
    β”‚  Coordinator    Message      Agent Config/
    β”‚  Agent          Database     Metadata
    β”‚                            β”‚
    β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
    β”‚  β”‚ - Orchest tasks      β”‚ β”‚
    β”‚  β”‚ - Route to agents    β”‚ β”‚
    β”‚  β”‚ - Synthesize results β”‚ β”‚
    β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜              β”‚
        β–²                        β–Ό
        β”‚                   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
        β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Ίβ”‚ Research Agent β”‚
        β”‚                  β”‚ Research Agent β”‚
        β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Ίβ”‚ Code Agent     β”‚
        β”‚                  β”‚ etc...         β”‚
        β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ“ Project Structure

β”œβ”€β”€ server/                          # Python gRPC backend
β”‚   β”œβ”€β”€ main.py                     # gRPC server implementation
β”‚   β”œβ”€β”€ api.py                      # FastAPI REST API layer
β”‚   β”œβ”€β”€ auth.py                     # JWT token generation & verification
β”‚   β”œβ”€β”€ agent_comm_pb2.py          # Generated protobuf Python files
β”‚   └── agent_comm_pb2_grpc.py     # Generated gRPC service stubs
β”‚
β”œβ”€β”€ sdk/                            # Python Agent SDK
β”‚   β”œβ”€β”€ agent.py                   # Base AgentClient class
β”‚   β”œβ”€β”€ intelligent_agent.py       # AI-powered agent with LLM support
β”‚   β”œβ”€β”€ coordinator_agent.py       # Task orchestration agent
β”‚   β”œβ”€β”€ code_agent.py              # Code-focused specialist agent
β”‚   β”œβ”€β”€ research_agent.py          # Research/analysis specialist agent
β”‚   β”œβ”€β”€ demo_agent.py              # Demo example agent
β”‚   β”œβ”€β”€ smart_coordinator.py       # Advanced coordinator
β”‚   └── agent_comm_pb2*.py         # Generated protobuf files
β”‚
β”œβ”€β”€ agent-dashboard/                # Next.js web UI
β”‚   β”œβ”€β”€ app/
β”‚   β”‚   β”œβ”€β”€ page.tsx              # Home page
β”‚   β”‚   β”œβ”€β”€ login/                # Authentication
β”‚   β”‚   β”œβ”€β”€ chat/                 # Chat interface
β”‚   β”‚   β”œβ”€β”€ layout.tsx            # Root layout
β”‚   β”‚   β”œβ”€β”€ components/
β”‚   β”‚   β”‚   β”œβ”€β”€ ChatInterface.tsx    # Agent chat UI
β”‚   β”‚   β”‚   β”œβ”€β”€ SendMessageForm.tsx  # Message composer
β”‚   β”‚   β”‚   β”œβ”€β”€ ClientLayout.tsx     # Client-side layout
β”‚   β”‚   β”‚   └── ThemeToggle.tsx      # Dark mode toggle
β”‚   β”‚   β”œβ”€β”€ contexts/
β”‚   β”‚   β”‚   └── ThemeContext.tsx     # Theme provider
β”‚   β”‚   └── globals.css
β”‚   β”œβ”€β”€ services/
β”‚   β”‚   β”œβ”€β”€ api.ts                 # API client (axios)
β”‚   β”‚   └── index.ts
β”‚   β”œβ”€β”€ hooks/
β”‚   β”‚   └── useSSE.ts              # Server-Sent Events hook
β”‚   β”œβ”€β”€ package.json
β”‚   β”œβ”€β”€ tsconfig.json
β”‚   β”œβ”€β”€ next.config.ts
β”‚   └── tailwind.config.js
β”‚
β”œβ”€β”€ proto/
β”‚   └── agent_comm.proto           # gRPC service & message definitions
β”‚
└── requirements.txt               # Python dependencies

πŸ”Œ Core Concepts

Message Types

  • DIRECT: Point-to-point messages between agents
  • BROADCAST: One agent sends to all others
  • EVENT: Event notifications
  • REQUEST: Inter-agent request (for coordinator pattern)
  • RESPONSE: Reply to a request
  • HEARTBEAT: Keep-alive signal

Agent Roles

  1. Specialist Agents (code-agent, research-agent)

    • Focused expertise in a domain
    • Respond to direct requests
    • Can be chained by coordinator
  2. Coordinator Agent

    • Receives complex user requests
    • Analyzes which specialist(s) to route to
    • Synthesizes results into final response
    • Manages inter-agent communication
  3. Basic Agents

    • Simple message send/receive
    • No AI/LLM integration
    • Good for testing and light tasks

πŸš€ Quick Start

Prerequisites

1. Clone & Setup Environment

git clone https://github.com/PG1204/AI-Agent-Communication-Framework.git
cd "AI Agent Communication Framework"

# Setup Python
python -m venv venv
source venv/bin/activate  # or `venv\Scripts\activate` on Windows
pip install -r requirements.txt

# Setup Node.js
cd agent-dashboard
npm install
cd ..

2. Database Setup

# Create PostgreSQL database
createdb agent_comm_db

# Create schema (connect to database)
psql -d agent_comm_db -c "
CREATE TABLE IF NOT EXISTS agent_messages (
  message_id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  sender_id TEXT NOT NULL,
  recipient_id TEXT,
  message_type INTEGER NOT NULL,
  payload BYTEA,
  timestamp TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
  correlation_id TEXT
);

CREATE INDEX idx_recipient_timestamp ON agent_messages(recipient_id, timestamp);
CREATE INDEX idx_sender_timestamp ON agent_messages(sender_id, timestamp);
"

3. Configure Secrets

Create a .env file in the project root or export environment variables:

Note: Currently configured for local development (localhost). Production deployment configurations will be documented soon.

# Database
export AGENT_DB_USER="your_db_user"
export AGENT_DB_PASSWORD="your_db_password"
export AGENT_DB_HOST="localhost"
export AGENT_DB_PORT="5432"
export AGENT_DB_NAME="agent_comm_db"

# JWT
export JWT_SECRET="your-super-secret-key-change-this-in-production"
export JWT_ALGORITHM="HS256"
export JWT_EXPIRY_MINUTES="60"

# AI Integration
export GROQ_API_KEY="your-groq-api-key"

# API URLs
export NEXT_PUBLIC_API_URL="http://localhost:8000"
export GRPC_HOST="localhost:50051"

4. Start the Services

Terminal 1 - gRPC Server:

python server/main.py
# Expected: "πŸš€ Starting gRPC server on [::]:50051..."
# Expected: "βœ… Server started and ready!"

Terminal 2 - REST API (FastAPI):

uvicorn server.api:app --reload --port 8000
# Expected: "Uvicorn running on http://127.0.0.1:8000"

Terminal 3 - Dashboard:

cd agent-dashboard
npm run dev
# Expected: "β–² Next.js 15.5.4"
# Open: http://localhost:3000

Terminal 4+ - Launch Agents:

# Coordinator Agent (orchestrates tasks)
python sdk/coordinator_agent.py

# Research Agent (in another terminal)
python sdk/research_agent.py

# Code Agent (in another terminal)
python sdk/code_agent.py

πŸ“‘ API Reference

REST API Endpoints

Authentication

POST /token
{
  "agent_id": "my-agent"
}
Response:
{
  "access_token": "eyJ0...",
  "token_type": "bearer"
}

Send Message

POST /messages/send
Authorization: Bearer <token>
{
  "sender_id": "agent-1",
  "recipient_id": "agent-2",
  "message_type": 0,
  "payload": "Hello Agent 2!",
  "correlation_id": "optional-id"
}

Get Messages

GET /messages?agent_id=agent-1&limit=20&offset=0
Authorization: Bearer <token>

Get All Agents

GET /agents?agent_id=agent-1
Authorization: Bearer <token>

Get Conversation

GET /conversations/{other_agent_id}?agent_id=agent-1&limit=50
Authorization: Bearer <token>

gRPC API

See proto/agent_comm.proto for detailed service definitions:

AgentRegistry Service

rpc RegisterAgent(RegisterAgentRequest) returns (RegisterAgentResponse);

Registers a new agent and returns a JWT token.

AgentComm Service

rpc StreamMessages(stream AgentMessage) returns (stream AgentMessage);

Bi-directional streaming for real-time message exchange.

πŸ€– Creating Custom Agents

Simple Agent (No AI)

import asyncio
from sdk.agent import AgentClient

async def main():
    agent = AgentClient("my-simple-agent")
    
    # Register with server
    await agent.register_agent()
    
    # Send a direct message
    await agent.send_direct_message("recipient-agent-id", "Hello!")
    
    # Send broadcast
    await agent.send_broadcast_message("Hello everyone!")
    
    # Run agent (listen for incoming messages)
    await agent.run()

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

AI-Powered Agent

import asyncio
import os
from sdk.intelligent_agent import IntelligentAgent

SYSTEM_PROMPT = """You are a helpful assistant specialized in [your domain].
Focus on [specific capabilities].
Keep responses concise and actionable."""

async def main():
    agent = IntelligentAgent(
        agent_id="my-ai-agent",
        system_prompt=SYSTEM_PROMPT,
        groq_api_key=os.getenv("GROQ_API_KEY")
    )
    
    await agent.connect()

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

Coordinator Agent (Task Router)

import asyncio
import os
from sdk.intelligent_agent import IntelligentAgent

COORDINATOR_PROMPT = """You are a Coordinator Agent.
When you receive a request:
1. Analyze what needs to be done
2. Decide which specialist agent(s) to send it to
3. Send request using format: "REQUEST TO [agent-name]: [specific task]"
4. Wait for responses and synthesize them"""

async def main():
    agent = IntelligentAgent(
        agent_id="coordinator-agent",
        system_prompt=COORDINATOR_PROMPT,
        groq_api_key=os.getenv("GROQ_API_KEY")
    )
    
    await agent.connect()

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

πŸ” Authentication & Security

JWT Token Flow

  1. Agent Registration β†’ gRPC RegisterAgent() β†’ Returns JWT token
  2. Agent Connection β†’ Includes token in gRPC metadata: Authorization: Bearer <token>
  3. Message Sending β†’ REST API validates token via verify_jwt()
  4. Dashboard Login β†’ Same JWT flow for web users

Security Best Practices

  • Move JWT_SECRET to environment variables (never hardcode)
  • Use strong, randomly generated JWT secrets (32+ characters)
  • Rotate tokens regularly (currently 60-minute expiry)
  • Use HTTPS in production
  • Add database password from environment (not hardcoded)
  • Implement agent credential verification before registration
  • Add rate limiting to REST API endpoints
  • Use connection pooling with asyncpg (already implemented)

πŸ› οΈ Development

Regenerate Protocol Buffers

If you modify proto/agent_comm.proto:

python -m grpc_tools.protoc \
  -I./proto \
  --python_out=./server \
  --grpc_python_out=./server \
  ./proto/agent_comm.proto

# Copy to SDK for agents
cp server/agent_comm_pb2*.py sdk/

Run Tests

# Test agent registration
python sdk/test_auth_client.py

# Test messaging (after server is running)
python sdk/agent.py test-agent-1  # Terminal 1
python sdk/agent.py test-agent-2  # Terminal 2

Debugging

  • gRPC Server Logs: Watch server/main.py output for connection/message flow
  • API Server Logs: Check uvicorn console for HTTP requests
  • Agent Logs: Each agent prints its operations (connection, send, receive)
  • Dashboard: Check browser console (F12) for frontend errors

πŸ“Š Example Workflows

Scenario 1: Simple Agent-to-Agent Chat

1. Launch code-agent and research-agent
2. Open http://localhost:3000
3. Log in with agent ID
4. Select other agent from list
5. Send message β†’ both agents receive and respond

Scenario 2: Coordinator Routing Complex Task

User Request: "How do I build a REST API in Python and compare it with gRPC?"

Coordinator Flow:
1. Receives request from user/dashboard
2. Routes to code-agent: "Write example REST API in Python"
3. Routes to research-agent: "Compare REST vs gRPC"
4. Waits for both responses
5. Synthesizes final answer
6. Sends back to user via dashboard

Scenario 3: Real-time Monitoring

1. Multiple specialist agents running
2. Dashboard shows live agent status
3. Message history visible
4. Admin can send broadcast messages to all agents
5. View message flow and response times

🚨 Known Issues & Limitations

  1. Database credentials in source code (server/main.py, server/api.py)

    • Need to migrate to environment variables
  2. No persistent agent state beyond message history

    • Consider adding agent metadata table for status, capabilities
  3. No rate limiting on REST API

    • Consider adding FastAPI slowapi
  4. Groq API rate limits for AI agents

    • Currently has retry logic with exponential backoff
  5. No agent discovery service

    • Agents must be manually registered or hardcoded in coordinator
  6. WebSocket/SSE not fully implemented

    • Dashboard uses polling instead of true push updates
  7. No message encryption (TLS/SSL)

    • Add in production environments

πŸ“¦ Dependencies

Python (server & agents)

  • fastapi - REST API framework
  • uvicorn - ASGI server
  • grpcio - gRPC runtime
  • protobuf - Protocol buffers
  • asyncpg - Async PostgreSQL driver
  • pydantic - Data validation
  • python-jose - JWT implementation
  • groq - Groq API client
  • python-dotenv - Environment variable management

Node.js (dashboard)

  • next 15.5.4 - React framework
  • react 19.1.0 - UI library
  • axios - HTTP client
  • tailwindcss - Styling
  • lucide-react - Icons

🀝 Contributing

  1. Create a feature branch: git checkout -b feature/your-feature
  2. Commit changes: git commit -am "Add your feature"
  3. Push to branch: git push origin feature/your-feature
  4. Create a Pull Request

πŸ“ License

This project is open source. Specify your license here.

πŸ†˜ Support & Troubleshooting

"Connection refused" on port 50051

  • Ensure gRPC server is running: python server/main.py
  • Check if port is already in use: lsof -i :50051

"No such table: agent_messages"

  • Ensure PostgreSQL is running and database schema is created
  • Verify database connection settings in environment variables

"Invalid token" errors

  • Regenerate token via /token endpoint
  • Check JWT_SECRET matches between server/main.py and server/auth.py
  • Verify token expiration (60 minutes default)

Agent not receiving messages

  • Check agent is registered (should print "Registered with ID")
  • Verify recipient_id is correct
  • Check database for messages (they may be persisted but not routed)
  • Check gRPC server logs for routing errors

Dashboard shows "Connection error"

  • Verify REST API running on http://localhost:8000
  • Check browser CORS settings
  • Verify NEXT_PUBLIC_API_URL environment variable

Groq API rate limits

  • Wait a few minutes before retrying
  • Reduce max_tokens in IntelligentAgent (currently 500)
  • Consider caching responses for repeated queries

πŸ“š Further Reading


Created: December 2024
Repository: https://github.com/PG1204/AI-Agent-Communication-Framework
Branch: new-improvements

For issues and feature requests, please use the GitHub Issues page.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors