connect_redis
Establish a connection to Redis servers to enable database operations like data management, TTL control, and backup procedures through the Redis MCP server.
Instructions
连接到 Redis 服务器
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| host | Yes | Redis 服务器地址 | |
| port | Yes | Redis 服务器端口 | |
| username | No | 用户名(可选) | |
| password | No | 密码(可选) | |
| db | No | 数据库索引(可选) | |
| tls | No | 是否使用 TLS 连接(可选) |
Implementation Reference
- src/services/mcpService.ts:763-786 (handler)The handler function that implements the core logic of the 'connect_redis' tool. It builds a connection configuration from input arguments with defaults, creates RedisService and BackupService instances, establishes the connection, and returns the connection 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', defining required host/port and optional auth/TLS parameters.{ 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 dispatch case in the CallToolRequestSchema handler that routes 'connect_redis' calls to the appropriate handler method.case 'connect_redis': return await this.handleConnectRedis(args);
- src/services/mcpService.ts:73-618 (registration)Registers the 'connect_redis' tool in the list returned by ListToolsRequestSchema.this.server.setRequestHandler(ListToolsRequestSchema, async () => { return { tools: [ // 连接管理工具 { 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'] } }, { name: 'disconnect_redis', description: '断开与 Redis 服务器的连接', inputSchema: { type: 'object', properties: {} } }, // String 操作工具 { name: 'string_set', description: '设置字符串键值', inputSchema: { type: 'object', properties: { key: { type: 'string', description: '键名' }, value: { type: 'string', description: '值' }, expireSeconds: { type: 'number', description: '过期时间(秒)(可选)' } }, required: ['key', 'value'] } }, { name: 'string_get', description: '获取字符串值', inputSchema: { type: 'object', properties: { key: { type: 'string', description: '键名' } }, required: ['key'] } }, { name: 'string_incr', description: '递增数值', inputSchema: { type: 'object', properties: { key: { type: 'string', description: '键名' }, increment: { type: 'number', description: '增量值(可选,默认为 1)' } }, required: ['key'] } }, { name: 'string_decr', description: '递减数值', inputSchema: { type: 'object', properties: { key: { type: 'string', description: '键名' }, decrement: { type: 'number', description: '减量值(可选,默认为 1)' } }, required: ['key'] } }, { name: 'string_mset', description: '批量设置键值', inputSchema: { type: 'object', properties: { keyValues: { type: 'array', items: { type: 'object', properties: { key: { type: 'string', description: '键名' }, value: { type: 'string', description: '值' } }, required: ['key', 'value'] }, description: '键值对数组' } }, required: ['keyValues'] } }, { name: 'string_mget', description: '批量获取键值', inputSchema: { type: 'object', properties: { keys: { type: 'array', items: { type: 'string' }, description: '键名数组' } }, required: ['keys'] } }, // Hash 操作工具 { name: 'hash_set', description: '设置哈希字段', inputSchema: { type: 'object', properties: { key: { type: 'string', description: '哈希键名' }, field: { type: 'string', description: '字段名' }, value: { type: 'string', description: '字段值' } }, required: ['key', 'field', 'value'] } }, { name: 'hash_mset', description: '批量设置哈希字段', inputSchema: { type: 'object', properties: { key: { type: 'string', description: '哈希键名' }, fieldValues: { type: 'array', items: { type: 'object', properties: { field: { type: 'string', description: '字段名' }, value: { type: 'string', description: '字段值' } }, required: ['field', 'value'] }, description: '字段值对数组' } }, required: ['key', 'fieldValues'] } }, { name: 'hash_get', description: '获取哈希字段', inputSchema: { type: 'object', properties: { key: { type: 'string', description: '哈希键名' }, field: { type: 'string', description: '字段名' } }, required: ['key', 'field'] } }, { name: 'hash_getall', description: '获取所有哈希字段', inputSchema: { type: 'object', properties: { key: { type: 'string', description: '哈希键名' } }, required: ['key'] } }, { name: 'hash_del', description: '删除哈希字段', inputSchema: { type: 'object', properties: { key: { type: 'string', description: '哈希键名' }, fields: { oneOf: [ { type: 'string', description: '字段名' }, { type: 'array', items: { type: 'string' }, description: '字段名数组' } ], description: '要删除的字段名或字段名数组' } }, required: ['key', 'fields'] } }, // List 操作工具 { name: 'list_lpush', description: '左侧推入列表', inputSchema: { type: 'object', properties: { key: { type: 'string', description: '列表键名' }, values: { oneOf: [ { type: 'string', description: '值' }, { type: 'array', items: { type: 'string' }, description: '值数组' } ], description: '要推入的值或值数组' } }, required: ['key', 'values'] } }, { name: 'list_rpush', description: '右侧推入列表', inputSchema: { type: 'object', properties: { key: { type: 'string', description: '列表键名' }, values: { oneOf: [ { type: 'string', description: '值' }, { type: 'array', items: { type: 'string' }, description: '值数组' } ], description: '要推入的值或值数组' } }, required: ['key', 'values'] } }, { name: 'list_lpop', description: '左侧弹出列表', inputSchema: { type: 'object', properties: { key: { type: 'string', description: '列表键名' }, count: { type: 'number', description: '弹出数量(可选)' } }, required: ['key'] } }, { name: 'list_rpop', description: '右侧弹出列表', inputSchema: { type: 'object', properties: { key: { type: 'string', description: '列表键名' }, count: { type: 'number', description: '弹出数量(可选)' } }, required: ['key'] } }, { name: 'list_range', description: '获取列表范围', inputSchema: { type: 'object', properties: { key: { type: 'string', description: '列表键名' }, start: { type: 'number', description: '起始索引' }, stop: { type: 'number', description: '结束索引' } }, required: ['key', 'start', 'stop'] } }, // Set 操作工具 { name: 'set_add', description: '添加集合成员', inputSchema: { type: 'object', properties: { key: { type: 'string', description: '集合键名' }, members: { oneOf: [ { type: 'string', description: '成员' }, { type: 'array', items: { type: 'string' }, description: '成员数组' } ], description: '要添加的成员或成员数组' } }, required: ['key', 'members'] } }, { name: 'set_remove', description: '移除集合成员', inputSchema: { type: 'object', properties: { key: { type: 'string', description: '集合键名' }, members: { oneOf: [ { type: 'string', description: '成员' }, { type: 'array', items: { type: 'string' }, description: '成员数组' } ], description: '要移除的成员或成员数组' } }, required: ['key', 'members'] } }, { name: 'set_members', description: '获取集合所有成员', inputSchema: { type: 'object', properties: { key: { type: 'string', description: '集合键名' } }, required: ['key'] } }, // Sorted Set 操作工具 { name: 'zset_add', description: '添加有序集合成员', inputSchema: { type: 'object', properties: { key: { type: 'string', description: '有序集合键名' }, members: { oneOf: [ { type: 'object', properties: { member: { type: 'string', description: '成员' }, score: { type: 'number', description: '分数' } }, required: ['member', 'score'], description: '单个成员' }, { type: 'array', items: { type: 'object', properties: { member: { type: 'string', description: '成员' }, score: { type: 'number', description: '分数' } }, required: ['member', 'score'] }, description: '成员数组' } ], description: '要添加的成员或成员数组' } }, required: ['key', 'members'] } }, { name: 'zset_remove', description: '移除有序集合成员', inputSchema: { type: 'object', properties: { key: { type: 'string', description: '有序集合键名' }, members: { oneOf: [ { type: 'string', description: '成员' }, { type: 'array', items: { type: 'string' }, description: '成员数组' } ], description: '要移除的成员或成员数组' } }, required: ['key', 'members'] } }, { name: 'zset_range', description: '获取有序集合范围', inputSchema: { type: 'object', properties: { key: { type: 'string', description: '有序集合键名' }, start: { type: 'number', description: '起始索引' }, stop: { type: 'number', description: '结束索引' }, withScores: { type: 'boolean', description: '是否返回分数(可选)' } }, required: ['key', 'start', 'stop'] } }, // 键管理工具 { name: 'key_delete', description: '删除键', inputSchema: { type: 'object', properties: { keys: { oneOf: [ { type: 'string', description: '键名' }, { type: 'array', items: { type: 'string' }, description: '键名数组' } ], description: '要删除的键名或键名数组' } }, required: ['keys'] } }, { name: 'key_expire', description: '设置键过期时间', inputSchema: { type: 'object', properties: { key: { type: 'string', description: '键名' }, seconds: { type: 'number', description: '过期时间(秒)' } }, required: ['key', 'seconds'] } }, { name: 'key_ttl', description: '获取键过期时间', inputSchema: { type: 'object', properties: { key: { type: 'string', description: '键名' } }, required: ['key'] } }, { name: 'key_search', description: '查找匹配的键', inputSchema: { type: 'object', properties: { pattern: { type: 'string', description: '匹配模式(支持通配符 * ? [])' } }, required: ['pattern'] } }, { name: 'key_type', description: '获取键类型', inputSchema: { type: 'object', properties: { key: { type: 'string', description: '键名' } }, required: ['key'] } }, { name: 'key_info', description: '获取键信息', inputSchema: { type: 'object', properties: { key: { type: 'string', description: '键名' } }, required: ['key'] } }, { name: 'key_delete_pattern', description: '批量删除匹配的键', inputSchema: { type: 'object', properties: { pattern: { type: 'string', description: '匹配模式(支持通配符 * ? [])' } }, required: ['pattern'] } }, { name: 'db_flush', description: '清空当前数据库', inputSchema: { type: 'object', properties: {} } }, // 备份与恢复工具 { name: 'backup_create', description: '创建 Redis 数据备份', inputSchema: { type: 'object', properties: { filename: { type: 'string', description: '备份文件名(可选)' }, path: { type: 'string', description: '备份路径(可选)' }, includePatterns: { type: 'array', items: { type: 'string' }, description: '包含的键模式数组(可选,默认为 ["*"])' }, excludePatterns: { type: 'array', items: { type: 'string' }, description: '排除的键模式数组(可选)' } } } }, { name: 'backup_restore', description: '从备份恢复 Redis 数据', inputSchema: { type: 'object', properties: { filename: { type: 'string', description: '备份文件名' }, path: { type: 'string', description: '备份路径(可选)' }, flushBeforeRestore: { type: 'boolean', description: '恢复前是否清空数据库(可选,默认为 false)' } }, required: ['filename'] } } ], }; });