cache_put
Store JSON-serializable data in the cache with a specified TTL (Time to Live) to efficiently manage API responses, computed values, or other frequently accessed data.
Instructions
Store a value in the cache with TTL. Useful for caching API responses, computed values, etc.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| key | Yes | Cache key | |
| namespace | No | Optional namespace to prevent key collisions | default |
| ttl_seconds | No | Time to live in seconds | |
| value | Yes | Value to cache (any JSON-serializable data) |
Implementation Reference
- src/index.ts:305-322 (handler)Handler function for the 'cache_put' tool. Parses arguments, computes expiration time, stores the entry in the cache Map, and returns success response with metadata.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.ts:98-121 (schema)Input schema definition for the 'cache_put' tool, specifying parameters key (required string), value (required any), and ttl_seconds (optional number with defaults and bounds).{ 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:189-191 (registration)Registration of the list tools handler, which exposes the 'cache_put' tool (included in the tools array) to MCP clients.server.setRequestHandler(ListToolsRequestSchema, async () => { return { tools }; });
- src/index.ts:12-18 (helper)Cache storage using Map<string, CacheEntry> to hold cache entries with value and expiration timestamp, used by cache_put handler.// Cache storage with TTL support interface CacheEntry { value: any; expiresAt: number; } const cache = new Map<string, CacheEntry>();
- src/index.ts:20-28 (helper)Periodic cleanup interval that removes expired entries from the cache, supporting the TTL functionality of cache_put.// 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