zrangebyscore
Retrieve members from a Redis sorted set whose scores fall within a specified range, optionally including their scores, to efficiently filter and manage ordered data.
Instructions
Return members from a sorted set with scores between min and max
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| key | Yes | Sorted set key | |
| max | Yes | Maximum score | |
| min | Yes | Minimum score | |
| withScores | No | Include scores in output |
Implementation Reference
- src/tools/zrangebyscore_tool.ts:5-59 (handler)The ZRangeByScoreTool class implementing the handler for the 'zrangebyscore' tool, including name, input schema, validation, and the execute method that performs the Redis ZRANGEBYSCORE command.export class ZRangeByScoreTool extends RedisTool { name = 'zrangebyscore'; description = 'Return members from a sorted set with scores between min and max'; inputSchema = { type: 'object', properties: { key: { type: 'string', description: 'Sorted set key' }, min: { type: 'number', description: 'Minimum score' }, max: { type: 'number', description: 'Maximum score' }, withScores: { type: 'boolean', description: 'Include scores in output', default: false } }, required: ['key', 'min', 'max'] }; validateArgs(args: unknown): args is ZRangeByScoreArgs { return typeof args === 'object' && args !== null && 'key' in args && typeof (args as any).key === 'string' && 'min' in args && typeof (args as any).min === 'number' && 'max' in args && typeof (args as any).max === 'number' && (!('withScores' in args) || typeof (args as any).withScores === 'boolean'); } async execute(args: unknown, client: RedisClientType): Promise<ToolResponse> { if (!this.validateArgs(args)) { return this.createErrorResponse('Invalid arguments for zrangebyscore'); } try { const result = await client.sendCommand([ 'ZRANGEBYSCORE', args.key, args.min.toString(), args.max.toString(), ...(args.withScores ? ['WITHSCORES'] : []) ]) as string[]; if (!Array.isArray(result) || result.length === 0) { return this.createSuccessResponse('No members found in the specified score range'); } if (args.withScores) { // Format result with scores when WITHSCORES is used const pairs = []; for (let i = 0; i < result.length; i += 2) { pairs.push(`${result[i]} (score: ${result[i + 1]})`); } return this.createSuccessResponse(pairs.join('\n')); } return this.createSuccessResponse(result.join('\n')); } catch (error) { return this.createErrorResponse(`Failed to get range by score from sorted set: ${error}`); } } }
- src/interfaces/types.ts:58-63 (schema)TypeScript interface defining the input parameters for the zrangebyscore tool.export interface ZRangeByScoreArgs { key: string; min: number; max: number; withScores?: boolean; }
- src/tools/tool_registry.ts:35-35 (registration)Registration of the ZRangeByScoreTool instance in the default tools array of ToolRegistry.new ZRangeByScoreTool(),