6
6
)
7
7
import ssl
8
8
import logging
9
- from .models import (
10
- Enqueue ,
11
- Fanout ,
12
- ListQueues ,
13
- ListExchanges ,
14
- GetQueueInfo ,
15
- DeleteQueue ,
16
- PurgeQueue ,
17
- DeleteExchange ,
18
- GetExchangeInfo
19
- )
20
9
from .connection import RabbitMQConnection , validate_rabbitmq_name
21
10
from .handlers import (
22
11
handle_enqueue ,
30
19
handle_get_exchange_info
31
20
)
32
21
from .admin import RabbitMQAdmin
22
+ from .tools import MCP_TOOLS
33
23
34
-
35
- async def serve (rabbitmq_host : str , port : int , username : str , password : str , use_tls : bool , log_level : str = "INFO" , api_port : int = 15671 ) -> None :
24
+ async def serve (rabbitmq_host : str , port : int , username : str , password : str , use_tls : bool , api_port : int = 15671 ) -> None :
36
25
# Setup server
37
26
server = Server ("mcp-rabbitmq" )
38
27
# Setup logger
39
28
logger = logging .getLogger ("mcp-rabbitmq" )
40
- logger .setLevel (log_level )
41
- # Configure logging with timestamp and file output
29
+ logger .setLevel (logging .INFO )
42
30
logging .basicConfig (
43
31
format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s' ,
44
32
handlers = [
@@ -49,53 +37,7 @@ async def serve(rabbitmq_host: str, port: int, username: str, password: str, use
49
37
50
38
@server .list_tools ()
51
39
async def list_tools () -> list [Tool ]:
52
- return [
53
- Tool (
54
- name = "enqueue" ,
55
- description = """Enqueue a message to a queue hosted on RabbitMQ""" ,
56
- inputSchema = Enqueue .model_json_schema (),
57
- ),
58
- Tool (
59
- name = "fanout" ,
60
- description = """Publish a message to an exchange with fanout type""" ,
61
- inputSchema = Fanout .model_json_schema (),
62
- ),
63
- Tool (
64
- name = "list_queues" ,
65
- description = """List all the queues in the broker""" ,
66
- inputSchema = ListQueues .model_json_schema (),
67
- ),
68
- Tool (
69
- name = "list_exchanges" ,
70
- description = """List all the exchanges in the broker""" ,
71
- inputSchema = ListExchanges .model_json_schema (),
72
- ),
73
- Tool (
74
- name = "get_queue_info" ,
75
- description = """Get detailed information about a specific queue""" ,
76
- inputSchema = GetQueueInfo .model_json_schema (),
77
- ),
78
- Tool (
79
- name = "delete_queue" ,
80
- description = """Delete a specific queue""" ,
81
- inputSchema = DeleteQueue .model_json_schema (),
82
- ),
83
- Tool (
84
- name = "purge_queue" ,
85
- description = """Remove all messages from a specific queue""" ,
86
- inputSchema = PurgeQueue .model_json_schema (),
87
- ),
88
- Tool (
89
- name = "delete_exchange" ,
90
- description = """Delete a specific exchange""" ,
91
- inputSchema = DeleteExchange .model_json_schema (),
92
- ),
93
- Tool (
94
- name = "get_exchange_info" ,
95
- description = """Get detailed information about a specific exchange""" ,
96
- inputSchema = GetExchangeInfo .model_json_schema (),
97
- )
98
- ]
40
+ return MCP_TOOLS
99
41
100
42
@server .call_tool ()
101
43
async def call_tool (
@@ -106,17 +48,15 @@ async def call_tool(
106
48
logger .debug ("Executing enqueue tool" )
107
49
message = arguments ["message" ]
108
50
queue = arguments ["queue" ]
109
-
110
51
validate_rabbitmq_name (queue , "Queue name" )
111
-
112
52
try :
113
53
# Setup RabbitMQ connection
114
54
rabbitmq = RabbitMQConnection (rabbitmq_host , port , username , password , use_tls )
115
55
handle_enqueue (rabbitmq , queue , message )
116
56
return [TextContent (type = "text" , text = str ("suceeded" ))]
117
57
except Exception as e :
118
58
logger .error (f"{ e } " )
119
- return [TextContent (type = "text" , text = str ("failed" ))]
59
+ return [TextContent (type = "text" , text = str (f "failed: { e } " ))]
120
60
elif name == "fanout" :
121
61
logger .debug ("Executing fanout tool" )
122
62
message = arguments ["message" ]
@@ -131,24 +71,23 @@ async def call_tool(
131
71
return [TextContent (type = "text" , text = str ("suceeded" ))]
132
72
except Exception as e :
133
73
logger .error (f"{ e } " )
134
- return [TextContent (type = "text" , text = str ("failed" ))]
74
+ return [TextContent (type = "text" , text = str (f "failed: { e } " ))]
135
75
elif name == "list_queues" :
136
76
try :
137
77
admin = RabbitMQAdmin (rabbitmq_host , api_port , username , password , use_tls )
138
78
result = handle_list_queues (admin )
139
79
return [TextContent (type = "text" , text = str (result ))]
140
80
except Exception as e :
141
81
logger .error (f"{ e } " )
142
- return [TextContent (type = "text" , text = str ("failed" ))]
143
- return [TextContent (type = "text" , text = str ("succeeded" ))]
82
+ return [TextContent (type = "text" , text = str (f"failed: { e } " ))]
144
83
elif name == "list_exchanges" :
145
84
try :
146
85
admin = RabbitMQAdmin (rabbitmq_host , api_port , username , password , use_tls )
147
86
result = handle_list_exchanges (admin )
148
87
return [TextContent (type = "text" , text = str (result ))]
149
88
except Exception as e :
150
89
logger .error (f"{ e } " )
151
- return [TextContent (type = "text" , text = str ("failed" ))]
90
+ return [TextContent (type = "text" , text = str (f "failed: { e } " ))]
152
91
elif name == "get_queue_info" :
153
92
try :
154
93
admin = RabbitMQAdmin (rabbitmq_host , api_port , username , password , use_tls )
@@ -159,7 +98,7 @@ async def call_tool(
159
98
return [TextContent (type = "text" , text = str (result ))]
160
99
except Exception as e :
161
100
logger .error (f"{ e } " )
162
- return [TextContent (type = "text" , text = str ("failed" ))]
101
+ return [TextContent (type = "text" , text = str (f "failed: { e } " ))]
163
102
elif name == "delete_queue" :
164
103
try :
165
104
admin = RabbitMQAdmin (rabbitmq_host , api_port , username , password , use_tls )
@@ -170,7 +109,7 @@ async def call_tool(
170
109
return [TextContent (type = "text" , text = str ("succeeded" ))]
171
110
except Exception as e :
172
111
logger .error (f"{ e } " )
173
- return [TextContent (type = "text" , text = str ("failed" ))]
112
+ return [TextContent (type = "text" , text = str (f "failed: { e } " ))]
174
113
elif name == "purge_queue" :
175
114
try :
176
115
admin = RabbitMQAdmin (rabbitmq_host , api_port , username , password , use_tls )
@@ -181,7 +120,7 @@ async def call_tool(
181
120
return [TextContent (type = "text" , text = str ("succeeded" ))]
182
121
except Exception as e :
183
122
logger .error (f"{ e } " )
184
- return [TextContent (type = "text" , text = str ("failed" ))]
123
+ return [TextContent (type = "text" , text = str (f "failed: { e } " ))]
185
124
elif name == "delete_exchange" :
186
125
try :
187
126
admin = RabbitMQAdmin (rabbitmq_host , api_port , username , password , use_tls )
@@ -192,7 +131,7 @@ async def call_tool(
192
131
return [TextContent (type = "text" , text = str ("succeeded" ))]
193
132
except Exception as e :
194
133
logger .error (f"{ e } " )
195
- return [TextContent (type = "text" , text = str ("failed" ))]
134
+ return [TextContent (type = "text" , text = str (f "failed: { e } " ))]
196
135
elif name == "get_exchange_info" :
197
136
try :
198
137
admin = RabbitMQAdmin (rabbitmq_host , api_port , username , password , use_tls )
@@ -203,7 +142,7 @@ async def call_tool(
203
142
return [TextContent (type = "text" , text = str (result ))]
204
143
except Exception as e :
205
144
logger .error (f"{ e } " )
206
- return [TextContent (type = "text" , text = str ("failed" ))]
145
+ return [TextContent (type = "text" , text = str (f "failed: { e } " ))]
207
146
raise ValueError (f"Tool not found: { name } " )
208
147
209
148
options = server .create_initialization_options ()
0 commit comments