get_hash
Retrieve all fields and values from a Redis hash using its key.
Instructions
获取哈希类型的所有字段和值
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| key | Yes | Redis键名 |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| result | Yes |
Implementation Reference
- src/redis_mcp_server/server.py:117-130 (registration)The tool 'get_hash' is registered as an MCP tool via @mcp.tool() decorator. It accepts a 'key' parameter (str) and returns a JSON string of the hash fields and values.
@mcp.tool() def get_hash(key: str) -> str: """获取哈希类型的所有字段和值 Args: key: Redis键名 """ try: result = db.get_hash(key) return json.dumps(result, ensure_ascii=False, indent=2, default=str) except PermissionError as e: return json.dumps({"error": str(e)}, ensure_ascii=False) except Exception as e: return json.dumps({"error": str(e)}, ensure_ascii=False) - src/redis_mcp_server/db.py:186-206 (handler)The RedisConnection.get_hash() method contains the actual business logic: it checks key permissions, uses HLEN to get the hash size, iterates with HSCAN to fetch fields (up to max_key_count), truncates long values, and returns the result dictionary.
def get_hash(self, key: str) -> dict[str, Any]: """获取哈希类型的值""" if not self._is_key_allowed(key): raise PermissionError(f"键 '{key}' 不允许访问") size = self._client.hlen(key) if size == 0: return {"key": key, "type": "hash", "fields": {}, "size": 0, "exists": False} fields = {} for field, value in self._client.hscan_iter(key, count=self.config.max_key_count): if len(fields) >= self.config.max_key_count: break fields[field] = self._truncate_value(value) return { "key": key, "type": "hash", "fields": fields, "size": size, "returned": len(fields), "truncated": len(fields) >= self.config.max_key_count, "exists": True, } - src/redis_mcp_server/db.py:234-250 (helper)The get_key_value() method in RedisConnection dispatches to get_hash() when the key's type is 'hash', serving as an entry point for automatic type-based value retrieval.
def get_key_value(self, key: str) -> dict[str, Any]: """自动识别键类型并获取值""" key_info = self.get_key_type(key) key_type = key_info["type"] if key_type == "string": return self.get_string(key) elif key_type == "list": return self.get_list(key) elif key_type == "set": return self.get_set(key) elif key_type == "hash": return self.get_hash(key) elif key_type == "zset": return self.get_zset(key) else: return {"key": key, "type": key_type, "note": f"暂不支持 {key_type} 类型的自动读取"}