Update Category
updateCategoryUpdate an existing category by its ID, with an optional new name.
Instructions
Update a category
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| id | Yes | The ID of the category | |
| name | No |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| category | Yes |
Implementation Reference
- src/repository/categories.ts:34-57 (handler)The main handler/repository function that updates a category by ID. Reads the DB, finds the category by index, merges the new name (if provided), updates the timestamp, writes to DB, and returns the updated category.
export const updateCategory = async ( id: string, { name }: UpdateCategory, ): Promise<Category | undefined> => { await db.read() const index = db.data.categories.findIndex((c) => c.id === id) if (index === -1) { return undefined } const existingCategory = db.data.categories[index] if (!existingCategory) { return undefined } const newCategory: Category = { ...existingCategory, ...(name ? { name } : {}), updatedAt: new Date().toISOString(), } db.data.categories[index] = newCategory await db.write() return newCategory - src/server/tools.ts:219-244 (handler)The tool handler registered with the MCP server. Receives the id and category fields, calls the repository updateCategory function, and returns success or error content.
server.registerTool( "updateCategory", { description: "Update a category", inputSchema: { id: z.string().describe("The ID of the category"), ...UpdateCategorySchema.shape, }, outputSchema: { category: CategorySchema }, title: "Update Category", }, async ({ id, ...category }) => { const updatedCategory = await updateCategory(id, category) if (!updatedCategory) { return { content: [{ text: "Category not found", type: "text" }], isError: true, } } return { content: [{ text: JSON.stringify(updatedCategory), type: "text" }], structuredContent: { category: updatedCategory }, } }, ) - src/schemas/categories.ts:18-22 (schema)Zod schema defining the input for updateCategory: an optional name field (string, min length 1).
export const UpdateCategorySchema = z.object({ name: z.string().min(1).optional(), }) export type UpdateCategory = z.infer<typeof UpdateCategorySchema> - src/schemas/categories.ts:22-22 (schema)TypeScript type inferred from the UpdateCategorySchema.
export type UpdateCategory = z.infer<typeof UpdateCategorySchema> - src/server/tools.ts:219-244 (registration)Registration of the 'updateCategory' tool on the MCP server via server.registerTool(), including description, input/output schemas, and the handler callback.
server.registerTool( "updateCategory", { description: "Update a category", inputSchema: { id: z.string().describe("The ID of the category"), ...UpdateCategorySchema.shape, }, outputSchema: { category: CategorySchema }, title: "Update Category", }, async ({ id, ...category }) => { const updatedCategory = await updateCategory(id, category) if (!updatedCategory) { return { content: [{ text: "Category not found", type: "text" }], isError: true, } } return { content: [{ text: JSON.stringify(updatedCategory), type: "text" }], structuredContent: { category: updatedCategory }, } }, )