import { MCPTool, MCPInput } from "mcp-framework";
import { z } from "zod";
import * as amqp from "amqplib";
const ConnectionSchema = z.object({
url: z.string().describe("RabbitMQ connection URL (e.g., amqp://localhost:5672)"),
action: z.enum(["connect", "disconnect", "status"]).describe("Action to perform: connect, disconnect, or status"),
});
class RabbitMQConnection extends MCPTool {
name = "rabbitmq_connection";
description = "Manage RabbitMQ connections - connect, disconnect, or check status";
schema = ConnectionSchema;
private static connection: amqp.ChannelModel | null = null;
private static channel: amqp.Channel | null = null;
async execute(input: MCPInput<this>) {
try {
switch (input.action) {
case "connect":
if (RabbitMQConnection.connection) {
return "Already connected to RabbitMQ";
}
const conn = await amqp.connect(input.url);
const chan = await conn.createChannel();
RabbitMQConnection.connection = conn;
RabbitMQConnection.channel = chan;
return `Successfully connected to RabbitMQ at ${input.url}`;
case "disconnect":
if (!RabbitMQConnection.connection) {
return "No active RabbitMQ connection";
}
if (RabbitMQConnection.channel) {
await RabbitMQConnection.channel.close();
RabbitMQConnection.channel = null;
}
await RabbitMQConnection.connection.close();
RabbitMQConnection.connection = null;
return "Disconnected from RabbitMQ";
case "status":
const isConnected = RabbitMQConnection.connection !== null;
const hasChannel = RabbitMQConnection.channel !== null;
return {
connected: isConnected,
channel_ready: hasChannel,
status: isConnected ? "Connected" : "Disconnected"
};
default:
return "Invalid action. Use 'connect', 'disconnect', or 'status'";
}
} catch (error) {
return `Error: ${error instanceof Error ? error.message : 'Unknown error'}`;
}
}
static getChannel(): amqp.Channel | null {
return this.channel;
}
static getConnection(): amqp.ChannelModel | null {
return this.connection;
}
}
export default RabbitMQConnection;