Skip to main content
Glama
redis

Redis MCP Server

Official
by redis

json_set

Set JSON values at specific paths in Redis keys with optional expiration times for structured data storage.

Instructions

Set a JSON value in Redis at a given path with an optional expiration time.

Args: name: The Redis key where the JSON document is stored. path: The JSON path where the value should be set. value: The JSON value to store (as JSON string, or will be auto-converted). expire_seconds: Optional; time in seconds after which the key should expire.

Returns: A success message or an error message.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
nameYes
pathYes
valueYes
expire_secondsNo

Implementation Reference

  • The json_set tool handler: an async function decorated with @mcp.tool() that sets a JSON value in Redis at a specified path, with optional expiration. Handles JSON parsing, Redis operations, and error handling.
    @mcp.tool()
    async def json_set(
        name: str,
        path: str,
        value: str,
        expire_seconds: Optional[int] = None,
    ) -> str:
        """Set a JSON value in Redis at a given path with an optional expiration time.
    
        Args:
            name: The Redis key where the JSON document is stored.
            path: The JSON path where the value should be set.
            value: The JSON value to store (as JSON string, or will be auto-converted).
            expire_seconds: Optional; time in seconds after which the key should expire.
    
        Returns:
            A success message or an error message.
        """
        # Try to parse the value as JSON, if it fails, treat it as a plain string
        try:
            parsed_value = json.loads(value)
        except (json.JSONDecodeError, TypeError):
            parsed_value = value
    
        try:
            r = RedisConnectionManager.get_connection()
            r.json().set(name, path, parsed_value)
    
            if expire_seconds is not None:
                r.expire(name, expire_seconds)
    
            return f"JSON value set at path '{path}' in '{name}'." + (
                f" Expires in {expire_seconds} seconds." if expire_seconds else ""
            )
        except RedisError as e:
            return f"Error setting JSON value at path '{path}' in '{name}': {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