feat(mcp): add delete_category and let update_category move categories
Merged
AL
1mo ago+239+239+012345678901234567890123456789-8-8-0123456789
Summary
The MCP server can already create categories (create_category) and rename, recolor or re-icon them (update_category). Two operations from the Categories settings page are still missing:
- deleting a category, and
- moving a category in the hierarchy (making it a subcategory, or promoting a subcategory to top-level).
This PR adds delete_category and a parent_id parameter on update_category.
Behavior
delete_category
- Takes
idand an optionalreplacement_id. It goes throughCategory#replace_and_destroy!, the pathCategory::DeletionsControlleruses:- transactions move to the replacement, or become uncategorized when
replacement_idis omitted; - subcategories of a deleted top-level category are kept and become top-level (
dependent: :nullify); - budget allocations for the category are removed (
dependent: :destroy). Recurring transactions are nullified by their foreign key, as in the web flow.
- transactions move to the replacement, or become uncategorized when
- Guests are refused (
forbidden), matching therequire_non_guest!guard on the web deletion flow. - Categories from other families return
not_found, and so do replacements from other families (replacement_not_found). A category can't replace itself (invalid_replacement). - The description tells the agent the action can't be undone and to confirm with the user first.
update_category + parent_id
- A top-level category id moves the category under that parent. The category takes the parent's color, through the existing
inherit_color_from_parentcallback. - An empty string makes the category top-level.
- The model's two-level limit (
category_level_limit) still applies. Moving a category that has subcategories, or nesting under a subcategory, returnsvalidation_failed. A category can't be its own parent (invalid_parent), and an unknown parent returnsparent_not_found. - Omitting
parent_idleaves the hierarchy unchanged, so existing callers behave as before.
delete_category sits in the standard registry next to create_category and update_category, and docs/hosting/mcp.md and docs/hosting/ai.md list it.
Related PRs
- #3025 also adds a
delete_category, as part of a much larger CRUD batch. This PR covers categories only and can be reviewed on its own. If #3025 lands first, I'll rebase onto it. - #4005 hides the family-config tools from guests (
FAMILY_CONFIG_FUNCTION_CLASSES).DeleteCategoryshould join that list if it merges. Meanwhile the tool rejects guests itself.
Tests
delete_category: uncategorizing transactions, moving them to a replacement, promoting subcategories, unknown id, self-replacement, cross-family id and replacement, guest refusal.update_category: moving under a parent with color inheritance, promoting with an emptyparent_id, the two-level limit, self-parent, unknown parent.test/models/assistantandtest/controllers/mcp_controller_test.rbpass on the rebased branch (476 runs, 0 failures, 0 errors).- Full
bin/rails testin the devcontainer: 0 errors. The only 2 failures come from CRLF line endings in a Windows checkout (the provider generator template test and thedatabase.ymlusername:check) and also fail onmainthere. - RuboCop and Brakeman are clean. There are no view, JS or
/api/v1changes, so neither erb_lint, Biome nor rswag applies.
🤖 Generated with Claude Code
Summary by CodeRabbit
- New Features
- Assistants can delete categories, leaving transactions uncategorized or moving them to a replacement category.
- Deleting a top-level category promotes its subcategories to top-level categories and removes its budget allocations.
- Categories can be moved under another category or promoted to top-level status.
- Bug Fixes
- Added validation for missing, invalid, or self-referential parent and replacement categories.
- Documentation
- Updated available-tool documentation to cover category deletion and category hierarchy management.