import { MCPTool, MCPInput } from "mcp-framework";
import { z } from "zod";
import RabbitMQConnection from "./RabbitMQConnection.js";
const ExchangeSchema = z.object({
action: z.enum(["create", "delete"]).describe("Action to perform on the exchange"),
exchange_name: z.string().describe("Name of the exchange"),
exchange_type: z.enum(["direct", "topic", "fanout", "headers"]).optional().describe("Type of exchange (default: direct)"),
durable: z.boolean().optional().describe("Whether the exchange should survive server restarts (default: true)"),
auto_delete: z.boolean().optional().describe("Whether the exchange should be deleted when no queues bound (default: false)"),
});
class ExchangeManagement extends MCPTool {
name = "exchange_management";
description = "Manage RabbitMQ exchanges - create or delete";
schema = ExchangeSchema;
async execute(input: MCPInput<this>) {
const channel = RabbitMQConnection.getChannel();
if (!channel) {
return "Error: No active RabbitMQ connection. Please connect first.";
}
try {
switch (input.action) {
case "create":
const options = {
durable: input.durable ?? true,
autoDelete: input.auto_delete ?? false,
};
await channel.assertExchange(
input.exchange_name,
input.exchange_type ?? "direct",
options
);
return {
message: `Exchange '${input.exchange_name}' created successfully`,
exchange: input.exchange_name,
type: input.exchange_type ?? "direct",
options
};
case "delete":
await channel.deleteExchange(input.exchange_name);
return `Exchange '${input.exchange_name}' deleted successfully`;
default:
return "Invalid action. Use 'create' or 'delete'";
}
} catch (error) {
return `Error: ${error instanceof Error ? error.message : 'Unknown error'}`;
}
}
}
export default ExchangeManagement;