hset
Set a field in a Redis hash with an optional expiration time to manage structured data storage.
Instructions
Set a field in a hash stored at key with an optional expiration time.
Args: name: The Redis hash key. key: The field name inside the hash. value: The value to set. expire_seconds: Optional; time in seconds after which the key should expire.
Returns: A success message or an error message.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| name | Yes | ||
| key | Yes | ||
| value | Yes | ||
| expire_seconds | No |
Implementation Reference
- src/tools/hash.py:10-37 (handler)The main handler function for the 'hset' tool. It is decorated with @mcp.tool() for registration and implements the logic to set a field in a Redis hash using hset, with optional expiration via expire.@mcp.tool() async def hset( name: str, key: str, value: str | int | float, expire_seconds: Optional[int] = None ) -> str: """Set a field in a hash stored at key with an optional expiration time. Args: name: The Redis hash key. key: The field name inside the hash. value: The value to set. expire_seconds: Optional; time in seconds after which the key should expire. Returns: A success message or an error message. """ try: r = RedisConnectionManager.get_connection() r.hset(name, key, str(value)) if expire_seconds is not None: r.expire(name, expire_seconds) return f"Field '{key}' set successfully in hash '{name}'." + ( f" Expires in {expire_seconds} seconds." if expire_seconds else "" ) except RedisError as e: return f"Error setting field '{key}' in hash '{name}': {str(e)}"