zrange
Retrieve a range of members from a Redis sorted set by specifying start and end indexes. Optionally include scores for each member in the results.
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 |
|---|---|---|---|
| key | Yes | ||
| start | Yes | ||
| end | Yes | ||
| with_scores | No |
Implementation Reference
- src/tools/sorted_set.py:36-56 (handler)The zrange tool handler: an async function decorated with @mcp.tool() that executes the Redis ZRANGE command to retrieve members from a sorted set, with optional scores, handling errors and formatting the response.@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)}"
- src/common/server.py:6-17 (registration)The load_tools function imports all modules in src/tools, triggering the @mcp.tool() decorators to register tools including zrange from sorted_set.py.def load_tools(): import src.tools as tools_pkg for _, module_name, _ in pkgutil.iter_modules(tools_pkg.__path__): importlib.import_module(f"src.tools.{module_name}") # Initialize FastMCP server mcp = FastMCP("Redis MCP Server", dependencies=["redis", "dotenv", "numpy", "aiohttp"]) # Load tools load_tools()