Skip to main content
Glama
redis

Redis MCP Server

Official
by redis

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
NameRequiredDescriptionDefault
keyYes
valueYes
expirationNo

Implementation Reference

  • 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)}"

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/redis/mcp-redis'

If you have feedback or need assistance with the MCP directory API, please join our Discord server