@@ -7,10 +7,26 @@ import {
7
7
import { z } from "zod" ;
8
8
import { createClient } from 'redis' ;
9
9
10
- // Get Redis URL from command line args or use default
10
+ // Configuration
11
11
const REDIS_URL = process . argv [ 2 ] || "redis://localhost:6379" ;
12
+ const MAX_RETRIES = 5 ;
13
+ const MIN_RETRY_DELAY = 1000 ; // 1 second
14
+ const MAX_RETRY_DELAY = 30000 ; // 30 seconds
15
+
16
+ // Create Redis client with retry strategy
12
17
const redisClient = createClient ( {
13
- url : REDIS_URL
18
+ url : REDIS_URL ,
19
+ socket : {
20
+ reconnectStrategy : ( retries ) => {
21
+ if ( retries >= MAX_RETRIES ) {
22
+ console . error ( `Maximum retries (${ MAX_RETRIES } ) reached. Giving up.` ) ;
23
+ return new Error ( 'Max retries reached' ) ;
24
+ }
25
+ const delay = Math . min ( Math . pow ( 2 , retries ) * MIN_RETRY_DELAY , MAX_RETRY_DELAY ) ;
26
+ console . error ( `Reconnection attempt ${ retries + 1 } /${ MAX_RETRIES } in ${ delay } ms` ) ;
27
+ return delay ;
28
+ }
29
+ }
14
30
} ) ;
15
31
16
32
// Define Zod schemas for validation
@@ -37,6 +53,11 @@ const server = new Server(
37
53
{
38
54
name : "redis" ,
39
55
version : "0.0.1"
56
+ } ,
57
+ {
58
+ capabilities : {
59
+ tools : { }
60
+ }
40
61
}
41
62
) ;
42
63
@@ -215,22 +236,51 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
215
236
// Start the server
216
237
async function main ( ) {
217
238
try {
239
+ // Set up Redis event handlers
240
+ redisClient . on ( 'error' , ( err : Error ) => {
241
+ console . error ( 'Redis Client Error:' , err ) ;
242
+ } ) ;
243
+
244
+ redisClient . on ( 'connect' , ( ) => {
245
+ console . error ( `Connected to Redis at ${ REDIS_URL } ` ) ;
246
+ } ) ;
247
+
248
+ redisClient . on ( 'reconnecting' , ( ) => {
249
+ console . error ( 'Attempting to reconnect to Redis...' ) ;
250
+ } ) ;
251
+
252
+ redisClient . on ( 'end' , ( ) => {
253
+ console . error ( 'Redis connection closed' ) ;
254
+ } ) ;
255
+
218
256
// Connect to Redis
219
- redisClient . on ( 'error' , ( err : Error ) => console . error ( 'Redis Client Error' , err ) ) ;
220
257
await redisClient . connect ( ) ;
221
- console . error ( `Connected to Redis successfully at ${ REDIS_URL } ` ) ;
222
258
259
+ // Set up MCP server
223
260
const transport = new StdioServerTransport ( ) ;
224
261
await server . connect ( transport ) ;
225
262
console . error ( "Redis MCP Server running on stdio" ) ;
226
263
} catch ( error ) {
227
264
console . error ( "Error during startup:" , error ) ;
265
+ await cleanup ( ) ;
266
+ }
267
+ }
268
+
269
+ // Cleanup function
270
+ async function cleanup ( ) {
271
+ try {
228
272
await redisClient . quit ( ) ;
229
- process . exit ( 1 ) ;
273
+ } catch ( error ) {
274
+ console . error ( "Error during cleanup:" , error ) ;
230
275
}
276
+ process . exit ( 1 ) ;
231
277
}
232
278
279
+ // Handle process termination
280
+ process . on ( 'SIGINT' , cleanup ) ;
281
+ process . on ( 'SIGTERM' , cleanup ) ;
282
+
233
283
main ( ) . catch ( ( error ) => {
234
284
console . error ( "Fatal error in main():" , error ) ;
235
- redisClient . quit ( ) . finally ( ( ) => process . exit ( 1 ) ) ;
236
- } ) ;
285
+ cleanup ( ) ;
286
+ } ) ;
0 commit comments