Skip to main content
Glama

zset_add

Add members with scores to a Redis sorted set to store and retrieve data by rank or score range.

Instructions

添加有序集合成员

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
keyYes有序集合键名
membersYes要添加的成员或成员数组

Implementation Reference

  • The main handler function for the zset_add MCP tool. Ensures Redis connection and calls the underlying RedisService.zadd method, returning the result as MCP content.
    private async handleZsetAdd(args: any) {
      this.ensureRedisConnection();
      const result = await this.redisService!.zadd(args.key, args.members);
      
      return {
        content: [
          {
            type: 'text',
            text: JSON.stringify(result, null, 2)
          }
        ]
      };
    }
  • Input schema definition for the zset_add tool, specifying key (string) and members (single object {member: string, score: number} or array thereof).
    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']
  • Registration in the tool dispatcher switch statement within CallToolRequestHandler, routing calls to the zset_add handler.
    case 'zset_add':
      return await this.handleZsetAdd(args);
  • Helper method in RedisService that implements the ZADD Redis command, converting MCP parameters to redis client zAdd format and handling single/array inputs.
    async zadd(key: string, members: RedisSortedSetMember | RedisSortedSetMember[]): Promise<RedisOperationResult<number>> {
      return this.executeCommand(async () => {
        if (!this.client) throw new Error('Redis client not initialized');
        
        if (Array.isArray(members)) {
          const args: Array<{ score: number, value: string }> = members.map(m => ({
            score: m.score,
            value: m.member
          }));
          return await this.client.zAdd(key, args);
        } else {
          return await this.client.zAdd(key, { score: members.score, value: members.member });
        }
      });
    }

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