Skip to main content
Glama

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
NameRequiredDescriptionDefault
dbNo数据库索引(可选)
hostYesRedis 服务器地址
passwordNo密码(可选)
portYesRedis 服务器端口
tlsNo是否使用 TLS 连接(可选)
usernameNo用户名(可选)

Implementation Reference

  • 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) } ] }; }
  • 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'] } },
  • The switch case in the CallToolRequestSchema handler that dispatches 'connect_redis' calls to the handler function.
    case 'connect_redis': return await this.handleConnectRedis(args);
  • 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)}` }; } }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/pickstar-2002/redis-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server