toggle_todo
Change the completion status of a specific todo item by its ID in the MCP Todo server's persistent task list.
Instructions
Toggle a todo by id
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| id | Yes |
Implementation Reference
- src/server.ts:95-111 (handler)Executes the toggle_todo tool: loads todos, finds the todo by ID, flips its 'done' status, saves the list, and returns a confirmation or error message.async ({ id }) => { const todos = await readTodos(); const i = todos.findIndex((t) => t.id === id); if (i === -1) return { content: [{ type: "text", text: `No todo with id ${id}` }] }; const todo = todos[i]!; todo.done = !todo.done; await writeTodos(todos); return { content: [ { type: "text", text: `Toggled #${id} to ${todo.done ? "done" : "not done"}`, }, ], }; }
- src/server.ts:90-94 (schema)Tool metadata and input schema for toggle_todo: requires a positive integer 'id'.{ title: "Toggle Todo", description: "Toggle a todo by id", inputSchema: { id: z.number().int().positive() }, },
- src/server.ts:88-112 (registration)Registers the toggle_todo tool on the MCP server with schema and handler function.server.registerTool( "toggle_todo", { title: "Toggle Todo", description: "Toggle a todo by id", inputSchema: { id: z.number().int().positive() }, }, async ({ id }) => { const todos = await readTodos(); const i = todos.findIndex((t) => t.id === id); if (i === -1) return { content: [{ type: "text", text: `No todo with id ${id}` }] }; const todo = todos[i]!; todo.done = !todo.done; await writeTodos(todos); return { content: [ { type: "text", text: `Toggled #${id} to ${todo.done ? "done" : "not done"}`, }, ], }; } );
- src/server.ts:20-30 (helper)Helper functions readTodos and writeTodos for loading and saving the todo list from/to JSON file, used by the toggle_todo handler.async function readTodos(): Promise<Todo[]> { try { const s = await fs.readFile(DB_PATH, "utf-8"); return JSON.parse(s); } catch { return []; } } async function writeTodos(todos: Todo[]) { await fs.writeFile(DB_PATH, JSON.stringify(todos, null, 2), "utf-8"); }