cache_get
Retrieve cached data by specifying a key using this tool. Optionally, use a namespace to avoid key conflicts. Returns null if the key is not found or expired.
Instructions
Get a value from the cache by key. Returns null if not found or expired.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| key | Yes | Cache key to retrieve | |
| namespace | No | Optional namespace to prevent key collisions | default |
Implementation Reference
- src/index.ts:84-97 (registration)Registration of the 'cache_get' tool in the tools list, including name, description, and input schema.{ name: "cache_get", description: "Get a value from the cache by key", inputSchema: { type: "object", properties: { key: { type: "string", description: "Cache key to retrieve" } }, required: ["key"] } },
- src/index.ts:268-303 (handler)The handler logic for the 'cache_get' tool. It retrieves the cache entry by key, checks if it exists and is not expired, and returns a JSON response with the value or appropriate error information.case "cache_get": { const { key } = args as { key: string }; const entry = cache.get(key); if (!entry) { return { content: [{ type: "text", text: JSON.stringify({ found: false, key }) }] }; } // Check if expired if (entry.expiresAt <= Date.now()) { cache.delete(key); return { content: [{ type: "text", text: JSON.stringify({ found: false, key, reason: "expired" }) }] }; } return { content: [{ type: "text", text: JSON.stringify({ found: true, key, value: entry.value, expires_in_seconds: Math.floor((entry.expiresAt - Date.now()) / 1000) }) }] }; }
- src/index.ts:13-28 (helper)Cache storage implementation using a Map with TTL support, including CacheEntry interface, the cache Map, and periodic cleanup interval used by cache_get.interface CacheEntry { value: any; expiresAt: number; } const cache = new Map<string, CacheEntry>(); // Clean up expired cache entries periodically setInterval(() => { const now = Date.now(); for (const [key, entry] of cache.entries()) { if (entry.expiresAt <= now) { cache.delete(key); } } }, 60000); // Clean every minute
- src/index-v2.ts:96-113 (registration)Registration of the enhanced 'cache_get' tool in index-v2.ts with namespace support.{ name: "cache_get", description: "Get a value from the cache by key. Returns null if not found or expired.", inputSchema: { type: "object", properties: { key: { type: "string", description: "Cache key to retrieve" }, namespace: { type: "string", description: "Optional namespace to prevent key collisions", default: "default" } }, required: ["key"] }
- src/index-v2.ts:393-439 (handler)The handler logic for 'cache_get' in v2 version, supporting namespaces via getCacheKey helper.case "cache_get": { const { key, namespace = "default" } = args as any; const cacheKey = getCacheKey(key, namespace); const entry = cache.get(cacheKey); if (!entry) { return { content: [{ type: "text", text: JSON.stringify({ found: false, key, namespace }) }] }; } // Check if expired if (entry.expiresAt <= Date.now()) { cache.delete(cacheKey); return { content: [{ type: "text", text: JSON.stringify({ found: false, key, namespace, reason: "expired" }) }] }; } return { content: [{ type: "text", text: JSON.stringify({ found: true, key, namespace, value: entry.value, expires_in_seconds: Math.floor((entry.expiresAt - Date.now()) / 1000) }) }] }; }