Skip to content

Refactor: Introduce services layer to separate business logic from HTTP client #34

@RadCod3

Description

@RadCod3

Problem

The FireflyClient in clients/firefly.py is currently handling too many responsibilities:

  1. ✅ HTTP communication (appropriate)
  2. ✅ Request/response serialization (appropriate)
  3. Business logic - aggregations, calculations (should be elsewhere)
  4. Multi-call orchestration - making multiple API calls and combining results (should be elsewhere)
  5. 🤔 Model conversion (debatable)

Examples of business logic in the client:

  • get_budget_spending(): Aggregates data from multiple limit entries, calculates percentages
  • get_budget_summary(): Orchestrates multiple API calls and combines results
  • create_bulk_transactions(): Orchestrates multiple individual creates

This violates the Single Responsibility Principle and makes the client harder to test and maintain.

Proposed Solution

Introduce a services layer between the MCP tools and the Firefly client:

MCP Tools → Services → Firefly Client → Firefly API

New Structure

src/lampyrid/
├── tools/              # MCP interface layer (thin)
│   ├── accounts.py     # MCP tool definitions only
│   ├── transactions.py
│   └── budgets.py
├── services/           # NEW: Business logic layer
│   ├── __init__.py
│   ├── accounts.py     # AccountService - orchestration, aggregation
│   ├── transactions.py # TransactionService
│   └── budgets.py      # BudgetService
├── clients/            # API client layer (thin)
│   └── firefly.py      # Pure HTTP client, returns raw Firefly models
├── models/
│   ├── firefly_models.py
│   └── lampyrid_models.py
└── config.py

Responsibility Distribution

Tools Layer (MCP interface):

  • Register MCP tools
  • Input validation (Pydantic)
  • Call service methods
  • Return results directly

Services Layer (Business logic):

  • Orchestrate multiple client calls
  • Business logic (aggregations, calculations)
  • Model conversion (Firefly → LamPyrid)
  • Return LamPyrid models

Client Layer (HTTP only):

  • HTTP communication
  • Request parameter formatting
  • Response parsing to Firefly models
  • Error handling (HTTP status)
  • Return raw Firefly models

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions