Skip to main content
Glama
redis

Redis MCP Server

Official
by redis

scan_all_keys

Retrieve all Redis keys matching a pattern by automatically handling SCAN cursor iterations. Use this tool to safely collect matching keys without the memory risks of KEYS * for large datasets.

Instructions

Scan and return ALL keys matching a pattern using multiple SCAN iterations.

This function automatically handles the SCAN cursor iteration to collect all matching keys. It's safer than KEYS * for large databases but will still collect all results in memory.

⚠️ WARNING: With very large datasets (millions of keys), this may consume significant memory. For large-scale operations, consider using scan_keys() with manual iteration instead.

Args: pattern: Pattern to match keys against (default is "*" for all keys). batch_size: Number of keys to scan per iteration (default 100).

Returns: A list of all keys matching the pattern or an error message.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
patternNo*
batch_sizeNo

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault
resultYes

Implementation Reference

  • The handler function for the 'scan_all_keys' MCP tool. It performs iterative SCAN operations on Redis to retrieve all keys matching the given pattern, handling cursor management and byte decoding internally. The @mcp.tool() decorator registers the tool.
    @mcp.tool()
    async def scan_all_keys(
        pattern: str = "*", batch_size: int = 100
    ) -> Union[str, List[str]]:
        """
        Scan and return ALL keys matching a pattern using multiple SCAN iterations.
    
        This function automatically handles the SCAN cursor iteration to collect all matching keys.
        It's safer than KEYS * for large databases but will still collect all results in memory.
    
        ⚠️  WARNING: With very large datasets (millions of keys), this may consume significant memory.
        For large-scale operations, consider using scan_keys() with manual iteration instead.
    
        Args:
            pattern: Pattern to match keys against (default is "*" for all keys).
            batch_size: Number of keys to scan per iteration (default 100).
    
        Returns:
            A list of all keys matching the pattern or an error message.
        """
        try:
            r = RedisConnectionManager.get_connection()
            all_keys = []
            cursor = 0
    
            while True:
                cursor, keys = r.scan(cursor=cursor, match=pattern, count=batch_size)
    
                # Convert bytes to strings if needed and add to results
                decoded_keys = [
                    key.decode("utf-8") if isinstance(key, bytes) else key for key in keys
                ]
                all_keys.extend(decoded_keys)
    
                # Break when scan is complete (cursor returns to 0)
                if cursor == 0:
                    break
    
            return all_keys
        except RedisError as e:
            return f"Error scanning all keys with pattern '{pattern}': {str(e)}"
Behavior4/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries full burden and does well. It discloses key behavioral traits: automatic cursor iteration, memory consumption risks with large datasets, and that it collects all results in memory. It doesn't cover error handling or performance characteristics beyond memory, but provides substantial operational context.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is well-structured and appropriately sized. It starts with the core purpose, explains behavioral characteristics, provides warnings, and clearly documents parameters and returns. Every sentence adds value with zero waste, and information is front-loaded effectively.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness5/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the tool's complexity (database scanning with memory implications), no annotations, and the presence of an output schema (which handles return values), the description is complete. It covers purpose, usage guidelines, behavioral transparency, parameter semantics, and warnings—everything needed for safe operation.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters4/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 0%, so the description must compensate. It explains both parameters: 'pattern' as 'Pattern to match keys against' with default '*', and 'batch_size' as 'Number of keys to scan per iteration' with default 100. This adds meaningful semantics beyond the bare schema, though it could elaborate on pattern syntax.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the tool's purpose: 'Scan and return ALL keys matching a pattern using multiple SCAN iterations.' It specifies the verb ('scan and return'), resource ('keys'), and scope ('ALL'), and distinguishes from sibling 'scan_keys' by noting it handles cursor iteration automatically. This is specific and differentiates from alternatives.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines5/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides explicit guidance on when to use this tool versus alternatives. It states it's 'safer than KEYS * for large databases' and recommends 'consider using scan_keys() with manual iteration instead' for large-scale operations. This clearly defines usage context and exclusions.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other 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