Skip to content

Add LREM command support for removing list elements by value #38

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/tools/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,27 @@ async def llen(name: str) -> int:
return r.llen(name)
except RedisError as e:
return f"Error retrieving length of list '{name}': {str(e)}"

@mcp.tool()
async def lrem(name: str, count: int, element: FieldT) -> str:
    """Remove elements from a Redis list.

    Args:
        name: The name of the list
        count: Number of elements to remove (0 = all, positive = from head, negative = from tail)
        element: The element value to remove

    Returns:
        A string indicating the number of elements removed.
    """
    try:
        r = RedisConnectionManager.get_connection()
        removed_count = r.lrem(name, count, element)

        if removed_count == 0:
            return f"Element '{element}' not found in list '{name}' or list does not exist."
        else:
            return f"Removed {removed_count} occurrence(s) of '{element}' from list '{name}'."

    except RedisError as e:
        return f"Error removing element from list '{name}': {str(e)}"