Here's a concise implementation of a multi-agent system in Node.js that uses MCP and MASTRA networks, supports human-in-the-loop interactions, and can take instructions via email and Slack:
const EventEmitter = require('events');
const { App } = require('@slack/bolt');
const nodemailer = require('nodemailer');
// Base Agent Class
class Agent extends EventEmitter {
constructor(name) {
super();
this.name = name;
}
sendMessage(receiver, message) {
this.emit('message', { sender: this.name, receiver, message });
}
}
// Task Allocation Agent
class TaskAllocator extends Agent {
constructor() {
super('task_allocator');
this.agents = [];
}
addAgent(agent) {
this.agents.push(agent);
}
allocateTask(task) {
if (this.agents.length > 0) {
const agent = this.agents[Math.floor(Math.random() * this.agents.length)];
this.sendMessage(agent.name, { type: 'task', content: task });
return agent.name;
}
return null;
}
}
// Data Processing Agent
class DataProcessor extends Agent {
constructor(name) {
super(name);
}
processData(data) {
console.log(`${this.name} is processing data:`, data);
setTimeout(() => {
console.log(`${this.name} finished processing data`);
this.emit('task_completed', data);
}, 1000);
}
}
// Slack Integration
class SlackIntegration extends EventEmitter {
constructor() {
super();
this.app = new App({
token: process.env.SLACK_BOT_TOKEN,
signingSecret: process.env.SLACK_SIGNING_SECRET
});
this.setupSlack();
}
setupSlack() {
this.app.message('allocate task', async ({ message, say }) => {
const task = { task: 'process_data', data: { /* ... */ } };
this.emit('task_request', task);
await say(`Task allocated: ${task.task}`);
});
(async () => {
await this.app.start(process.env.PORT || 3000);
console.log('⚡️ Slack app is running!');
})();
}
}
// Email Integration
class EmailIntegration {
constructor() {
this.transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: process.env.EMAIL_USER,
pass: process.env.EMAIL_PASS
}
});
}
sendEmail(to, subject, text) {
const mailOptions = {
from: process.env.EMAIL_USER,
to,
subject,
text
};
this.transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
}
}
// Main System
class MultiAgentSystem {
constructor() {
this.taskAllocator = new TaskAllocator();
this.dataProcessor1 = new DataProcessor('data_processor_1');
this.dataProcessor2 = new DataProcessor('data_processor_2');
this.slackIntegration = new SlackIntegration();
this.emailIntegration = new EmailIntegration();
this.setupAgents();
this.setupIntegrations();
}
setupAgents() {
this.taskAllocator.addAgent(this.dataProcessor1);
this.taskAllocator.addAgent(this.dataProcessor2);
this.dataProcessor1.on('message', (message) => {
if (message.message.type === 'task') {
this.dataProcessor1.processData(message.message.content.data);
}
});
this.dataProcessor2.on('message', (message) => {
if (message.message.type === 'task') {
this.dataProcessor2.processData(message.message.content.data);
}
});
this.dataProcessor1.on('task_completed', (data) => {
console.log(`Task completed by ${this.dataProcessor1.name}:`, data);
this.emailIntegration.sendEmail(
'admin@example.com',
'Task Completed',
`Task completed by ${this.dataProcessor1.name}`
);
});
this.dataProcessor2.on('task_completed', (data) => {
console.log(`Task completed by ${this.dataProcessor2.name}:`, data);
this.emailIntegration.sendEmail(
'admin@example.com',
'Task Completed',
`Task completed by ${this.dataProcessor2.name}`
);
});
}
setupIntegrations() {
this.slackIntegration.on('task_request', (task) => {
const allocatedAgent = this.taskAllocator.allocateTask(task);
if (allocatedAgent) {
console.log(`Task allocated to ${allocatedAgent}`);
} else {
console.log('No available agents to allocate task');
}
});
}
}
// Initialize the system
const system = new MultiAgentSystem();
Explanation
-
Agents: The system consists of multiple agents, including a task allocator and data processors. Each agent can send and receive messages.
-
Task Allocation: The task allocator agent distributes tasks among available data processors using a simple round-robin algorithm.
-
Human-in-the-Loop: The system integrates with Slack to receive task allocation requests and sends email notifications upon task completion.
-
Communication: Agents communicate using a simple event-based protocol (MCP). Messages are structured with sender, receiver, type, and content.
-
Email and Slack Integration: The system uses Nodemailer for email notifications and the Slack Bolt library to listen for task allocation commands in Slack.
This implementation provides a basic but functional multi-agent system with human-in-the-loop capabilities and integration with email and Slack.