zadd
Add members to a Redis sorted set with assigned scores and optional expiration times. Enables efficient storage, retrieval, and management of ordered data structures in Redis.
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 |
|---|---|---|---|
| expiration | No | ||
| key | Yes | ||
| member | Yes | ||
| score | Yes |
Implementation Reference
- src/tools/sorted_set.py:9-33 (handler)The zadd tool handler function, decorated with @mcp.tool() for automatic registration. It adds a member to a Redis sorted set with score and optional expiration, using RedisConnectionManager.@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 optional expiration. Output is str confirmation or error.async def zadd( key: str, score: float, member: str, expiration: Optional[int] = None ) -> str:
- src/tools/sorted_set.py:9-9 (registration)Registration of the zadd tool via @mcp.tool() decorator.@mcp.tool()