feat(mcp): add delete_category and let update_category move categories

Merged
AL
1mo ago
+239-8

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 id and an optional replacement_id. It goes through Category#replace_and_destroy!, the path Category::DeletionsController uses:
    • transactions move to the replacement, or become uncategorized when replacement_id is 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.
  • Guests are refused (forbidden), matching the require_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_parent callback.
  • 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, returns validation_failed. A category can't be its own parent (invalid_parent), and an unknown parent returns parent_not_found.
  • Omitting parent_id leaves 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). DeleteCategory should 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 empty parent_id, the two-level limit, self-parent, unknown parent.
  • test/models/assistant and test/controllers/mcp_controller_test.rb pass on the rebased branch (476 runs, 0 failures, 0 errors).
  • Full bin/rails test in the devcontainer: 0 errors. The only 2 failures come from CRLF line endings in a Windows checkout (the provider generator template test and the database.yml username: check) and also fail on main there.
  • RuboCop and Brakeman are clean. There are no view, JS or /api/v1 changes, 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.