set
Store string values in Redis with optional expiration times to manage key-value data persistence and caching.
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 |
|---|---|---|---|
| key | Yes | ||
| value | Yes | ||
| expiration | No |
Implementation Reference
- src/tools/string.py:11-49 (handler)Handler for the 'set' tool: sets a Redis string key with optional expiration using Redis SET or SETEX.@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)}"