cache_put
Store any JSON-serializable value in a cache with a configurable time-to-live (TTL) to avoid redundant API calls or computations. Specify key, value, optional namespace, and TTL from 1 second to 24 hours.
Instructions
Store a value in the cache with TTL. Useful for caching API responses, computed values, etc.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| key | Yes | Cache key | |
| value | Yes | Value to cache (any JSON-serializable data) | |
| ttl_seconds | No | Time to live in seconds | |
| namespace | No | Optional namespace to prevent key collisions | default |
Implementation Reference
- src/index.ts:98-121 (registration)Tool registration with inputSchema for cache_put (name, description, inputSchema properties including key, value, ttl_seconds)
{ name: "cache_put", description: "Store a value in the cache with TTL", inputSchema: { type: "object", properties: { key: { type: "string", description: "Cache key" }, value: { description: "Value to cache (any JSON-serializable data)" }, ttl_seconds: { type: "number", description: "Time to live in seconds", default: 300, minimum: 1, maximum: 86400 // 24 hours } }, required: ["key", "value"] } }, - src/index.ts:305-322 (handler)Handler function for cache_put tool execution - stores value with TTL in an in-memory Map
case "cache_put": { const { key, value, ttl_seconds = 300 } = args as any; const expiresAt = Date.now() + (ttl_seconds * 1000); cache.set(key, { value, expiresAt }); return { content: [{ type: "text", text: JSON.stringify({ success: true, key, ttl_seconds, expires_at: new Date(expiresAt).toISOString() }) }] }; } - src/index-v2.ts:115-143 (registration)Alternate registration in index-v2.ts with additional namespace support
{ name: "cache_put", description: "Store a value in the cache with TTL. Useful for caching API responses, computed values, etc.", inputSchema: { type: "object", properties: { key: { type: "string", description: "Cache key" }, value: { description: "Value to cache (any JSON-serializable data)" }, ttl_seconds: { type: "number", description: "Time to live in seconds", default: 300, minimum: 1, maximum: 86400 // 24 hours }, namespace: { type: "string", description: "Optional namespace to prevent key collisions", default: "default" } }, required: ["key", "value"] } }, - src/index-v2.ts:441-461 (handler)Handler function for cache_put in index-v2.ts - adds namespace support using composite cache keys
case "cache_put": { const { key, value, ttl_seconds = 300, namespace = "default" } = args as any; const cacheKey = getCacheKey(key, namespace); const expiresAt = Date.now() + (ttl_seconds * 1000); cache.set(cacheKey, { value, expiresAt }); return { content: [{ type: "text", text: JSON.stringify({ success: true, key, namespace, ttl_seconds, expires_at: new Date(expiresAt).toISOString(), cache_size: cache.size }) }] }; } - src/index-v2.ts:274-276 (helper)Helper function getCacheKey used by cache_put handler in index-v2.ts to support namespaced cache keys
function getCacheKey(key: string, namespace: string = "default"): string { return `${namespace}:${key}`; }