set
Store a string value in Redis using MCP, with options to set expiry in milliseconds and ensure the key does not already exist.
Instructions
Set string value with optional NX (only if not exists) and PX (expiry in milliseconds) options
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| key | Yes | Key to set | |
| nx | No | Only set if key does not exist | |
| px | No | Set expiry in milliseconds | |
| value | Yes | Value to set |
Implementation Reference
- src/tools/set_tool.ts:27-49 (handler)The execute method implements the core logic of the 'set' MCP tool, handling argument validation and executing the Redis SET command with support for NX (set if not exists) and PX (expiration in ms) options.async execute(args: unknown, client: RedisClientType): Promise<ToolResponse> { if (!this.validateArgs(args)) { return this.createErrorResponse('Invalid arguments for set'); } try { const options: any = {}; if (args.nx) { options.NX = true; } if (args.px) { options.PX = args.px; } const result = await client.set(args.key, args.value, options); if (result === null) { return this.createSuccessResponse('Key not set (NX condition not met)'); } return this.createSuccessResponse('OK'); } catch (error) { return this.createErrorResponse(`Failed to set key: ${error}`); } }
- src/tools/set_tool.ts:8-17 (schema)Input schema defining the parameters for the 'set' tool: required key and value strings, optional boolean nx and number px.inputSchema = { type: 'object', properties: { key: { type: 'string', description: 'Key to set' }, value: { type: 'string', description: 'Value to set' }, nx: { type: 'boolean', description: 'Only set if key does not exist' }, px: { type: 'number', description: 'Set expiry in milliseconds' } }, required: ['key', 'value'] };
- src/tools/tool_registry.ts:24-44 (registration)Registers the SetTool instance in the ToolRegistry by including it in the defaultTools array and calling registerTool in the loop.private registerDefaultTools() { const defaultTools = [ new HMSetTool(), new HGetTool(), new HGetAllTool(), new ScanTool(), new SetTool(), new GetTool(), new DelTool(), new ZAddTool(), new ZRangeTool(), new ZRangeByScoreTool(), new ZRemTool(), new SAddTool(), new SMembersTool(), ]; for (const tool of defaultTools) { this.registerTool(tool); } }