1
+ #!/usr/bin/env node
2
+
1
3
import { Server } from "@modelcontextprotocol/sdk/server/index.js" ;
2
4
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js" ;
3
5
import {
@@ -19,11 +21,14 @@ const redisClient = createClient({
19
21
socket : {
20
22
reconnectStrategy : ( retries ) => {
21
23
if ( retries >= MAX_RETRIES ) {
22
- console . error ( `Maximum retries (${ MAX_RETRIES } ) reached. Giving up.` ) ;
24
+ console . error ( `[Redis Error] Maximum retries (${ MAX_RETRIES } ) reached. Giving up.` ) ;
25
+ console . error ( `[Redis Error] Connection: ${ REDIS_URL } ` ) ;
23
26
return new Error ( 'Max retries reached' ) ;
24
27
}
25
28
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` ) ;
29
+ console . error ( `[Redis Retry] Attempt ${ retries + 1 } /${ MAX_RETRIES } failed` ) ;
30
+ console . error ( `[Redis Retry] Next attempt in ${ delay } ms` ) ;
31
+ console . error ( `[Redis Retry] Connection: ${ REDIS_URL } ` ) ;
27
32
return delay ;
28
33
}
29
34
}
@@ -233,26 +238,27 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
233
238
}
234
239
} ) ;
235
240
236
- // Start the server
237
- async function main ( ) {
238
- try {
239
- // Set up Redis event handlers
240
- redisClient . on ( 'error' , ( err : Error ) => {
241
- console . error ( 'Redis Client Error:' , err ) ;
242
- } ) ;
241
+ // Set up Redis event handlers
242
+ redisClient . on ( 'error' , ( err : Error ) => {
243
+ console . error ( `[Redis Error] ${ err . name } : ${ err . message } ` ) ;
244
+ console . error ( `[Redis Error] Connection: ${ REDIS_URL } ` ) ;
245
+ console . error ( `[Redis Error] Stack: ${ err . stack } ` ) ;
246
+ } ) ;
243
247
244
- redisClient . on ( 'connect' , ( ) => {
245
- console . error ( `Connected to Redis at ${ REDIS_URL } ` ) ;
246
- } ) ;
248
+ redisClient . on ( 'connect' , ( ) => {
249
+ console . error ( `[Redis Connected] Successfully connected to ${ REDIS_URL } ` ) ;
250
+ } ) ;
247
251
248
- redisClient . on ( 'reconnecting' , ( ) => {
249
- console . error ( 'Attempting to reconnect to Redis ...' ) ;
250
- } ) ;
252
+ redisClient . on ( 'reconnecting' , ( ) => {
253
+ console . error ( '[Redis Reconnecting] Connection lost, attempting to reconnect ...' ) ;
254
+ } ) ;
251
255
252
- redisClient . on ( 'end' , ( ) => {
253
- console . error ( 'Redis connection closed' ) ;
254
- } ) ;
256
+ redisClient . on ( 'end' , ( ) => {
257
+ console . error ( '[ Redis Disconnected] Connection closed' ) ;
258
+ } ) ;
255
259
260
+ async function runServer ( ) {
261
+ try {
256
262
// Connect to Redis
257
263
await redisClient . connect ( ) ;
258
264
@@ -261,26 +267,25 @@ async function main() {
261
267
await server . connect ( transport ) ;
262
268
console . error ( "Redis MCP Server running on stdio" ) ;
263
269
} catch ( error ) {
264
- console . error ( "Error during startup:" , error ) ;
265
- await cleanup ( ) ;
270
+ const err = error as Error ;
271
+ console . error ( "[Redis Fatal] Server initialization failed" ) ;
272
+ console . error ( `[Redis Fatal] Error: ${ err . name } : ${ err . message } ` ) ;
273
+ console . error ( `[Redis Fatal] Connection: ${ REDIS_URL } ` ) ;
274
+ console . error ( `[Redis Fatal] Stack: ${ err . stack } ` ) ;
275
+ await redisClient . quit ( ) . catch ( ( ) => { } ) ;
276
+ process . exit ( 1 ) ;
266
277
}
267
278
}
268
279
269
- // Cleanup function
270
- async function cleanup ( ) {
271
- try {
272
- await redisClient . quit ( ) ;
273
- } catch ( error ) {
274
- console . error ( "Error during cleanup:" , error ) ;
275
- }
276
- process . exit ( 1 ) ;
277
- }
278
-
279
280
// Handle process termination
280
- process . on ( 'SIGINT' , cleanup ) ;
281
- process . on ( 'SIGTERM' , cleanup ) ;
281
+ process . on ( 'SIGINT' , async ( ) => {
282
+ await redisClient . quit ( ) . catch ( ( ) => { } ) ;
283
+ process . exit ( 0 ) ;
284
+ } ) ;
282
285
283
- main ( ) . catch ( ( error ) => {
284
- console . error ( "Fatal error in main():" , error ) ;
285
- cleanup ( ) ;
286
+ process . on ( 'SIGTERM' , async ( ) => {
287
+ await redisClient . quit ( ) . catch ( ( ) => { } ) ;
288
+ process . exit ( 0 ) ;
286
289
} ) ;
290
+
291
+ runServer ( ) ;
0 commit comments