4
4
TextContent ,
5
5
Tool ,
6
6
)
7
- import pika
8
7
import ssl
9
- from .models import Enqueue , Fanout
8
+ from .models import Enqueue , Fanout , ListQueues
10
9
from .logger import Logger , LOG_LEVEL
11
10
from .connection import RabbitMQConnection , validate_rabbitmq_name
12
- from .handlers import handle_enqueue , handle_fanout
11
+ from .handlers import handle_enqueue , handle_fanout , handle_list_queues
12
+ from .admin import RabbitMQAdmin
13
13
14
14
15
- async def serve (rabbitmq_host : str , port : int , username : str , password : str , use_tls : bool , log_level : str = LOG_LEVEL .DEBUG .name ) -> None :
15
+ async def serve (rabbitmq_host : str , port : int , username : str , password : str , use_tls : bool , log_level : str = LOG_LEVEL .DEBUG .name , api_port : int = 15671 ) -> None :
16
16
# Setup server
17
17
server = Server ("mcp-rabbitmq" )
18
18
# Setup logger
@@ -25,8 +25,6 @@ async def serve(rabbitmq_host: str, port: int, username: str, password: str, use
25
25
logger = Logger ("server.log" , log_level )
26
26
if is_log_level_exception :
27
27
logger .warning ("Wrong log_level received. Default to WARNING" )
28
- # Setup RabbitMQ connection
29
- rabbitmq = RabbitMQConnection (rabbitmq_host , port , username , password , use_tls )
30
28
31
29
@server .list_tools ()
32
30
async def list_tools () -> list [Tool ]:
@@ -40,6 +38,11 @@ async def list_tools() -> list[Tool]:
40
38
name = "fanout" ,
41
39
description = """Publish a message to an exchange with fanout type""" ,
42
40
inputSchema = Fanout .model_json_schema (),
41
+ ),
42
+ Tool (
43
+ name = "list_queues" ,
44
+ description = """List all the queues in the broker""" ,
45
+ inputSchema = ListQueues .model_json_schema (),
43
46
)
44
47
]
45
48
@@ -56,6 +59,8 @@ async def call_tool(
56
59
validate_rabbitmq_name (queue , "Queue name" )
57
60
58
61
try :
62
+ # Setup RabbitMQ connection
63
+ rabbitmq = RabbitMQConnection (rabbitmq_host , port , username , password , use_tls )
59
64
handle_enqueue (rabbitmq , queue , message )
60
65
return [TextContent (type = "text" , text = str ("suceeded" ))]
61
66
except Exception as e :
@@ -69,11 +74,22 @@ async def call_tool(
69
74
validate_rabbitmq_name (exchange , "Exchange name" )
70
75
71
76
try :
77
+ # Setup RabbitMQ connection
78
+ rabbitmq = RabbitMQConnection (rabbitmq_host , port , username , password , use_tls )
72
79
handle_fanout (rabbitmq , exchange , message )
73
80
return [TextContent (type = "text" , text = str ("suceeded" ))]
74
81
except Exception as e :
75
82
logger .error (f"{ e } " )
76
83
return [TextContent (type = "text" , text = str ("failed" ))]
84
+ elif name == "list_queues" :
85
+ try :
86
+ admin = RabbitMQAdmin (rabbitmq_host , api_port , username , password , use_tls )
87
+ result = handle_list_queues (admin )
88
+ return [TextContent (type = "text" , text = str (result ))]
89
+ except Exception as e :
90
+ logger .error (f"{ e } " )
91
+ return [TextContent (type = "text" , text = str ("failed" ))]
92
+ return [TextContent (type = "text" , text = str ("succeeded" ))]
77
93
raise ValueError (f"Tool not found: { name } " )
78
94
79
95
options = server .create_initialization_options ()
0 commit comments