Skip to content

Commit 5e54cd3

Browse files
authored
Merge pull request #1166 from modelcontextprotocol/force-bump-redis-and-memory
Add missing capabilities and improve connection handling for server-redis
2 parents ed96303 + f323325 commit 5e54cd3

File tree

2 files changed

+84
-9
lines changed

2 files changed

+84
-9
lines changed

src/redis/README.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,31 @@
22

33
A Model Context Protocol server that provides access to Redis databases. This server enables LLMs to interact with Redis key-value stores through a set of standardized tools.
44

5+
## Prerequisites
6+
7+
1. Redis server must be installed and running
8+
- [Download Redis](https://redis.io/download)
9+
- For Windows users: Use [Windows Subsystem for Linux (WSL)](https://redis.io/docs/getting-started/installation/install-redis-on-windows/) or [Memurai](https://www.memurai.com/) (Redis-compatible Windows server)
10+
- Default port: 6379
11+
12+
## Common Issues & Solutions
13+
14+
### Connection Errors
15+
16+
**ECONNREFUSED**
17+
- **Cause**: Redis server is not running or unreachable
18+
- **Solution**:
19+
- Verify Redis is running: `redis-cli ping` should return "PONG"
20+
- Check Redis service status: `systemctl status redis` (Linux) or `brew services list` (macOS)
21+
- Ensure correct port (default 6379) is not blocked by firewall
22+
- Verify Redis URL format: `redis://hostname:port`
23+
24+
### Server Behavior
25+
26+
- The server implements exponential backoff with a maximum of 5 retries
27+
- Initial retry delay: 1 second, maximum delay: 30 seconds
28+
- Server will exit after max retries to prevent infinite reconnection loops
29+
530
## Components
631

732
### Tools
@@ -77,4 +102,4 @@ docker build -t mcp/redis -f src/redis/Dockerfile .
77102

78103
## License
79104

80-
This MCP server is licensed under the MIT License. This means you are free to use, modify, and distribute the software, subject to the terms and conditions of the MIT License. For more details, please see the LICENSE file in the project repository.
105+
This MCP server is licensed under the MIT License. This means you are free to use, modify, and distribute the software, subject to the terms and conditions of the MIT License. For more details, please see the LICENSE file in the project repository.

src/redis/src/index.ts

Lines changed: 58 additions & 8 deletions
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
@@ -36,7 +52,12 @@ const ListArgumentsSchema = z.object({
3652
const server = new Server(
3753
{
3854
name: "redis",
39-
version: "1.0.0"
55+
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)