zrange
Retrieve a specific range of members from a Redis sorted set by specifying start and end indices, optionally including scores for precise data handling.
Instructions
Retrieve a range of members from a Redis sorted set.
Args: key (str): The sorted set key. start (int): The starting index. end (int): The ending index. with_scores (bool, optional): Whether to include scores in the result.
Returns: str: The sorted set members in the given range or an error message.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| end | Yes | ||
| key | Yes | ||
| start | Yes | ||
| with_scores | No |
Implementation Reference
- src/tools/sorted_set.py:36-56 (handler)The main handler function for the 'zrange' MCP tool. It is decorated with @mcp.tool() for registration and implements the logic to retrieve a range of members from a Redis sorted set, including optional scores. The function signature provides the input schema via type hints and docstring.@mcp.tool() async def zrange(key: str, start: int, end: int, with_scores: bool = False) -> str: """Retrieve a range of members from a Redis sorted set. Args: key (str): The sorted set key. start (int): The starting index. end (int): The ending index. with_scores (bool, optional): Whether to include scores in the result. Returns: str: The sorted set members in the given range or an error message. """ try: r = RedisConnectionManager.get_connection() members = r.zrange(key, start, end, withscores=with_scores) return ( str(members) if members else f"Sorted set {key} is empty or does not exist" ) except RedisError as e: return f"Error retrieving sorted set {key}: {str(e)}"