# lugodoy/finance-agent-mcp architecture

> Architecture diagram of the GitHub repository lugodoy/finance-agent-mcp, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-09-01). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/lugodoy/finance-agent-mcp
- Repository: https://github.com/lugodoy/finance-agent-mcp

## Overview

#### Purpose and Entry Points
A Python conversational finance assistant for querying shared-expense records in natural language. The primary user entry point is the Streamlit application at `app/main.py`; it delegates interpretation and response generation to Gemini-backed agent code in `llm/client_gemini.py`. The MCP server can also run standalone via `mcp_server/server.py`.

#### Presentation Layer
`app/main.py` owns the Streamlit chat experience, session-facing interaction, and agent initialization/cache boundary. Styling is local to `app/styles.css`. The UI submits a user question and renders the agent’s final natural-language response; it does not query MySQL directly.

#### Agent and LLM Boundary
`llm/client_gemini.py` integrates Google Gemini and orchestrates tool calling. `llm/prompts.py` contains the system/few-shot prompt context that constrains the model to financial-query behavior and response formatting. Gemini interprets intent and selects available MCP tools; database access remains outside the model process.

#### MCP Tool Service
`mcp_server/server.py` exposes finance capabilities through Model Context Protocol, decoupling the agent from SQL implementation. Its core tool surface is:
- Expense-detail/item lookup: `mcp_server/tools/expense_items.py`
- Aggregate expense summaries by item and/or period: `mcp_server/tools/expense_summary.py`

The control path is Streamlit → Gemini agent → MCP tool invocation → data layer → tool result → Gemini-formatted response → Streamlit.

#### Data Access and Persistence
`database/connection.py` centralizes MySQL connection and parameterized-query execution for MCP tools. The operational schema is a `personal_finance.transactions` table containing item name, unit price, quantity, and purchase date; seed data is provisioned from `scripts/seed.sql`. This is the principal trust boundary: tools own query construction and return structured financial data, rather than granting direct SQL access to the LLM.

#### Runtime and Infrastructure
The runtime targets Python 3.13+, Streamlit, Google Gemini API, MCP SDK, MySQL Connector/Python, and MySQL 8.0. `docker-compose.yml` provisions the local MySQL service, schema, and sample data; environment values, including `GEMINI_API_KEY` and database credentials, are represented by `.env.example`. Shared runtime configuration/logging and date handling live in `shared/config_logging.py` and `shared/date_config.py`.

#### Extensibility Shape
The modular boundaries support adding MCP tools in `mcp_server/tools/`, changing Gemini prompts/client behavior in `llm/`, or replacing the LLM provider without moving database logic into the UI. The current architecture is single-database and implicitly single shared expense dataset; authentication, group scoping, and advanced analytics are not present.

