connect_redis
Establish a connection to a Redis server by specifying host and port, with optional parameters like username, password, database index, and TLS for secure access. Integrates with Redis MCP for efficient database operations.
Instructions
连接到 Redis 服务器
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| db | No | 数据库索引(可选) | |
| host | Yes | Redis 服务器地址 | |
| password | No | 密码(可选) | |
| port | Yes | Redis 服务器端口 | |
| tls | No | 是否使用 TLS 连接(可选) | |
| username | No | 用户名(可选) |
Implementation Reference
- src/services/mcpService.ts:763-786 (handler)The main handler function for the 'connect_redis' MCP tool. It constructs the Redis connection configuration from arguments and defaults, instantiates RedisService and RedisBackupService, connects to Redis, and returns the result.private async handleConnectRedis(args: any) { const config: RedisConnectionConfig = { host: args.host || this.defaultConfig.host || 'localhost', port: args.port || this.defaultConfig.port || 6379, username: args.username || this.defaultConfig.username, password: args.password || this.defaultConfig.password, db: args.db || this.defaultConfig.db || 0, tls: args.tls || this.defaultConfig.tls || false }; this.redisService = new RedisService(config); this.backupService = new RedisBackupService(this.redisService); const result = await this.redisService.connect(); return { content: [ { type: 'text', text: JSON.stringify(result, null, 2) } ] }; }
- src/services/mcpService.ts:77-92 (schema)The tool specification including name, description, and input schema for 'connect_redis' returned by listTools.{ name: 'connect_redis', description: '连接到 Redis 服务器', inputSchema: { type: 'object', properties: { host: { type: 'string', description: 'Redis 服务器地址' }, port: { type: 'number', description: 'Redis 服务器端口' }, username: { type: 'string', description: '用户名(可选)' }, password: { type: 'string', description: '密码(可选)' }, db: { type: 'number', description: '数据库索引(可选)' }, tls: { type: 'boolean', description: '是否使用 TLS 连接(可选)' } }, required: ['host', 'port'] } },
- src/services/mcpService.ts:626-627 (registration)The switch case in the CallToolRequestSchema handler that dispatches 'connect_redis' calls to the handler function.case 'connect_redis': return await this.handleConnectRedis(args);
- src/services/redisService.ts:26-60 (helper)The RedisService.connect() helper method invoked by the tool handler to create and connect the Redis client using the 'redis' package.async connect(): Promise<RedisOperationResult<string>> { try { if (this.connected && this.client) { return { success: true, data: 'Already connected' }; } const { host, port, username, password, db, tls } = this.config; this.client = createClient({ socket: { host, port, tls: tls ? true : false, reconnectStrategy: (retries) => Math.min(retries * 50, 1000) }, username: username || undefined, password: password || undefined, database: db || 0 }); // 设置错误处理 this.client.on('error', (err) => { console.error('Redis Client Error:', err); }); await this.client.connect(); this.connected = true; return { success: true, data: 'Connected to Redis server' }; } catch (error) { return { success: false, error: `Failed to connect to Redis: ${error instanceof Error ? error.message : String(error)}` }; } }