Add split_transaction and unsplit_transaction MCP tools

BA
11d ago
+599-0

Problem

Splitting a transaction is available in the web split editor, the REST API (/api/v1/transactions/:id/split) and rules, but not on the assistant/MCP surface. There, update_transaction refuses split children and points at "the split editor", which an MCP client can't open. The split rule action is also hidden from the MCP rule tools (SETTINGS_ONLY_ACTION_TYPES). So an agent has no way to, for example, record that half of a rent payment is a flatmate's share.

Refs #4725 (covers the split half; trade recording is out of scope here).

Behavior

Two new tools on the shared Assistant.function_classes registry, so they're available from the in-app assistant and /mcp:

  • split_transaction — splits a transaction into 2–20 parts that must sum exactly to it. Each part has its own name (defaults to the original's), amount, category, merchant, tags and excluded flag. Part amounts are positive magnitudes, as get_transactions returns them; each part takes the original's direction.
    • An already split transaction is only re-split when replace_existing: true; the parts are replaced, as in the REST PATCH.
    • Passing a child's id works on the whole split, as in the web and REST controllers.
    • Transfers, pending and excluded transactions are refused with the same reasons the REST API gives.
  • unsplit_transaction — removes the parts and restores the original.

Both go through the same locked model calls as the REST API (Entry#split_once!, #resplit!, #unsplit_once!), validate category/merchant/tag ids with Entry::SplitReferences.for_user and reject unknown ones, reject amounts with more than the column's 4 decimals rather than rounding them, and require owner or full control on the account. A failure to enqueue the post-split sync is reported as a warning on a successful result, as in create_transaction. Shared lookup, permission and serialization live in Assistant::Function::SplitSupport.

Docs: docs/hosting/mcp.md and docs/hosting/ai.md list the new tools.

Validation

  • Minitest: test/models/assistant/function/split_transaction_test.rb and unsplit_transaction_test.rb (split with an excluded part, per-part merchants and tags, income splits, default names, sum mismatch, negative and over-precise amounts, sync-enqueue warning, single part, non-numeric amount, foreign category, re-split guard, re-split via a child id, transfers, read-only collaborators, other families, unsplit via parent and child with children destroyed, not-split). mcp_controller_test.rb asserts both tools appear in tools/list.
  • Not yet run locally. The environment this was written in couldn't install gems, so only ruby -c was run on the changed files. This stays a draft until CI (tests, rubocop, brakeman) is green; I'll fix anything it reports before requesting review.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features
    • Split transactions into separately categorized and tagged parts, replace an existing split, or undo a split using AI and MCP tools.
    • Split parts can inherit transaction details, and their amounts are validated against the original transaction.
    • Splitting is limited to eligible transactions and users with sufficient account permissions.
  • Documentation
    • Added guidance for the transaction-splitting tools in the AI and MCP tool listings.