Main entry points: [`llm/client_gemini.py`](https://github.com/lugodoy/finance-agent-mcp/blob/main/llm/client_gemini.py) (Gemini agent), [`mcp_server/server.py`](https://github.com/lugodoy/finance-agent-mcp/blob/main/mcp_server/server.py) (MCP server), [`database/connection.py`](https://github.com/lugodoy/finance-agent-mcp/blob/main/database/connection.py) (MySQL query layer), [`app/main.py`](https://github.com/lugodoy/finance-agent-mcp/blob/main/app/main.py) (Streamlit chat app)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_ui["Presentation"]
  node_streamlit["Streamlit chat app<br/>Python UI<br/>[main.py]"]
  node_styles["Chat styling<br/>CSS<br/>[styles.css]"]
end

subgraph group_agent["Agent and LLM"]
  node_gemini_client["Gemini agent<br/>LLM client<br/>[client_gemini.py]"]
  node_prompts["Finance prompts<br/>prompt context<br/>[prompts.py]"]
  node_gemini_api{{"Google Gemini API<br/>external LLM"}}
end

subgraph group_mcp["MCP service"]
  node_mcp_server["MCP server<br/>tool service<br/>[server.py]"]
  node_expense_items["Expense item lookup<br/>MCP tool<br/>[expense_items.py]"]
  node_expense_summary["Expense summaries<br/>MCP tool<br/>[expense_summary.py]"]
end

subgraph group_data["Data and runtime"]
  node_db_connection["MySQL query layer<br/>data access<br/>[connection.py]"]
  node_transactions[("Transactions table<br/>MySQL table")]
  node_mysql[("MySQL 8.0<br/>database runtime<br/>[docker-compose.yml]")]
  node_seed["Sample data seed<br/>SQL seed<br/>[seed.sql]"]
  node_runtime_config["Runtime config<br/>configuration<br/>[.env.example]"]
  node_shared_runtime["Shared runtime utilities<br/>Python utilities<br/>[config_logging.py]"]
end

node_styles -->|"styles"| node_streamlit
node_streamlit -->|"user question"| node_gemini_client
node_gemini_client -->|"loads context"| node_prompts
node_gemini_client -->|"interprets and generates"| node_gemini_api
node_gemini_client -->|"invokes tools"| node_mcp_server
node_mcp_server -->|"exposes"| node_expense_items
node_mcp_server -->|"exposes"| node_expense_summary
node_expense_items -->|"parameterized lookup"| node_db_connection
node_expense_summary -->|"parameterized aggregation"| node_db_connection
node_db_connection -->|"queries"| node_transactions
node_mysql -->|"hosts"| node_transactions
node_seed -->|"provisions"| node_transactions
node_runtime_config -.->|"API key"| node_gemini_client
node_runtime_config -.->|"DB credentials"| node_db_connection
node_shared_runtime -.->|"runtime support"| node_mcp_server
node_mcp_server -->|"structured results"| node_gemini_client
node_gemini_client -->|"formatted response"| node_streamlit

click node_streamlit "https://github.com/lugodoy/finance-agent-mcp/blob/main/app/main.py"
click node_styles "https://github.com/lugodoy/finance-agent-mcp/blob/main/app/styles.css"
click node_gemini_client "https://github.com/lugodoy/finance-agent-mcp/blob/main/llm/client_gemini.py"
click node_prompts "https://github.com/lugodoy/finance-agent-mcp/blob/main/llm/prompts.py"
click node_mcp_server "https://github.com/lugodoy/finance-agent-mcp/blob/main/mcp_server/server.py"
click node_expense_items "https://github.com/lugodoy/finance-agent-mcp/blob/main/mcp_server/tools/expense_items.py"
click node_expense_summary "https://github.com/lugodoy/finance-agent-mcp/blob/main/mcp_server/tools/expense_summary.py"
click node_db_connection "https://github.com/lugodoy/finance-agent-mcp/blob/main/database/connection.py"
click node_mysql "https://github.com/lugodoy/finance-agent-mcp/blob/main/docker-compose.yml"
click node_seed "https://github.com/lugodoy/finance-agent-mcp/blob/main/scripts/seed.sql"
click node_runtime_config "https://github.com/lugodoy/finance-agent-mcp/blob/main/.env.example"
click node_shared_runtime "https://github.com/lugodoy/finance-agent-mcp/blob/main/shared/config_logging.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_streamlit,node_styles toneBlue
class node_gemini_client,node_prompts,node_gemini_api toneAmber
class node_mcp_server,node_expense_items,node_expense_summary toneMint
class node_db_connection,node_transactions,node_mysql,node_seed,node_runtime_config,node_shared_runtime toneRose
```

## Components

### Presentation

- [Streamlit chat app](https://github.com/lugodoy/finance-agent-mcp/blob/main/app/main.py): Python UI · Owns chat interaction and agent caching. · `app/main.py`
- [Chat styling](https://github.com/lugodoy/finance-agent-mcp/blob/main/app/styles.css): CSS · `app/styles.css`

### Agent and LLM

- [Gemini agent](https://github.com/lugodoy/finance-agent-mcp/blob/main/llm/client_gemini.py): LLM client · Orchestrates Gemini and MCP tool calls. · `llm/client_gemini.py`
- [Finance prompts](https://github.com/lugodoy/finance-agent-mcp/blob/main/llm/prompts.py): prompt context · Constrains financial-query behavior and response format. · `llm/prompts.py`
- **Google Gemini API**: external LLM

### MCP service

- [MCP server](https://github.com/lugodoy/finance-agent-mcp/blob/main/mcp_server/server.py): tool service · Exposes finance capabilities without giving the model SQL access. · `mcp_server/server.py`
- [Expense item lookup](https://github.com/lugodoy/finance-agent-mcp/blob/main/mcp_server/tools/expense_items.py): MCP tool · `mcp_server/tools/expense_items.py`
- [Expense summaries](https://github.com/lugodoy/finance-agent-mcp/blob/main/mcp_server/tools/expense_summary.py): MCP tool · Aggregates expenses by item and/or period. · `mcp_server/tools/expense_summary.py`

### Data and runtime

- [MySQL query layer](https://github.com/lugodoy/finance-agent-mcp/blob/main/database/connection.py): data access · Centralizes parameterized query execution. · `database/connection.py`
- **Transactions table**: MySQL table · Shared expense records: item, price, quantity, and purchase date.
- [MySQL 8.0](https://github.com/lugodoy/finance-agent-mcp/blob/main/docker-compose.yml): database runtime · `docker-compose.yml`
- [Sample data seed](https://github.com/lugodoy/finance-agent-mcp/blob/main/scripts/seed.sql): SQL seed · `scripts/seed.sql`
- [Runtime config](https://github.com/lugodoy/finance-agent-mcp/blob/main/.env.example): configuration · Supplies Gemini API and database credentials. · `.env.example`
- [Shared runtime utilities](https://github.com/lugodoy/finance-agent-mcp/blob/main/shared/config_logging.py): Python utilities · Provides configuration, logging, and date handling. · `shared/config_logging.py`

## Connections

- Chat styling → Streamlit chat app: styles
- Streamlit chat app → Gemini agent: user question
- Gemini agent → Finance prompts: loads context
- Gemini agent → Google Gemini API: interprets and generates
- Gemini agent → MCP server: invokes tools
- MCP server → Expense item lookup: exposes
- MCP server → Expense summaries: exposes
- Expense item lookup → MySQL query layer: parameterized lookup
- Expense summaries → MySQL query layer: parameterized aggregation
- MySQL query layer → Transactions table: queries
- MySQL 8.0 → Transactions table: hosts
- Sample data seed → Transactions table: provisions
- Runtime config → Gemini agent: API key
- Runtime config → MySQL query layer: DB credentials
- Shared runtime utilities → MCP server: runtime support
- MCP server → Gemini agent: structured results
- Gemini agent → Streamlit chat app: formatted response
