json_set
Set or update JSON values in Redis at specific paths with optional expiration. Use this tool to manage JSON data efficiently and control key lifetimes.
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. 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 |
|---|---|---|---|
| expire_seconds | No | ||
| name | Yes | ||
| path | Yes | ||
| value | Yes |
Implementation Reference
- src/tools/json.py:9-45 (handler)The json_set tool handler function, decorated with @mcp.tool() for registration. Implements setting JSON value in Redis using redis.json().set, with optional expiration.@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)}"
- src/tools/json.py:9-9 (registration)Registration of the json_set tool via @mcp.tool() decorator.@mcp.tool()
- src/tools/json.py:16-26 (schema)Input schema defined in the docstring of the json_set function."""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. """