Skip to content

Commit 52e60e6

Browse files
committed
Add missing capabilities and better connection handling
1 parent 6279021 commit 52e60e6

File tree

1 file changed

+57
-7
lines changed

1 file changed

+57
-7
lines changed

src/redis/src/index.ts

+57-7
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,26 @@ import {
77
import { z } from "zod";
88
import { createClient } from 'redis';
99

10-
// Get Redis URL from command line args or use default
10+
// Configuration
1111
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
1217
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+
}
1430
});
1531

1632
// Define Zod schemas for validation
@@ -37,6 +53,11 @@ const server = new Server(
3753
{
3854
name: "redis",
3955
version: "0.0.1"
56+
},
57+
{
58+
capabilities: {
59+
tools: {}
60+
}
4061
}
4162
);
4263

@@ -215,22 +236,51 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
215236
// Start the server
216237
async function main() {
217238
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+
218256
// Connect to Redis
219-
redisClient.on('error', (err: Error) => console.error('Redis Client Error', err));
220257
await redisClient.connect();
221-
console.error(`Connected to Redis successfully at ${REDIS_URL}`);
222258

259+
// Set up MCP server
223260
const transport = new StdioServerTransport();
224261
await server.connect(transport);
225262
console.error("Redis MCP Server running on stdio");
226263
} catch (error) {
227264
console.error("Error during startup:", error);
265+
await cleanup();
266+
}
267+
}
268+
269+
// Cleanup function
270+
async function cleanup() {
271+
try {
228272
await redisClient.quit();
229-
process.exit(1);
273+
} catch (error) {
274+
console.error("Error during cleanup:", error);
230275
}
276+
process.exit(1);
231277
}
232278

279+
// Handle process termination
280+
process.on('SIGINT', cleanup);
281+
process.on('SIGTERM', cleanup);
282+
233283
main().catch((error) => {
234284
console.error("Fatal error in main():", error);
235-
redisClient.quit().finally(() => process.exit(1));
236-
});
285+
cleanup();
286+
});

0 commit comments

Comments
 (0)