Skip to main content
Glama
minhyeoky

Ledger CLI MCP Server

by minhyeoky

ledger_raw_command

Execute raw Ledger CLI commands to query and analyze financial data directly through the Model Context Protocol server, enabling tasks like financial reporting and budget analysis.

Instructions

Run a raw ledger command

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
paramsYes

Implementation Reference

  • main.py:264-266 (handler)
    The handler function for the 'ledger_raw_command' tool. It is registered via the @mcp.tool decorator and executes the raw ledger command by delegating to the run_ledger helper function.
    @mcp.tool(description="Run a raw ledger command")
    def ledger_raw_command(params: LedgerRawCommand) -> str:
        return run_ledger(params.command)
  • Pydantic input schema for the ledger_raw_command tool, defining the required 'command' field as a list of strings for raw ledger arguments.
    class LedgerRawCommand(BaseModel):
        command: List[str] = Field(..., description="Raw ledger command arguments")
  • Helper utility function to execute arbitrary ledger CLI commands securely via subprocess, with input validation, error handling, and ledger file path management. Directly invoked by the ledger_raw_command handler.
    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
Install Server

Other Tools

Related Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/minhyeoky/mcp-server-ledger'

If you have feedback or need assistance with the MCP directory API, please join our Discord server