cache_get
Retrieve a cached value by key, returning null if the key is missing or expired. Supports optional namespaces to prevent collisions.
Instructions
Get a value from the cache by key. Returns null if not found or expired.
Input 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:268-303 (handler)The cache_get tool handler in the original implementation. Retrieves a value from cache by key, checks for expiration, and returns the value with remaining TTL.
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-v2.ts:393-439 (handler)The cache_get tool handler in the v2 implementation. Retrieves a value from cache by key (with namespace support), checks for expiration, and returns the value with remaining TTL.
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) }) }] }; } - src/index.ts:84-97 (schema)Schema definition for cache_get in the original implementation. Requires 'key' parameter (string).
{ 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-v2.ts:96-114 (schema)Schema definition for cache_get in the v2 implementation. Requires 'key' (string) with optional 'namespace' (string, default 'default').
{ 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.ts:189-191 (registration)The 'cache_get' tool is registered via the 'tools' array (line 85) which is provided to the ListToolsRequestSchema handler. The call handler at line 211 dispatches to the case at line 268.
server.setRequestHandler(ListToolsRequestSchema, async () => { return { tools }; });