zadd
Add members with scores to a Redis sorted set and optionally set expiration times for time-sensitive data management.
Instructions
Add a member to a Redis sorted set with an optional expiration time.
Args: key (str): The sorted set key. score (float): The score of the member. member (str): The member to add. expiration (int, optional): Expiration time in seconds.
Returns: str: Confirmation message or an error message.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| key | Yes | ||
| score | Yes | ||
| member | Yes | ||
| expiration | No |
Implementation Reference
- src/tools/sorted_set.py:9-33 (handler)The main handler function for the 'zadd' tool. It is decorated with @mcp.tool(), which registers it as an MCP tool. The function adds a member with a score to a Redis sorted set and optionally sets an expiration on the key.@mcp.tool() async def zadd( key: str, score: float, member: str, expiration: Optional[int] = None ) -> str: """Add a member to a Redis sorted set with an optional expiration time. Args: key (str): The sorted set key. score (float): The score of the member. member (str): The member to add. expiration (int, optional): Expiration time in seconds. Returns: str: Confirmation message or an error message. """ try: r = RedisConnectionManager.get_connection() r.zadd(key, {member: score}) if expiration: r.expire(key, expiration) return f"Successfully added {member} to {key} with score {score}" + ( f" and expiration {expiration} seconds" if expiration else "" ) except RedisError as e: return f"Error adding to sorted set {key}: {str(e)}"
- src/tools/sorted_set.py:10-12 (schema)Input schema defined by function parameters with type hints and comprehensive docstring describing args and return type.async def zadd( key: str, score: float, member: str, expiration: Optional[int] = None ) -> str: