Skip to main content
Glama

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
NameRequiredDescriptionDefault
commandYes要执行的 Redis 命令 (例如, 'GET', 'SET')
argsNo命令的参数列表 (例如: ["mykey", "myvalue"])

Implementation Reference

  • 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,
            };
        }
    };
  • 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"]
    };
  • 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;
    }
  • 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'
    ]);
  • 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;
    }
  • 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);
            }
        }
    }
Behavior1/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

Tool has no description.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness1/5

Is the description appropriately sized, front-loaded, and free of redundancy?

Tool has no description.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness1/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Tool has no description.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters1/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Tool has no description.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose1/5

Does the description clearly state what the tool does and how it differs from similar tools?

Tool has no description.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines1/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

Tool has no description.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

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/xiaoguomeiyitian/ToolBox'

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