kv_set
Set a key-value pair in a space with optional time-to-live and keyspace for organized, time-limited data storage.
Instructions
Set key-value pair in a space with optional TTL and keyspace.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| key | Yes | Key to set | |
| val | Yes | Value to set | |
| keyspace | No | Keyspace to use | |
| ttl | No | Time to live in millis for value | |
| json | No | Output info as JSON (not table) |
Implementation Reference
- src/index.ts:1233-1257 (handler)The handler for the 'kv_set' tool. It extracts key, val, keyspace, ttl, and json arguments, builds the CLI 'set' command with appropriate flags, executes it via 'executeCohoCommand', and returns the result as text content.
case "kv_set": { const { key, val, keyspace, ttl, json } = args as KvSetArgs; const setArgs = [ 'set', '--project', config.projectId, '--space', config.space, key, val ]; if (keyspace) setArgs.push('--keyspace', keyspace); if (ttl) setArgs.push('--ttl', ttl.toString()); if (json) setArgs.push('--json'); const result = await executeCohoCommand(setArgs); return { content: [ { type: "text", text: result } ], isError: false }; } - src/index.ts:155-161 (schema)The Zod schema (kvSetSchema) defining input validation for 'kv_set': key (required string), val (required string), keyspace (optional string), ttl (optional number), json (optional boolean).
const kvSetSchema = z.object({ key: z.string().describe("Key to set"), val: z.string().describe("Value to set"), keyspace: z.string().optional().describe("Keyspace to use"), ttl: z.number().optional().describe("Time to live in millis for value"), json: z.boolean().optional().describe("Output info as JSON (not table)"), }); - src/index.ts:446-461 (registration)Registration of the 'kv_set' tool in the tools array with name, description, schema reference, and inputSchema for JSON Schema exposure.
{ name: "kv_set", description: "Set key-value pair in a space with optional TTL and keyspace.", schema: kvSetSchema, inputSchema: { type: "object", properties: { key: { type: "string", description: "Key to set" }, val: { type: "string", description: "Value to set" }, keyspace: { type: "string", description: "Keyspace to use" }, ttl: { type: "number", description: "Time to live in millis for value" }, json: { type: "boolean", description: "Output info as JSON (not table)" } }, required: ["key", "val"] } }, - src/index.ts:529-564 (helper)Helper function 'executeCohoCommand' used by kv_set handler to run the 'coho' CLI with sanitized arguments and admin token.
async function executeCohoCommand(args: string[]): Promise<string> { const safeArgs = ['coho', ...args, '--admintoken', '***']; console.error(`Executing command: ${safeArgs.join(' ')}`); try { const { stdout, stderr } = await execFile('coho', [...args, '--admintoken', config.adminToken], { timeout: 120000 // 2 minutes timeout for CLI operations }); if (stderr) { // Sanitize stderr before logging to avoid token exposure const safeSterr = stderr.replace(new RegExp(config.adminToken, 'g'), '***'); console.error(`Command output to stderr:`, safeSterr); } console.error(`Command successful`); const result = stdout || stderr; // Sanitize result to ensure admin token is not exposed return result ? result.replace(new RegExp(config.adminToken, 'g'), '***') : result; } catch (error: any) { // Comprehensive sanitization of all error properties to avoid admin token exposure const sanitizeText = (text: string): string => text ? text.replace(new RegExp(config.adminToken, 'g'), '***') : text; const sanitizedMessage = sanitizeText(error?.message || 'Unknown error'); const sanitizedCmd = sanitizeText(error?.cmd || ''); const sanitizedStdout = sanitizeText(error?.stdout || ''); const sanitizedStderr = sanitizeText(error?.stderr || ''); // Log sanitized error details console.error(`Command failed: ${sanitizedMessage}`); if (sanitizedCmd) console.error(`Command: ${sanitizedCmd}`); if (sanitizedStdout) console.error(`Stdout: ${sanitizedStdout}`); if (sanitizedStderr) console.error(`Stderr: ${sanitizedStderr}`); // Return sanitized error message const errorDetails = [sanitizedMessage, sanitizedStderr].filter(Boolean).join(' - '); throw new McpError(ErrorCode.InvalidRequest, `Command failed: ${errorDetails}`); } }