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 {
@@ -233,26 +235,25 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
233
235
}
234
236
} ) ;
235
237
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
- } ) ;
238
+ // Set up Redis event handlers
239
+ redisClient . on ( 'error' , ( err : Error ) => {
240
+ console . error ( 'Redis Client Error:' , err ) ;
241
+ } ) ;
243
242
244
- redisClient . on ( 'connect' , ( ) => {
245
- console . error ( `Connected to Redis at ${ REDIS_URL } ` ) ;
246
- } ) ;
243
+ redisClient . on ( 'connect' , ( ) => {
244
+ console . error ( `Connected to Redis at ${ REDIS_URL } ` ) ;
245
+ } ) ;
247
246
248
- redisClient . on ( 'reconnecting' , ( ) => {
249
- console . error ( 'Attempting to reconnect to Redis...' ) ;
250
- } ) ;
247
+ redisClient . on ( 'reconnecting' , ( ) => {
248
+ console . error ( 'Attempting to reconnect to Redis...' ) ;
249
+ } ) ;
251
250
252
- redisClient . on ( 'end' , ( ) => {
253
- console . error ( 'Redis connection closed' ) ;
254
- } ) ;
251
+ redisClient . on ( 'end' , ( ) => {
252
+ console . error ( 'Redis connection closed' ) ;
253
+ } ) ;
255
254
255
+ async function runServer ( ) {
256
+ try {
256
257
// Connect to Redis
257
258
await redisClient . connect ( ) ;
258
259
@@ -261,26 +262,21 @@ async function main() {
261
262
await server . connect ( transport ) ;
262
263
console . error ( "Redis MCP Server running on stdio" ) ;
263
264
} catch ( error ) {
264
- console . error ( "Error during startup:" , error ) ;
265
- await cleanup ( ) ;
265
+ console . error ( "Fatal error running server:" , error ) ;
266
+ await redisClient . quit ( ) . catch ( ( ) => { } ) ;
267
+ process . exit ( 1 ) ;
266
268
}
267
269
}
268
270
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
271
// Handle process termination
280
- process . on ( 'SIGINT' , cleanup ) ;
281
- process . on ( 'SIGTERM' , cleanup ) ;
272
+ process . on ( 'SIGINT' , async ( ) => {
273
+ await redisClient . quit ( ) . catch ( ( ) => { } ) ;
274
+ process . exit ( 0 ) ;
275
+ } ) ;
282
276
283
- main ( ) . catch ( ( error ) => {
284
- console . error ( "Fatal error in main():" , error ) ;
285
- cleanup ( ) ;
277
+ process . on ( 'SIGTERM' , async ( ) => {
278
+ await redisClient . quit ( ) . catch ( ( ) => { } ) ;
279
+ process . exit ( 0 ) ;
286
280
} ) ;
281
+
282
+ runServer ( ) ;
0 commit comments