redis_tool
Execute Redis commands to manage strings, hashes, lists, and sets directly from the ToolBox MCP Server for data operations.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| command | Yes | 要执行的 Redis 命令 (例如, 'GET', 'SET') | |
| args | No | 命令的参数列表 (例如: ["mykey", "myvalue"]) |
Implementation Reference
- src/tools/redis_tool.ts:59-122 (handler)Main execution handler for redis_tool: parses command/args, blocks dangerous commands, executes via ioredis.call(), formats results, handles errors with hints.export default async (request: any) => { try { const command = String(request.params.arguments?.command).toUpperCase(); const args = request.params.arguments?.args || []; if (DANGEROUS_COMMANDS.has(command)) { return { content: [{ type: "text", text: JSON.stringify({ error: "Execution of dangerous command is denied.", command: command, hint: `The command '${command}' is on the denylist for security reasons.` }, null, 2), }], isError: true, }; } if (!Array.isArray(args)) { return { content: [{ type: "text", text: JSON.stringify({ error: "Invalid parameter type for 'args'", hint: "'args' must be an array of strings. For example: [\"key\", \"value\"]" }, null, 2), }], isError: true, }; } const client = getClient(); const results = await client.call(command, ...args); const formattedResults = formatRedisResults(command, results); return { content: [{ type: "text", text: JSON.stringify(formattedResults, null, 2), }], }; } catch (error: any) { const command = request.params.arguments?.command || 'UNKNOWN'; const args = request.params.arguments?.args || []; const errorResponse = { error: { code: error.code || error.name || "REDIS_ERROR", message: error.message, }, command: command, args: args, hint: getErrorHint(command, error.message) }; return { content: [{ type: "text", text: JSON.stringify(errorResponse, null, 2), }], isError: true, }; } };
- src/tools/redis_tool.ts:37-57 (schema)Input schema defining 'command' (required string) and 'args' (array of strings) for redis_tool./** Parameter list for the redis_tool */ export const schema = { name: "redis_tool", description: "执行 Redis 命令,支持字符串、哈希、列表、集合等多种数据结构。", type: "object", properties: { command: { type: "string", description: "要执行的 Redis 命令 (例如, 'GET', 'SET')", }, args: { type: "array", description: "命令的参数列表 (例如: [\"mykey\", \"myvalue\"])", items: { type: "string" }, default: [] }, }, required: ["command"] };
- src/tools/redis_tool.ts:10-27 (helper)Singleton Redis client manager: creates/reconnects client using REDIS_URI env var with retry config.function getClient(): Redis { const redisUri = process.env.REDIS_URI; if (!redisUri) { throw new Error("REDIS_URI environment variable is not set."); } // If client is null or has been disconnected (status 'end'), create a new one. if (redisClient === null || redisClient.status === 'end') { redisClient = new Redis(redisUri, { retryStrategy: times => Math.min(times * 50, 2000), maxRetriesPerRequest: 3, enableOfflineQueue: true, enableReadyCheck: true, connectTimeout: 5000, }); } return redisClient; }
- src/tools/redis_tool.ts:32-35 (helper)Set of blocked dangerous Redis commands to prevent destructive operations.const DANGEROUS_COMMANDS = new Set([ 'FLUSHALL', 'FLUSHDB', 'KEYS', 'SHUTDOWN', 'CONFIG', 'SCRIPT', 'EVAL', 'EVALSHA', 'SAVE', 'BGSAVE', 'SLAVEOF' ]);
- src/tools/redis_tool.ts:127-147 (helper)Formats Redis results: converts HGETALL array to object, buffers to strings, null to {result: null}.function formatRedisResults(command: string, results: any): any { if (results === null) { return { result: null }; } if (Array.isArray(results) && command.toUpperCase() === 'HGETALL') { const obj: Record<string, any> = {}; for (let i = 0; i < results.length; i += 2) { if (i + 1 < results.length) { obj[results[i]] = results[i + 1]; } } return obj; } if (Buffer.isBuffer(results)) { return results.toString(); } return results; }
- src/tools/redis_tool.ts:194-204 (helper)Cleanup function to disconnect Redis client on tool unload/reload.export async function destroy() { console.log("Destroy redis_tool"); if (redisClient) { try { // Forcefully disconnect for tool reloads redisClient.disconnect(); } catch (error: any) { console.error("Force disconnect error:", error.message); } } }