Skip to main content
Glama
redis

Redis MCP Server

Official
by redis

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
NameRequiredDescriptionDefault
keyYes
startYes
endYes
with_scoresNo

Implementation Reference

  • 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)}"
  • 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()

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/redis/mcp-redis'

If you have feedback or need assistance with the MCP directory API, please join our Discord server