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
| Name | Required | Description | Default |
|---|---|---|---|
| pattern | No | * | |
| batch_size | No |
Implementation Reference
- src/tools/misc.py:159-200 (handler)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)}"