Skip to main content
Glama
lm203688

redis-mcp-server

by lm203688

scan_keys

Scan and retrieve Redis key names matching a specified pattern, with an optional limit on the number of keys returned.

Instructions

扫描匹配的键

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
patternNo匹配模式(如 user:* 、session:* ),默认 **
countNo最多返回键数量,默认100

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault
resultYes

Implementation Reference

  • MCP tool handler for 'scan_keys'. Decorated with @mcp.tool(), it accepts optional pattern and count parameters, calls db.scan_keys(), and returns JSON-serialized results.
    @mcp.tool()
    def scan_keys(pattern: str = "*", count: Optional[int] = None) -> str:
        """扫描匹配的键
    
        Args:
            pattern: 匹配模式(如 user:* 、session:* ),默认 *
            count: 最多返回键数量,默认100
        """
        try:
            result = db.scan_keys(pattern, count)
            return json.dumps(result, ensure_ascii=False, indent=2)
        except Exception as e:
            return json.dumps({"error": str(e)}, ensure_ascii=False)
  • Core database method RedisConnection.scan_keys(). Uses Redis SCAN command to iterate over keys matching a pattern, respects max_key_count config, filters blocked/disallowed keys, and returns a dict with pattern, keys list, count, and truncated flag.
    def scan_keys(
        self, pattern: str = "*", count: Optional[int] = None
    ) -> dict[str, Any]:
        """扫描匹配的键"""
        max_count = count or self.config.max_key_count
        keys = []
        cursor = 0
        while True:
            cursor, batch = self._client.scan(cursor=cursor, match=pattern, count=min(max_count, 100))
            keys.extend(batch)
            if cursor == 0 or len(keys) >= max_count:
                break
        keys = keys[:max_count]
        # 过滤不允许访问的键
        keys = [k for k in keys if self._is_key_allowed(k)]
        return {
            "pattern": pattern,
            "keys": keys,
            "count": len(keys),
            "truncated": len(keys) >= max_count,
        }
  • Registration via @mcp.tool() decorator on line 70 registers 'scan_keys' as a tool in the FastMCP server.
    @mcp.tool()
    def scan_keys(pattern: str = "*", count: Optional[int] = None) -> str:
        """扫描匹配的键
    
        Args:
            pattern: 匹配模式(如 user:* 、session:* ),默认 *
            count: 最多返回键数量,默认100
        """
        try:
            result = db.scan_keys(pattern, count)
            return json.dumps(result, ensure_ascii=False, indent=2)
        except Exception as e:
            return json.dumps({"error": str(e)}, ensure_ascii=False)
  • Input schema defined by function signature: pattern (str, default '*') and count (Optional[int]). Docstring describes parameters.
    def scan_keys(pattern: str = "*", count: Optional[int] = None) -> str:
        """扫描匹配的键
    
        Args:
            pattern: 匹配模式(如 user:* 、session:* ),默认 *
            count: 最多返回键数量,默认100
        """
  • Test confirming 'scan_keys' is registered among expected tool names.
    def test_server_has_tools(self):
        tool_names = list(self.server.mcp._tool_manager._tools.keys())
        expected = [
            "ping", "server_info", "db_size", "scan_keys",
            "get_key_type", "get_key_value", "get_hash",
            "get_memory_usage", "set_string", "delete_keys", "set_expire",
        ]
        for t in expected:
            assert t in tool_names, f"缺少工具: {t}"
Behavior2/5

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

With no annotations provided, description fails to disclose behavioral traits like performance impact (scanning can be slow on large datasets), whether it is read-only or blocking, or any side effects. The simple verb 'scan' implies non-destructive but lacks explicit clarification.

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

Conciseness4/5

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

Single sentence is concise and front-loaded with the core action. However, it lacks any additional structure or context, which would be beneficial.

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

Completeness3/5

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

Given full schema coverage and presence of an output schema, the description does not need to explain return values. However, it omits usage context and behavioral details, making it minimally adequate for a simple tool.

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

Parameters3/5

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

Schema has 100% coverage with descriptions for both parameters. The tool description adds no extra meaning beyond what the schema already provides, so baseline score of 3 applies.

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

Purpose4/5

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

Description '扫描匹配的键' clearly states verb ('scan') and resource ('matching keys'), making purpose understandable despite being minimal. It distinguishes from siblings like 'get_key_value' by indicating pattern-based scanning.

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

Usage Guidelines2/5

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

No guidance on when to use this tool versus alternatives such as 'get_key_value' or 'delete_keys'. No mention of prerequisites or scenarios where scanning is appropriate.

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/lm203688/redis-mcp-server'

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