ledger_print
Filter and print financial transactions in ledger format using date ranges or regex patterns for reporting and analysis with Ledger CLI.
Instructions
Print transactions in ledger format
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| params | Yes |
Implementation Reference
- main.py:218-229 (handler)The handler function for the 'ledger_print' tool, decorated with @mcp.tool for registration. It builds the 'ledger print' command based on input parameters and executes it via the run_ledger helper.@mcp.tool(description="Print transactions in ledger format") def ledger_print(params: LedgerPrint) -> str: cmd = ["print"] 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]) return run_ledger(cmd)
- main.py:72-82 (schema)Pydantic input schema (BaseModel) defining parameters for the ledger_print tool: query, begin_date, and end_date.class LedgerPrint(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)" )
- main.py:107-129 (helper)Helper function used by ledger_print (and other tools) to safely execute ledger CLI commands with input validation and error handling.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