ledger_register
Filter, sort, and group financial transactions by date, payee, or period using regex patterns and customizable parameters for precise financial analysis.
Instructions
Show transaction register
Tool call template
Replace each placeholder with a value matching the input schema. Only required parameters are included; add optional parameters as needed.
{
"id": 1,
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"arguments": {
"params": "<params>"
},
"name": "ledger_register"
}
}Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| params | Yes |
Implementation Reference
- main.py:35-55 (schema)Pydantic model defining the input schema for the ledger_register tool parameters.
class LedgerRegister(BaseModel): query: Optional[str] = Field( None, description="Filter transactions by regex pattern" ) begin_date: Optional[str] = Field( None, description="Start date for transactions (YYYY/MM/DD)" ) end_date: Optional[str] = Field( None, description="End date (exclusive) for transactions (YYYY/MM/DD)" ) monthly: bool = Field(False, description="Group by month") weekly: bool = Field(False, description="Group by week") daily: bool = Field(False, description="Group by day") yearly: bool = Field(False, description="Group by year") sort: Optional[str] = Field( None, description="Sort transactions (date, amount, payee)" ) by_payee: bool = Field(False, description="Group by payee") current: bool = Field( False, description="Show only transactions on or before today" ) - main.py:160-185 (handler)The handler function for the ledger_register tool. It constructs the ledger CLI command based on parameters and executes it using run_ledger helper.
@mcp.tool(description="Show transaction register") def ledger_register(params: LedgerRegister) -> str: cmd = ["register"] if params.query: cmd.append(params.query) if params.begin_date: cmd.extend(["-b", params.begin_date]) if params.end_date: cmd.extend(["-e", params.end_date]) if params.monthly: cmd.append("--monthly") if params.weekly: cmd.append("--weekly") if params.daily: cmd.append("--daily") if params.yearly: cmd.append("--yearly") if params.sort: cmd.extend(["-S", params.sort]) if params.by_payee: cmd.append("-P") if params.current: cmd.append("-c") return run_ledger(cmd) - main.py:107-129 (helper)Shared helper function that executes ledger CLI commands safely, used by ledger_register and other tools.
def run_ledger(args: List[str]) -> str: try: if not LEDGER_FILE: return "Ledger file path not set. Please provide it via --ledger-file argument or LEDGER_FILE environment variable." # Validate inputs to prevent command injection for arg in args: if ";" in arg or "&" in arg or "|" in arg: return "Error: Invalid characters in command arguments." result = subprocess.run( ["ledger", "-f", LEDGER_FILE] + args, check=True, text=True, capture_output=True, ) return result.stdout except subprocess.CalledProcessError as e: error_message = f"Ledger command failed: {e.stderr}" if "couldn't find file" in e.stderr: error_message = f"Ledger file not found at {LEDGER_FILE}. Please provide a valid path via --ledger-file argument or LEDGER_FILE environment variable." return error_message