redis_hget
Get the value of a field from a Redis hash. Provide the key and field name to retrieve the stored value, enabling targeted access to hash data without fetching the entire hash.
Instructions
获取哈希字段。
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| key | Yes | ||
| field | Yes |
Implementation Reference
- src/index.ts:192-192 (handler)The handler function for the redis_hget tool. Calls client.hGet(args.key, args.field) and returns the result string.
redis_hget: async () => { const r = await client.hGet(args.key, args.field); return `HGET 成功。${args.key}.${args.field} = ${r ?? 'null'}`; }, - src/index.ts:173-173 (schema)Input schema registration for redis_hget tool. Defines 'key' and 'field' as required string parameters.
{ name: "redis_hget", description: "获取哈希字段。", inputSchema: { type: "object", properties: { key: { type: "string" }, field: { type: "string" } }, required: ["key", "field"] } }, - src/index.ts:165-177 (registration)Tool registration listing all tools including redis_hget under ListToolsRequestSchema handler.
server.setRequestHandler(ListToolsRequestSchema, async () => ({ tools: [ { name: "redis_set", description: "设置键值。自动读取项目 .env 配置连接 Redis。", inputSchema: { type: "object", properties: { key: { type: "string" }, value: { type: "string" } }, required: ["key", "value"] } }, { name: "redis_get", description: "获取键值。", inputSchema: { type: "object", properties: { key: { type: "string" } }, required: ["key"] } }, { name: "redis_del", description: "删除键。", inputSchema: { type: "object", properties: { key: { type: "string" } }, required: ["key"] } }, { name: "redis_exists", description: "检查键是否存在。", inputSchema: { type: "object", properties: { key: { type: "string" } }, required: ["key"] } }, { name: "redis_info", description: "获取连接信息。", inputSchema: { type: "object", properties: {} } }, { name: "redis_hset", description: "设置哈希字段。", inputSchema: { type: "object", properties: { key: { type: "string" }, field: { type: "string" }, value: { type: "string" } }, required: ["key", "field", "value"] } }, { name: "redis_hget", description: "获取哈希字段。", inputSchema: { type: "object", properties: { key: { type: "string" }, field: { type: "string" } }, required: ["key", "field"] } }, { name: "redis_hgetall", description: "获取哈希所有字段。", inputSchema: { type: "object", properties: { key: { type: "string" } }, required: ["key"] } }, { name: "redis_hdel", description: "删除哈希字段。", inputSchema: { type: "object", properties: { key: { type: "string" }, fields: { type: "array", items: { type: "string" } } }, required: ["key", "fields"] } } ] })); - src/index.ts:179-197 (registration)CallToolRequestSchema handler that dispatches to redis_hget via the ops record.
server.setRequestHandler(CallToolRequestSchema, async (request) => { try { const projectDir = getProjectDir(); const client = await getClient(projectDir); const args = request.params.arguments as any; const ops: Record<string, () => Promise<string>> = { redis_info: async () => `Redis MCP 信息\n项目目录: ${projectDir}\n连接数: ${clientCache.size}`, redis_set: async () => { const r = await client.set(args.key, args.value); return `SET 成功。键: ${args.key}`; }, redis_get: async () => { const r = await client.get(args.key); return `GET 成功。键: ${args.key}, 值: ${r ?? 'null'}`; }, redis_del: async () => { const r = await client.del(args.key); return `DEL 成功。删除 ${r} 个键`; }, redis_exists: async () => { const r = await client.exists(args.key); return `EXISTS 成功。键 ${args.key} ${r > 0 ? '存在' : '不存在'}`; }, redis_hset: async () => { await client.hSet(args.key, args.field, args.value); return `HSET 成功。${args.key}.${args.field} = ${args.value}`; }, redis_hget: async () => { const r = await client.hGet(args.key, args.field); return `HGET 成功。${args.key}.${args.field} = ${r ?? 'null'}`; }, redis_hgetall: async () => { const r = await client.hGetAll(args.key); return `HGETALL 成功。${args.key}: ${JSON.stringify(r)}`; }, redis_hdel: async () => { const r = await client.hDel(args.key, args.fields); return `HDEL 成功。删除 ${r} 个字段`; } }; if (!ops[request.params.name]) throw new McpError(ErrorCode.MethodNotFound, `未知工具: ${request.params.name}`);