set
Store a Redis string value with an optional expiration time by specifying a key and value. Confirms successful storage or returns an error message.
Instructions
Set a Redis string value with an optional expiration time.
Args: key (str): The key to set. value (str, bytes, int, float, dict): The value to store. 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 | ||
| value | Yes |
Implementation Reference
- src/tools/string.py:11-49 (handler)Implementation of the 'set' MCP tool, which sets a Redis key-value pair supporting multiple value types and optional TTL (time-to-live) expiration.@mcp.tool() async def set( key: str, value: Union[str, bytes, int, float, dict], expiration: Optional[int] = None, ) -> str: """Set a Redis string value with an optional expiration time. Args: key (str): The key to set. value (str, bytes, int, float, dict): The value to store. expiration (int, optional): Expiration time in seconds. Returns: str: Confirmation message or an error message. """ if isinstance(value, bytes): encoded_value = value elif isinstance(value, dict): encoded_value = json.dumps(value) else: encoded_value = str(value) if isinstance(encoded_value, str): encoded_value = encoded_value.encode("utf-8") try: r: Redis = RedisConnectionManager.get_connection() if expiration: r.setex(key, expiration, encoded_value) else: r.set(key, encoded_value) return f"Successfully set {key}" + ( f" with expiration {expiration} seconds" if expiration else "" ) except RedisError as e: return f"Error setting key {key}: {str(e)}"