redis_get
Retrieve a value from Redis by specifying a key. This tool fetches the stored data associated with the key, allowing access to the value.
Instructions
获取键值。
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| key | Yes |
Implementation Reference
- src/index.ts:165-177 (registration)Tool registration for 'redis_get' in the ListToolsRequestSchema handler, defining its name, description, and input schema (type: object, required: ['key'] with a string property 'key').
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:188-188 (handler)Handler function for 'redis_get': calls client.get(args.key) and returns a success message with the retrieved value (or 'null').
redis_get: async () => { const r = await client.get(args.key); return `GET 成功。键: ${args.key}, 值: ${r ?? 'null'}`; }, - src/index.ts:168-168 (schema)Input schema for 'redis_get': JSON Schema with type 'object', properties { key: { type: 'string' } }, required: ['key'].
{ name: "redis_get", description: "获取键值。", inputSchema: { type: "object", properties: { key: { type: "string" } }, required: ["key"] } }, - src/index.ts:135-156 (helper)Helper function getClient() that establishes and caches a Redis connection per project directory, used by the redis_get handler.
async function getClient(projectDir: string): Promise<any> { if (clientCache.has(projectDir)) return clientCache.get(projectDir)!; const config = getRedisConfig(projectDir); const client = createClient({ url: `redis://:${config.password}@${config.host}:${config.port}/${config.db}`, socket: { connectTimeout: OPERATION_TIMEOUT } }); client.on('error', (err: any) => console.error('Redis 错误:', err)); // 显式连接并带超时保护 try { await withTimeout(client.connect(), OPERATION_TIMEOUT, `Redis 连接超时 (${OPERATION_TIMEOUT}ms)`); console.error(`Redis 连接成功 [${projectDir}] -> ${config.host}:${config.port}/${config.db}`); } catch (error) { console.error(`Redis 连接失败:`, error); throw error; } clientCache.set(projectDir, client); return client; }