zrange
Retrieve a specified range of members from a sorted set by index using key, start, and stop parameters, optionally including scores for efficient data access in Redis MCP Server.
Instructions
Return a range of members from a sorted set by index
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| key | Yes | Sorted set key | |
| start | Yes | Start index (0-based) | |
| stop | Yes | Stop index (inclusive) | |
| withScores | No | Include scores in output |
Implementation Reference
- src/tools/zrange_tool.ts:5-59 (handler)ZRangeTool class: defines the 'zrange' tool name, input schema, validation, and execute handler that runs the Redis ZRANGE command with optional WITHSCORES.export class ZRangeTool extends RedisTool { name = 'zrange'; description = 'Return a range of members from a sorted set by index'; inputSchema = { type: 'object', properties: { key: { type: 'string', description: 'Sorted set key' }, start: { type: 'number', description: 'Start index (0-based)' }, stop: { type: 'number', description: 'Stop index (inclusive)' }, withScores: { type: 'boolean', description: 'Include scores in output', default: false } }, required: ['key', 'start', 'stop'] }; validateArgs(args: unknown): args is ZRangeArgs { return typeof args === 'object' && args !== null && 'key' in args && typeof (args as any).key === 'string' && 'start' in args && typeof (args as any).start === 'number' && 'stop' in args && typeof (args as any).stop === '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 zrange'); } try { const result = await client.sendCommand([ 'ZRANGE', args.key, args.start.toString(), args.stop.toString(), ...(args.withScores ? ['WITHSCORES'] : []) ]) as string[]; if (!Array.isArray(result) || result.length === 0) { return this.createSuccessResponse('No members found in the specified 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 from sorted set: ${error}`); } } }
- src/interfaces/types.ts:51-56 (schema)TypeScript interface ZRangeArgs defining the input parameters for the zrange tool.export interface ZRangeArgs { key: string; start: number; stop: number; withScores?: boolean; }
- src/tools/zrange_tool.ts:8-17 (schema)JSON schema for zrange tool input validation.inputSchema = { type: 'object', properties: { key: { type: 'string', description: 'Sorted set key' }, start: { type: 'number', description: 'Start index (0-based)' }, stop: { type: 'number', description: 'Stop index (inclusive)' }, withScores: { type: 'boolean', description: 'Include scores in output', default: false } }, required: ['key', 'start', 'stop'] };
- src/tools/tool_registry.ts:34-34 (registration)Registration of ZRangeTool instance in the default tools array within ToolRegistry.new ZRangeTool(),