Skip to content

Conversation

@nishanthp
Copy link

@nishanthp nishanthp commented Jul 30, 2025

This PR implements the LREM (List Remove) functionality in list.py, enabling removal of list elements by value with count-based control.

Changes:

  • Added lrem function to support Redis LREM command
  • Allows removing elements equal to a specified value from Redis lists
  • Supports count parameter for controlling removal behavior:
    • count > 0: Remove elements from head to tail
    • count < 0: Remove elements from tail to head
    • count = 0: Remove all matching elements

Usage:

# Remove first 2 occurrences of "value" from head
lrem("mylist", 2, "value")

# Remove first occurrence of "value" from tail
lrem("mylist", -1, "value")

# Remove all occurrences of "value"
lrem("mylist", 0, "value")

@nishanthp nishanthp changed the title Add lrem functionality to list.py Add LREM command support for removing list elements by value Jul 30, 2025
@nishanthp
Copy link
Author

I was not sure how to add reviewers, so tagging you to review the PR mortensi. Thank you!

@mortensi
Copy link
Member

mortensi commented Aug 1, 2025

Thanks @nishanthp, we are looking at restructuring the toolset, and eventually reduce the number of exposed tools. As soon as we move forward, we'll review the PR and work on it. Thanks for contributing, I will keep you posted!

@nishanthp
Copy link
Author

Thanks @mortensi for the update! Looking forward to collaborating with you as things progress.

Comment on lines 78 to 99
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)}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This tool needs to be covered by unit tests (see test_list.py) for reference.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vchomakov Sure, I can add the required unit tests.

Copy link
Author

@nishanthp nishanthp Nov 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added the test cases for lrem method. Please see the below screenshot for details.

Screenshot 2025-11-08 at 2 52 21 PM

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding the tests, @nishanthp!
There is a formatting issue, thought: https://github.com/redis/mcp-redis/actions/runs/19199746370/job/54979827575
You can fix it by running uv run ruff format on your branch.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vchomakov Ran the format command locally. I have pushed the changes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test_server_tool_count_and_names and test_server_lists_tools fail because the server returns an unexpected tool lrem, and the number of tools is 45, while 44 are expected in the test.

@nishanthp Please, adjust the tests to fix the pipeline failures.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vchomakov I initially ran only the tests I added, my mistake. I’ve now executed all the tests for my branch locally. Please see the screenshot below for reference.

Screenshot 2025-11-11 at 11 42 04 AM

@nishanthp nishanthp force-pushed the addlremfunctionality branch from a606bdd to 6125417 Compare November 8, 2025 22:54
@nishanthp nishanthp force-pushed the addlremfunctionality branch from 1d20bf4 to e326f04 Compare November 10, 2025 18:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants