Definition:
Agent-to-Agent (A2A) Protocol is a communication framework that enables autonomous software agents to interact, negotiate, and collaborate directly with each other without human intervention. These protocols define how agents discover each other, exchange information, coordinate tasks, and work together to achieve individual or collective goals in distributed systems.
Key Properties:
- Autonomous communication: Agents interact independently
- Message-based: Structured communication protocols
- Goal-oriented: Agents work toward specific objectives
- Negotiation capable: Can bargain and reach agreements
- Distributed coordination: No central controller required
Core Components:
- Agent Discovery: Finding other agents in the network
- Message Exchange: Structured communication format
- Negotiation Protocol: Bargaining and agreement mechanisms
- Task Coordination: Collaborative work distribution
- Trust and Security: Authentication and secure communication
Example:
Multiple delivery drones need to coordinate package deliveries without human management
class DroneAgent {
constructor(id, initialLocation, battery) {
this.id = id;
this.location = initialLocation;
this.battery = battery;
this.capabilities = ['delivery', 'surveillance', 'emergency'];
this.activeDeliveries = [];
this.network = new AgentNetwork();
this.messageHandler = new A2AMessageHandler(this);
// Register with agent network
this.network.registerAgent(this);
}
// A2A Protocol: Delivery Negotiation
async handleNewDelivery(delivery) {
console.log(`🤖 ${this.id}: New delivery request received`);
// Step 1: Broadcast delivery opportunity to other agents
const announcement = {
type: 'DELIVERY_OPPORTUNITY',
from: this.id,
delivery: delivery,
timestamp: Date.now()
};
await this.network.broadcast(announcement);
// Step 2: Collect bids from other agents
const bids = await this.collectBids(delivery, 5000); // 5 second timeout
// Step 3: Include own bid
const myBid = this.calculateBid(delivery);
bids.push({ agent: this.id, bid: myBid, details: this.getBidDetails(delivery) });
// Step 4: Negotiate and select best agent
const winner = await this.conductAuction(bids, delivery);
if (winner.agent === this.id) {
console.log(`🏆 ${this.id}: Won delivery auction - executing`);
await this.executeDelivery(delivery);
} else {
console.log(`📤 ${this.id}: Delegating delivery to ${winner.agent}`);
await this.delegateDelivery(winner.agent, delivery);
}
}
async receiveMessage(message) {
switch (message.type) {
case 'DELIVERY_OPPORTUNITY':
await this.handleDeliveryOpportunity(message);
break;
case 'BID_REQUEST':
await this.handleBidRequest(message);
break;
case 'AUCTION_RESULT':
await this.handleAuctionResult(message);
break;
case 'COLLABORATION_REQUEST':
await this.handleCollaborationRequest(message);
break;
default:
console.log(`❓ ${this.id}: Unknown message type: ${message.type}`);
}
}
async handleDeliveryOpportunity(message) {
const delivery = message.delivery;
// Calculate if this delivery is suitable for me
const suitability = this.assessDeliverySuitability(delivery);
if (suitability.canHandle) {
console.log(`💰 ${this.id}: Submitting bid for delivery`);
const bid = {
type: 'DELIVERY_BID',
from: this.id,
to: message.from,
deliveryId: delivery.id,
bid: suitability.bid,
estimatedTime: suitability.time,
confidence: suitability.confidence
};
await this.network.sendMessage(message.from, bid);
} else {
console.log(`🚫 ${this.id}: Cannot handle delivery - ${suitability.reason}`);
}
}
assessDeliverySuitability(delivery) {
const distance = this.calculateDistance(this.location, delivery.destination);
const batteryRequired = distance * 2; // Round trip
const timeEstimate = distance * 10; // 10 minutes per unit
if (this.battery < batteryRequired) {
return { canHandle: false, reason: 'Insufficient battery' };
}
if (this.activeDeliveries.length >= 3) {
return { canHandle: false, reason: 'Too busy' };
}
// Calculate bid (lower is better)
const bid = distance + (100 - this.battery) + (this.activeDeliveries.length * 10);
return {
canHandle: true,
bid: bid,
time: timeEstimate,
confidence: Math.max(0, 100 - distance * 5)
};
}
async conductAuction(bids, delivery) {
console.log(`🎯 ${this.id}: Conducting auction with ${bids.length} bids`);
// Sort bids by score (lower is better)
bids.sort((a, b) => a.bid - b.bid);
const winner = bids[0];
// Notify all participants of auction result
const result = {
type: 'AUCTION_RESULT',
from: this.id,
deliveryId: delivery.id,
winner: winner.agent,
winningBid: winner.bid
};
for (const bid of bids) {
if (bid.agent !== this.id) {
await this.network.sendMessage(bid.agent, result);
}
}
return winner;
}
// A2A Protocol: Collaborative Task Execution
async requestCollaboration(task, requiredCapabilities) {
console.log(`🤝 ${this.id}: Requesting collaboration for task: ${task.type}`);
const collaborationRequest = {
type: 'COLLABORATION_REQUEST',
from: this.id,
task: task,
requiredCapabilities: requiredCapabilities,
deadline: Date.now() + task.timeLimit
};
const responses = await this.network.broadcastAndCollect(
collaborationRequest,
'COLLABORATION_RESPONSE',
10000 // 10 second timeout
);
// Form collaboration team
const team = this.selectCollaborationTeam(responses, requiredCapabilities);
if (team.length > 0) {
console.log(`👥 ${this.id}: Formed team: ${team.map(t => t.agent).join(', ')}`);
await this.coordinateTeamExecution(task, team);
} else {
console.log(`😞 ${this.id}: No suitable collaborators found`);
}
}
async handleCollaborationRequest(message) {
const task = message.task;
const capabilities = message.requiredCapabilities;
// Check if I can contribute
const myCapabilities = this.getAvailableCapabilities();
const canContribute = capabilities.some(cap => myCapabilities.includes(cap));
if (canContribute && this.isAvailable()) {
const response = {
type: 'COLLABORATION_RESPONSE',
from: this.id,
to: message.from,
taskId: task.id,
availableCapabilities: myCapabilities,
availability: this.getAvailabilityWindow(),
cost: this.calculateCollaborationCost(task)
};
await this.network.sendMessage(message.from, response);
console.log(`✋ ${this.id}: Offered to collaborate on task ${task.id}`);
}
}
}
class A2ADeliverySystem {
constructor() {
this.network = new AgentNetwork();
this.agents = [];
}
addDrone(id, location, battery) {
const drone = new DroneAgent(id, location, battery);
this.agents.push(drone);
return drone;
}
async processDelivery(delivery) {
console.log(`📦 New delivery: ${delivery.id} to ${delivery.destination}`);
// Any available agent can initiate the delivery process
const availableAgents = this.agents.filter(agent => agent.isAvailable());
if (availableAgents.length > 0) {
// Let the first available agent coordinate
await availableAgents[0].handleNewDelivery(delivery);
} else {
console.log('❌ No agents available to coordinate delivery');
}
}
}
// Usage Example
async function demonstrateA2A() {
const system = new A2ADeliverySystem();
// Add drone agents
const drone1 = system.addDrone('Alpha', [0, 0], 90);
const drone2 = system.addDrone('Beta', [3, 4], 70);
const drone3 = system.addDrone('Gamma', [6, 2], 85);
console.log('🚁 Drone fleet initialized with A2A protocol');
// Process deliveries autonomously
const deliveries = [
{ id: 'DEL001', destination: [2, 3], weight: 1.5, priority: 'high' },
{ id: 'DEL002', destination: [8, 1], weight: 0.8, priority: 'normal' },
{ id: 'DEL003', destination: [1, 6], weight: 2.1, priority: 'urgent' }
];
for (const delivery of deliveries) {
await system.processDelivery(delivery);
await new Promise(resolve => setTimeout(resolve, 1000)); // Brief pause
}
}
demonstrateA2A();