Skip to content

Commit a606bdd

Browse files
NishanthNishanth
authored andcommitted
Add lrem functionality to list.py
1 parent a17fa43 commit a606bdd

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/tools/list.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,27 @@ async def llen(name: str) -> int:
7373
return r.llen(name)
7474
except RedisError as e:
7575
return f"Error retrieving length of list '{name}': {str(e)}"
76+
77+
@mcp.tool()
78+
async def lrem(name: str, count: int, element: FieldT) -> str:
79+
    """Remove elements from a Redis list.
80+
   
81+
    Args:
82+
        name: The name of the list
83+
        count: Number of elements to remove (0 = all, positive = from head, negative = from tail)
84+
        element: The element value to remove
85+
       
86+
    Returns:
87+
        A string indicating the number of elements removed.
88+
    """
89+
    try:
90+
        r = RedisConnectionManager.get_connection()
91+
        removed_count = r.lrem(name, count, element)
92+
       
93+
        if removed_count == 0:
94+
            return f"Element '{element}' not found in list '{name}' or list does not exist."
95+
        else:
96+
            return f"Removed {removed_count} occurrence(s) of '{element}' from list '{name}'."
97+
           
98+
    except RedisError as e:
99+
        return f"Error removing element from list '{name}': {str(e)}"

0 commit comments

Comments
 (0)