Skip to main content
Glama
MCP Financial Calculator

MCP Financial Calculator

README.md
# MCP Financial Calculator

[中文说明](./README.zh-CN.md)

A Node.js MCP service that provides:

- Basic arithmetic: `add`, `subtract`, `multiply`, `divide`
- Cash flow and return tools: `xnpv`, `xirr`, `npv`, `irr`, `mirr`
- Time value of money tools: `pv`, `fv`, `pmt`, `nper`, `rate`
- Other metrics: `cagr`, `payback_period`, `discounted_payback_period`

## 1. Install

```bash
npm install
```

## 2. Start

```bash
npm start
```

The service communicates with MCP clients via stdio.

## 3. Run Tests

```bash
npm test
```

## 4. MCP Config Example

```json
{
  "mcpServers": {
    "financial-calculator": {
      "command": "node",
      "args": ["/absolute/path/to/mcp-financial-calculator/src/index.js"]
    }
  }
}
```

## 5. Tool Input Examples

1. `xnpv`
Input:
```json
{
  "rate": 0.1,
  "cashflows": [
    { "amount": -10000, "date": "2024-01-01" },
    { "amount": 12000, "date": "2025-01-01" }
  ]
}
```

2. `xirr`
Input:
```json
{
  "cashflows": [
    { "amount": -10000, "date": "2024-01-01" },
    { "amount": 12000, "date": "2025-01-01" }
  ],
  "guess": 0.1
}
```

3. `npv`
Input:
```json
{
  "rate": 0.1,
  "cashflows": [-10000, 3000, 4200, 6800]
}
```

4. `irr`
Input:
```json
{
  "cashflows": [-10000, 3000, 4200, 6800],
  "guess": 0.1
}
```

5. `mirr`
Input:
```json
{
  "cashflows": [-10000, 3000, 4200, 6800],
  "financeRate": 0.08,
  "reinvestRate": 0.1
}
```

6. `pv`
Input:
```json
{
  "rate": 0.01,
  "nper": 12,
  "pmt": -1000,
  "fv": 0,
  "type": 0
}
```

7. `fv`
Input:
```json
{
  "rate": 0.01,
  "nper": 12,
  "pmt": -1000,
  "pv": 0,
  "type": 0
}
```

8. `pmt`
Input:
```json
{
  "rate": 0.01,
  "nper": 12,
  "pv": 10000,
  "fv": 0,
  "type": 0
}
```

9. `nper`
Input:
```json
{
  "rate": 0.01,
  "pmt": -1000,
  "pv": 10000,
  "fv": 0,
  "type": 0
}
```

10. `rate`
Input:
```json
{
  "nper": 12,
  "pmt": -1000,
  "pv": 10000,
  "fv": 0,
  "type": 0,
  "guess": 0.01
}
```

11. `cagr`
Input:
```json
{
  "beginningValue": 100,
  "endingValue": 150,
  "years": 3
}
```

12. `payback_period`
Input:
```json
{
  "cashflows": [-10000, 3000, 4200, 6800]
}
```

13. `discounted_payback_period`
Input:
```json
{
  "discountRate": 0.1,
  "cashflows": [-10000, 3000, 4200, 6800]
}
```

## 6. Parameter Constraints

- Cashflow tools usually require at least 2 values.
- `xirr`, `irr`, and `mirr` require both positive and negative cashflows.
- Rate parameters must be greater than `-1`.
- Irregular cashflow `date` must be strict `YYYY-MM-DD`.
- `type` only supports `0` (end of period) or `1` (beginning of period).

## 7. Code Structure

- `src/index.js`: MCP server bootstrap only
- `src/tools/`: tool registration modules
- `src/lib/finance.js`: financial algorithms
- `src/lib/result.js`: unified result format
- `src/schemas.js`: input validation schemas

## 8. Finance Concepts in Plain Language

- `NPV` / `XNPV`:
  Convert future cashflows into today's value by discounting them with a rate. If NPV is positive, the investment is better than your required return.
- `IRR` / `XIRR`:
  Find the rate that makes NPV equal to 0. This project uses numerical solving because there is usually no closed-form formula.
- `MIRR`:
  A more stable alternative to IRR with two explicit assumptions: borrowing cost (`financeRate`) and reinvestment return (`reinvestRate`).
- `PV` / `FV`:
  `PV` asks "what is a future amount worth today?" and `FV` asks "what will today's plan be worth in the future?".
- `PMT` / `NPER` / `RATE`:
  Loan or savings planning trio:
  `PMT` = payment per period,
  `NPER` = number of periods,
  `RATE` = implied periodic interest rate.
- `CAGR`:
  Smoothed annual growth rate from a start value to an end value over years.
- `Payback Period`:
  How long until cumulative cashflows recover the initial outflow.
- `Discounted Payback Period`:
  Same as payback period, but each future cashflow is discounted first.

## 9. Sign Convention (Important)

- Outflow (money you pay) should be negative.
- Inflow (money you receive) should be positive.
- Mixed signs are required for `IRR`, `XIRR`, and `MIRR`.

## 10. Notes on Numerical Results

- Floating-point math is approximate, so tiny differences are normal.
- `XNPV` / `XIRR` use an Actual/365 day basis in this implementation.

TDQS

C2.9/5.0

Scored across 17 tools

Disambiguation4/5

Each tool maps to a distinct financial formula, and pairs like irr/xirr, npv/xnpv, and rate/cagr are separated by the periodic-vs-irregular cashflow distinction stated in their descriptions. The add/subtract/multiply/divide group is trivially distinct but feels tangential to the financial-analysis core, and a couple of near-neighbors (irr vs xirr, cagr vs rate) still require reading descriptions to pick correctly.

Naming Consistency4/5

All names are lowercase snake_case using standard finance abbreviations (xirr, nper, mirr, discounted_payback_period), which reads predictably as a set. There is no verb_noun convention, but that is appropriate for a calculator where the tool name is the formula, so consistency holds with only minor length deviations.

Tool Count4/5

At 17 tools the surface is on the heavy side, but each corresponds to a specific, non-redundant financial function rather than a variant of the same action. The four basic arithmetic tools are the only entries that feel like padding for a 'financial calculator' scope.

Completeness4/5

The set covers time value of money (fv, pv, pmt, nper, rate), cashflow valuation (npv, xnpv, irr, xirr, mirr), and payback analysis, which is solid coverage for the stated purpose. Minor gaps remain around amortization schedules, effective/nominal rate conversion, and compounding helpers, but agents can compose these from existing tools.

Maintenance

ActivityInactive
ResponsivenessNo recent community issues