add-metadata
Add custom metadata (key-value pairs) to specific tasks in Todo.txt files using task ID, enabling enhanced task management and categorization.
Instructions
Add custom metadata (key-value pairs) to a task by ID.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| metadata | Yes | ||
| taskId | Yes |
Implementation Reference
- src/tools.ts:221-249 (registration)Full registration of the 'add-metadata' MCP tool, including name, description, input schema, and inline handler function.server.tool( "add-metadata", "Add custom metadata (key-value pairs) to a task by ID.", { taskId: z.number(), metadata: z.record(z.string()), }, async ({ taskId, metadata }) => { const tasks = await loadTasks(); const idx = getTaskIndex(taskId, tasks); if (idx === null) { return { content: [ { type: "text", text: "Invalid task ID." }, ], isError: true, }; } Object.entries(metadata).forEach(([key, value]) => { tasks[idx].setExtension(key as string, value as string); }); await saveTasks(tasks); return { content: [ { type: "text", text: "Metadata added successfully." }, ], }; } );
- src/tools.ts:228-248 (handler)Handler logic for 'add-metadata': loads tasks, validates task ID, sets extensions (metadata key-values) on the task using jstodotxt Item.setExtension, persists changes with saveTasks.async ({ taskId, metadata }) => { const tasks = await loadTasks(); const idx = getTaskIndex(taskId, tasks); if (idx === null) { return { content: [ { type: "text", text: "Invalid task ID." }, ], isError: true, }; } Object.entries(metadata).forEach(([key, value]) => { tasks[idx].setExtension(key as string, value as string); }); await saveTasks(tasks); return { content: [ { type: "text", text: "Metadata added successfully." }, ], }; }
- src/tools.ts:224-227 (schema)Input schema using Zod: taskId as number (1-based), metadata as object of string keys to string values.{ taskId: z.number(), metadata: z.record(z.string()), },
- src/tools.ts:14-17 (helper)Shared helper function used by the add-metadata handler (and others) to map 1-based task ID to 0-based array index.function getTaskIndex(taskId: number, tasks: Item[]): number | null { const idx = taskId - 1; if (idx < 0 || idx >= tasks.length) return null; return idx;